#!/bin/bash
#
#  ctrltgl.sh
#
#  Copyright (c) 2008 Fergus Incoronato
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, #  USA.
#

info() {
        echo "=======> $1"
}

warning() {
        info "WARNING: $1" >&2
}

error() {
        info "ERROR: $1" >&2
}

interrupted() {
        echo ""
        error "Interrupted."
        exit 1
}

parse_options(){
        if (($#<4)); then
                echo "`basename $CTRLTGL_COMMAND`: not enough arguments"
                exit 1
        elif (($#>4)); then
                echo "`basename $CTRLTGL_COMMAND`: too many arguments"
                exit 1
        fi

        while [ "$1" ]; do
                case $1 in
                        -c|--control)
                                if [ ! "$2" ]; then
                                        echo "`basename
$CTRLTGL_COMMAND`: option $1 requires an argument" exit 1
                                fi
                                CTRLTGL_CONTROL="$2"
                                CTRLTGL_OVFILE="$2.state"
                                shift ;;
                        -d|--directory)
                                if [ ! "$2" ]; then
                                        echo "`basename
$CTRLTGL_COMMAND`: option $1 requires an argument" exit 1
                                fi
                                CTRLTGL_WORK_DIR="$2"
                                shift ;;
                        *)
                                echo "`basename $CTRLTGL_COMMAND`:
invalid option $1" exit 1 ;;
                esac
                shift
        done
}

check_exists() {
        if [ ! -e "$1" ]; then
                error "'$1' does not exist."
                exit 1
        fi
}

check_is_directory() {
        if [ ! -d "$1" ]; then
                error "'$1' is not a directory."
                exit 1
        fi
}

check_is_regular() {
        if [ ! -f "$1" ]; then
                error "'$1' is not a regular file."
                exit 1
        fi
}

check_is_writable() {
        if [ ! -w "$1" ]; then 
                error "'$1' is not writable."
                exit 1
        fi
}

check_is_readable() {
        if [ ! -r "$1" ]; then
                error "'$1' is not readable."
                exit 1
        fi
}

main() {
        local OVFILE_SIZE NUMID MIN MAX OLD_VOL CUR_VOL NEW_VOL


        parse_options "$@"


        check_exists "$CTRLTGL_WORK_DIR"
        check_is_directory "$CTRLTGL_WORK_DIR"
        check_is_writable "$CTRLTGL_WORK_DIR"
        check_is_readable "$CTRLTGL_WORK_DIR"
        check_exists "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"
        check_is_regular "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"
        check_is_writable "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"
        check_is_readable "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"


        OVFILE_SIZE="`find "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE" -printf
"%s\n"`"

        if ((OVFILE_SIZE<1)) || ((OVFILE_SIZE>4)); then
                error "'$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE' size is
incompatible with allowed values." exit 1
        fi      


        NUMID="`amixer controls | grep "$CTRLTGL_CONTROL" | cut -d , -f
1 | cut -d = -f 2`" MIN="`amixer cget numid=$NUMID | grep ";" | cut
-d , -f 4 | cut -d = -f 2`" MAX="`amixer cget numid=$NUMID | grep ";" |
cut -d , -f 5 | cut -d = -f 2`"


        OLD_VOL="`cat "$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"`"

        if [ "$OLD_VOL" ]; then
                if ((OLD_VOL<MIN)); then
                        error "'$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE'
content is lesser than minimum allowed value." exit 1
                elif ((OLD_VOL>MAX)); then
                        error "'$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE'
content is greater than maximum allowed value." exit 1
                fi
        fi


        CUR_VOL="`amixer cget numid="$NUMID" | grep : | cut -d , -f 2`"

        if ((CUR_VOL==0)) && [ "$OLD_VOL" ]; then
                NEW_VOL="$OLD_VOL"
                CUR_VOL=""
        else
                NEW_VOL="0"
        fi


        amixer cset numid="$NUMID" "$NEW_VOL"

        sed "1s|^$OLD_VOL$|$CUR_VOL|" -i
"$CTRLTGL_WORK_DIR/$CTRLTGL_OVFILE"


        exit 0
}

trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM

readonly CTRLTGL_VERSION="0.1.0"
readonly CTRLTGL_COMMAND="$0"

main "$@"

# End of file
-- 
Value your freedom, or you will lose it, teaches history.
``Don't bother us with politics,'' respond those who don't
want to learn.

 -- Richard M. Stallman
    http://www.gnu.org/philosophy/linux-gnu-freedom.html

Attachment: signature.asc
Description: PGP signature

_______________________________________________
ml mailing list
[email protected]
http://nalug.net/mailman/listinfo/ml

Rispondere a