#!/bin/bash
# Access Kmix volume trough scripting

EXITCODE_HELP=5
EXITCODE_ERROR=1
PROG_NAME='volume'
PROG_AUTHOR='Straight-A-Software'
PROG_EMAIL='x_terminat_or_3@yahoo.fr'
PROG_VERSION='2.0'
BASE=`basename $0`
TEMPFILE="$HOME/.volume.sh"

function end
{
 unset -f end error shorthelp info help morehelp dcopinit getvolume setvolume
 unset -f loadvolume mute unmute volume up down
 unset -v PROG_NAME PROG_VERSION PROG_AUTHOR BASE PROG_EMAIL XVALUE XVOLUME TEMPFILE VOLUME 
 #default to exitcode 0
 EXITCODE=0
 if [ $# -gt 0 ]; then
  if [ -n $1 ]; then
   EXITCODE=$1
  fi
 fi
 if [ $# -gt 1 ]; then
  if [ -n "$2" ]; then
   echo "$2"
  fi
 fi
 exit $EXITCODE
}

function error
{
 if [ $# -gt 0 ]; then
  end $EXITCODE_ERROR "$1"
 fi
 end $EXITCODE_ERROR
}

function shorthelp
{
 #this function is a trademark of Straight-A-Software
 #used in conjunction with 'myscripts' utility
 echo "Access Kmix volume trough cli"
 end $EXITCODE_HELP
}

function info
{
 echo "$PROG_NAME v$PROG_VERSION (c) $PROG_AUTHOR Released under the GPL2"
 echo "$PROG_EMAIL"
 echo ""
}

function help
{
 info
 echo "Access Kmix's volume trough command line interface."
 echo "Get, set, save volume"
 echo
 echo "Usage: $BASE --help | --shorthelp | --morehelp --version --info"
 echo "       $BASE --mute | --unmute | --up [xx] | --down [xx] | --volume [xxx]"
 echo ""
 end $EXITCODE_HELP
}

function morehelp
{
 info
 echo "Note: this program depends on Kmixer.  Install it with the KdeMultimedia "
 echo "package."
 echo ""
 echo "When you --mute, it will try to save a temporary file in your home directory"
 echo "to save the mastervolume so that when you use --unmute, the volume level will"
 echo "be at the same level as before."
 echo ""
 echo "If no temporary file is found when using --unmute, then the volume level will "
 echo "be set to 55%"
 echo ""
 echo "The name of the temporary file is ~.volumelevel.sh"
 echo "This file is removed after using --unmute"
 echo ""
 echo "When using --up, or --down, it defaults to 10% change unless specified otherwise"
 echo
 end $EXITCODE_HELP
}

function dcopinit
{
 type dcop >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Error dcop not found!"
 fi
 #dcop found, try to call it
 dcop >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Dcop cannot start!"
 fi
 #try kmix
 dcop "kmix" >/dev/null 2>&1
 if [ $? -gt 1 ]; then
  #kmix is NOT running
  type dcopstart > /dev/null 2>&1
  if [ $? -gt 0 ]; then
   #cannot use dcopstart, try to start it manually
   type kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Cannot find Kmix!"
   fi
   kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Cannot start Kmix!"
   fi
  else
   dcopstart kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Dcop server cannot start Kmix!"
   fi
  fi
  sleep .5 #alow kmix to start
 fi
 #see if kmix has a mastervolume
 dcop kmix Mixer0 masterVolume >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Kmixer does not have a mastervolume!"
 fi
}

function getvolume
{
 temp=$(dcop kmix Mixer0 masterVolume 2>&1)
 if [ $? -eq 0 ]; then
  XVOLUME=$temp
  return
 fi
 error "Problem retrieving volume!"
}

function setvolume
{
 VOLUME=55 #default
 if [ $# -gt 0 ]; then
   if [ $1 -ge 0 ]; then
    VOLUME=$1
   fi
   if [ $1 -gt 100 ]; then
    VOLUME=100
   fi
 fi
 dcop kmix Mixer0 setMasterVolume $VOLUME >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Problem setting volume!"
 fi
}

function savevolume
{
 getvolume
 echo "echo '$XVOLUME'" >"$TEMPFILE" 2>&1
 if [ -f "$TEMPFILE" ]; then
   chmod +x "$TEMPFILE" 2>&1
 fi
}

function loadvolume
{
 XVOLUME=55
 if [ -x "$TEMPFILE" ]; then
  XVOLUME=$("$TEMPFILE")
  if [ $XVOLUME -lt 0 ]; then
   XVOLUME=0
  elif [ $XVOLUME -gt 100 ]; then
   XVOLUME=100
  fi
  rm -rf "$TEMPFILE" > /dev/null 2>&1
 fi
}

function mute
{
 savevolume
 setvolume 0
 end
}

function unmute
{
 loadvolume
 setvolume $XVOLUME
 end
}

function volume
{
 SET=0
 if [ $# -gt 0 ]; then
  if [ -n $1 ]; then
   if [ $1 -gt -1 ]; then
    SET=1
   fi
  fi
 fi
 if [ $SET -eq 1 ]; then
  XVOLUME=$1
  if [ $XVOLUME -gt 100 ]; then
   XVOLUME=100
  fi
  setvolume $XVOLUME
  end
 fi
 getvolume
 echo $XVOLUME
 end
}

function up
{
 INC=10
 if [ $# -gt 0 ]; then
  if [ $1 -gt 0 ]; then
   INC=$1
  fi
 fi
 getvolume
 INC=$[$XVOLUME + $INC]
 if [ $INC -gt 100 ]; then
  INC=100
 fi
 setvolume $INC
 end
}

function down
{
 DEC=10
 if [ $# -gt 0 ]; then
  if [ $1 -gt 0 ]; then
   DEC=$1
  fi
 fi
 getvolume
 DEC=$[$XVOLUME - $DEC]
 if [ $DEC -lt 0 ]; then
  DEC=0
 fi
 setvolume $DEC
 end
}

#process command line arguments
if [ $# -eq 0 ]; then
 help
fi

XVALUE=-1

if [ $# -gt 1 ]; then
 if [ -n $2 ]; then
  XVALUE=$2
 fi
fi
  
case $1 in 
   --volume)
     dcopinit
     volume $XVALUE
     ;;
   --up)
     dcopinit
     up $XVALUE
     ;;
   --down)
     dcopinit
     down $XVALUE
     ;;
   --mute)
     dcopinit
     mute
     ;;
   --unmute)
     dcopinit
     unmute
     ;;
   --help)
     help
     ;;
   --shorthelp|--info)
     shorthelp
     ;;
   --morehelp)
     morehelp
     ;;
   --version)
     info
     end $EXITCODE_HELP
     ;;
esac

help