#!/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 (($#<5)); then
                echo "`basename $CTRLTGL_COMMAND`: not enough arguments"
                exit 1  
        elif (($#>5)); then
                echo "`basename $CTRLTGL_COMMAND`: too many arguments"
                exit 1  
        fi

        while [ "$1" ]; do
                case $1 in
                        set|sset)
                                if [ ! "$2" ]; then
                                        echo "`basename $CTRLTGL_COMMAND`: 
Specify a scontrol identifier: 'name',index"
                                        exit 1
                                elif [ ! "$3" ]; then
                                        echo "`basename $CTRLTGL_COMMAND`: 
Specify what you want to set..."
                                        exit 1
                                fi
                                SCONTROL="$2"
                                OVFILE="$2.state"
                                PARAMETER="$3"
                                shift
                                shift ;;
                        -d|--directory)
                                if [ ! "$2" ]; then
                                        echo "`basename $CTRLTGL_COMMAND`: 
option $1 requires an argument"
                                        exit 1
                                fi
                                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
}

toggle_volume() {
        if ((CUR_VOL!=MIN)) || [ ! "$OLD_VOL" ]; then
                NEW_VOL="$MIN"
        else
                CUR_VOL=""
                NEW_VOL="$OLD_VOL"
        fi      
}

mute_volume() {
        NEW_VOL="$MIN"
        if [ "$OLD_VOL" ]; then
                CUR_VOL="$OLD_VOL"
        fi
}

unmute_volume() {
        if [ ! "$OLD_VOL" ]; then
                NEW_VOL="$CUR_VOL"
        else
                NEW_VOL="$OLD_VOL"
        fi
        CUR_VOL=""
}

lower_volume() {
        if [ ! "$OLD_VOL" ]; then
                NEW_VOL=$((CUR_VOL-${PARAMETER%-}))
                if ((NEW_VOL<MIN)); then NEW_VOL="$MIN"; fi
                CUR_VOL="$OLD_VOL"
        else
                NEW_VOL="$CUR_VOL"
                CUR_VOL=$((OLD_VOL-${PARAMETER%-}))
                if ((CUR_VOL<MIN)); then CUR_VOL="$MIN"; fi
        fi
}

raise_volume() {
        if [ ! "$OLD_VOL" ]; then
                NEW_VOL=$((CUR_VOL+${PARAMETER%+}))
                if ((NEW_VOL>MAX)); then NEW_VOL="$MAX"; fi
                CUR_VOL="$OLD_VOL"
        else
                NEW_VOL="$CUR_VOL"
                CUR_VOL=$((OLD_VOL+${PARAMETER%+}))
                if ((CUR_VOL>MAX)); then CUR_VOL="$MAX"; fi
        fi
}

main() {
        local SCONTROL OVFILE PARAMETER WORK_DIR OVFILE_SIZE NUMID MIN MAX 
OLD_VOL CUR_VOL NEW_VOL

        parse_options "$@"

        check_exists "$WORK_DIR"
        check_is_directory "$WORK_DIR"
        check_is_writable "$WORK_DIR"
        check_is_readable "$WORK_DIR"

        check_exists "$WORK_DIR/$OVFILE"
        check_is_regular "$WORK_DIR/$OVFILE"
        check_is_writable "$WORK_DIR/$OVFILE"
        check_is_readable "$WORK_DIR/$OVFILE"

        OVFILE_SIZE="`find "$WORK_DIR/$OVFILE" -printf "%s\n"`"

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

        NUMID="`amixer controls | grep "$SCONTROL" | 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 "$WORK_DIR/$OVFILE"`"
        CUR_VOL="`amixer cget numid="$NUMID" | grep : | cut -d , -f 2`"

        if [ "$OLD_VOL" ]; then
                if ((CUR_VOL!=MIN)); then
                        sed "1s|^$OLD_VOL$||" -i "$WORK_DIR/$OVFILE"
                        OLD_VOL=""
                elif ((OLD_VOL<MIN)); then
                        sed "1s|^$OLD_VOL$|$MIN|" -i "$WORK_DIR/$OVFILE"
                        OLD_VOL="$MIN"
                elif ((OLD_VOL>MAX)); then
                        sed "1s|^$OLD_VOL$|$MAX|" -i "$WORK_DIR/$OVFILE"
                        OLD_VOL="$MAX"
                fi
        fi

        case $PARAMETER in
                toggle)
                        toggle_volume ;;
                mute)
                        mute_volume ;;
                unmute)
                        unmute_volume ;;
                *[[:digit:]]-)
                        lower_volume ;;
                *[[:digit:]]+)
                        raise_volume ;;
                *)
                        echo "`basename $CTRLTGL_COMMAND`: invalid parameter 
$PARAMETER"
                        exit 1 ;;
        esac

        sed "1s|^$OLD_VOL$|$CUR_VOL|" -i "$WORK_DIR/$OVFILE"

        amixer sset "$SCONTROL" "$NEW_VOL"

        exit 0
}

trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM

readonly CTRLTGL_VERSION="0.2.30"
readonly CTRLTGL_COMMAND="$0"

main "$@"

# End of file

Attachment: signature.asc
Description: PGP signature

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

Rispondere a