#! /bin/sh

# esound-control.sh - A little script to easily control of EsounD,
# the Enlightened Sound Daemon
#
# Copyright (c) 2000 Raphaël HALIMI <raph@captainblood.org>
#
# 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

# Some variables...
VERSION="0.9.1"
TMPFILE=/tmp/esd-controller_tempfile.$$

# Which interface will we use ?
case $1 in
   "")
      if [ -n "`which Xdialog`" -a -n "$DISPLAY" ] ; then
         INTERFACE="DIALOG"
	 DIALOG=Xdialog
      elif [ -n "`which dialog`" ] ; then
         INTERFACE="DIALOG"
	 DIALOG=dialog
      else
         INTERFACE="BASH"
      fi
      ;;
   "--bash")
      INTERFACE="BASH"
      ;;
   *)
      echo -e "EsounD Controller v$VERSION, (c) 2000 Raphaël HALIMI <raph@captainblood.org>\n"
      echo "This software comes with ABSOLUTELY NO WARRANTY; for details, please look inside"
      echo "the script, at the top comments. This is free software, and you are welcome to"
      echo "redistribute it under certain conditions; again, for details, please look at"
      echo -e "the top comments inside the script.\n"
      echo "Usage: esound-control.sh [option]"
      echo "Options:"
      echo -e "  --bash - force using bash interface\n"
      echo "If no option is specified, the script will use Xdialog by default if you have"
      echo "it and if an X display is available, or will fall back to dialog, and it will"
      echo "finally fall back to bash if you have neither Xdialog nor dialog."
      exit
      ;;
esac


# Now we start...
while true ; do
  if [ "$INTERFACE" = "DIALOG" ] ; then

      # See if EsounD is running and/or on standby, to build the menu
      if [ `ps ax | grep esd | grep -v grep | awk '{print $1}'` ] ; then
         RUNNING="YES"
	 OPTION_ONE="Kill"
	 COMMENT_ONE="Kill EsounD server"
	 if [ "`esdctl standbymode`" = "server is running" ] ; then
	    STANDBY="RUNNING"
	    OPTION_TWO="Standby"
	    COMMENT_TWO="Put EsounD server on standby"
	 elif [ "`esdctl standbymode`" = "server is on standby" ] ; then
	    STANDBY="STANDBY"
	    OPTION_TWO="Run"
	    COMMENT_TWO="Put back EsounD to running state"
	 fi
      else
         RUNNING="NO"
	 OPTION_ONE="Start"
	 COMMENT_ONE="Start EsounD server"
	 STANDBY="NOT RUNNING"
	 OPTION_TWO="Standby"
	 COMMENT_TWO="Put EsounD server on standby"
      fi

      # And now the menu
      $DIALOG --clear --title "EsounD Controller v$VERSION" \
             --backtitle "Captain Blood's Scripts : http://www.captainblood.org" \
             --menu "\nEsounD started : $RUNNING \nEsounD state : $STANDBY \n\nWhat do you want to do ?" 0 0 0 \
	     "1. $OPTION_ONE server" "$COMMENT_ONE" \
	     "2. $OPTION_TWO server" "$COMMENT_TWO" \
             "3. See server info" "See info about the server only" \
             "4. See all info" "See info about both server and clients" \
	     "5. Switch to Bash" "Switch to Bash interface" \
	     2> $TMPFILE
      
      RETVAL=$?
      CHOICE=`cat $TMPFILE`
      
      case "$RETVAL" in
       0)
         case "$CHOICE" in
	  "1. Kill server")
	   $DIALOG --title "Kill EsounD server" --defaultno \
	          --yesno "It may kill ALL of your currently running applications\nthat use EsounD. Are you sure you want to kill EsounD ?" 0 0
           if [ "$?" = "0" ] ; then
	      killall esd
	      $DIALOG --title "Info" --msgbox "Server killed." 0 0
	   fi
	   ;;
	 "1. Start server")
	  esd > /dev/null &
	  esdctl unlock
	  $DIALOG --title "Info" --msgbox "Server started." 0 0
	  ;;
	 "2. Standby server")
	  if [ "$RUNNING" = "NO" ] ; then
	     $DIALOG --title "Oops !" \
	             --msgbox "The server's not started yet :-)" 0 0
	  elif [ "$RUNNING" = "YES" ] ; then
	     esdctl off
	     $DIALOG --title "Info" \
	             --msgbox "EsounD server is on standby.\nPress OK to run it again." 0 0
	     esdctl on
	  fi
	  ;;
	 "2. Run server")
	  esdctl on
	  ;;
	 "3. See server info")
	  esdctl serverinfo > $TMPFILE
	  $DIALOG --title "Server info" --textbox $TMPFILE 0 0
	  ;;
	 "4. See all info")
	  esdctl allinfo > $TMPFILE
	  $DIALOG --title "All info" --textbox $TMPFILE 0 0
	  ;;
	 "5. Switch to Bash")
	  if [ "$DIALOG" = "Xdialog" -a "$TERM" != "xterm" ] ; then
	     $DIALOG --title "Warning" \
	             --msgbox "This script wasn't started from an Xterm." 0 0
	  else
	     rm $TMPFILE
	     $DIALOG --sleep 2 \
	             --title "Bye bye !" \
		     --infobox "Switching to Bash interface." 0 0
	     INTERFACE="BASH"
	  fi
	  ;;
	 esac
	 ;;
       1)
         rm $TMPFILE
         exit
	 ;;
       255)
         rm $TMPFILE
         exit
	 ;;
      esac
  fi

  if [ "$INTERFACE" = "BASH" ] ; then
      clear
      echo -e "Captain Blood's Scripts : http://www.captainblood.org\n"
      echo "EsounD Controller v$VERSION" ; echo

      # See if EsounD is running and/or on standby, it will be usefull later
      if [ `ps ax | grep esd | grep -v grep | awk '{print $1}'` ] ; then
         RUNNING=1
	 if [ "`esdctl standbymode`" = "server is running" ] ; then
	    STANDBY=0
	 elif [ "`esdctl standbymode`" = "server is on standby" ] ; then
	    STANDBY=1
	 fi
      else
         RUNNING=0
	 STANDBY=0
      fi

      # We print some informations... In Technicolor :-)
      GREEN="\033[32m"
      RED="\033[31m"
      NORMAL="\033[0m"

      if [ "$RUNNING" = 1 ] ; then
         echo -e "EsounD started : "$GREEN"YES"$NORMAL
	 if [ "$STANDBY" = 0 ] ; then
	    echo -e "EsounD state : "$GREEN"RUNNING"$NORMAL
	 elif [ "$STANDBY" = 1 ] ; then
	    echo -e "EsounD state : "$RED"STANDBY"$NORMAL
	 fi
      elif [ "$RUNNING" = 0 ] ; then
           echo -e "EsounD started : "$RED"NO"$NORMAL
      fi

      echo

      # And now the menu
      echo "What do you want to do ?" ; echo
      if [ "$RUNNING" = 0 ] ; then
         echo "1. Start server"
      elif [ "$RUNNING" = 1 ] ; then
         echo "1. Kill server"
      fi      
      if [ "$STANDBY" = 0 ] ; then 
         echo "2. Standby server"
      elif [ "$STANDBY" = 1 ] ; then
         echo "2. Run server"
      fi      
      echo "3. See server info"
      echo "4. See server/clients info"
      echo "5. Switch to Dialog interface"
      echo; echo -n "Which option (or press [ENTER] to exit) ? " ; read ACTION
      case "$ACTION" in
       1)
	if [ "$RUNNING" = 1 ] ; then
	   echo ; echo "It may kill ALL currently running applications which use EsounD."
	   echo -n "Are you sure you want to kill the EsounD server ? (y/N) " ; read SURE
	   if [ "$SURE" = Y -o "$SURE" = y ] ; then
	      killall esd
	      echo ; echo -n "Server killed." ; sleep 1
	   fi   
	elif [ "$RUNNING" = 0 ] ; then
	   esd > /dev/null &
	   esdctl unlock
	   echo ; echo -n "Server started." ; sleep 1
	fi
        ;;
       2)
        if [ "$RUNNING" = 0 ] ; then
	   echo ; echo -n "Er... The server's not started yet :-)" ; sleep 2
	elif [ "$RUNNING" = 1 ] ; then
	   if [ "$STANDBY" = 0 ] ; then
	      esdctl off
	      clear ; echo -e "EsounD server is on standby.\n"
	      echo -n "Press ENTER to run it again." ; read
	      esdctl on
	   elif [ "$STANDBY" = 1 ] ; then
	      esdctl on
	   fi
	fi
        ;;
       3)
        clear ; esdctl serverinfo | more ; echo
	echo -n "Press ENTER to return to the menu. " ; read
	;;
       4)
        clear ; esdctl allinfo | more ; echo
	echo -n "Press ENTER to return to the menu. " ; read
	;;
       5)
        clear ; echo -n "Switching to Dialog interface." ; sleep 2
	INTERFACE="DIALOG"
	clear
	;;
      "")
	exit
	;;
       *)
        echo ; echo -n "That's not an option !" ; sleep 2
        ;;
      esac
  fi
done
