#!/bin/sh
#functions for pupRadio #by 01micko.. 01micko@gmail.com 
#gpl2 licence

BOOK_DIR=$APP_DIR/bookmarks

TBOOK_DIR=$APP_DIR/tbookmarks

. $APP_DIR/stations

. $APP_DIR/urls


OLD_NAME=`grep "CH$RNUMBER" $APP_DIR/stations`
NEW_NAME="CH$RNUMBER=\"$STATION\""

OLD_URL=`grep "URL$RNUMBER" $APP_DIR/urls`
NEW_URL="URL$RNUMBER=\"$RSURL\""

. $APP_DIR/tel-stations

. $APP_DIR/tel-urls


OLD_TNAME=`grep "TCH$NUMBER" $APP_DIR/tel-stations`
NEW_TNAME="TCH$NUMBER=\"$CHANNEL\""

OLD_TURL=`grep "TURL$NUMBER" $APP_DIR/tel-urls`
NEW_TURL="TURL$NUMBER=\"$SURL\""

echo "PLAYER="$PLAYER"" > $APP_DIR/config

GXINEVER=`gxine --help | grep '^gxine ' | cut -f 2 -d ' ' 2>/dev/null`

if [ "$PLAYER" = "mplayer" ]; then OPT="-fs" ;fi
if [ "$PLAYER" = "gxine" ]; then  
 if [ "$GXINEVER" = "0.5.904" ]; then OPT="" 
    else OPT="-fs"
 fi
fi
if [ "$PLAYER" = "xine" ]; then OPT="-F" ;fi
if [ "$PLAYER" = "gnome-mplayer" ]; then OPT="--g-fatal-warnings -q" ;fi
if [ "$PLAYER" = "vlc" ]; then OPT="-f" ;fi

HELP=`cat $APP_DIR/help`

case $1 in
1)killall $PLAYER ; $PLAYER $URL1 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH1" &
	echo "$PLAYER $URL1 &" > $APP_DIR/last-played
;;
2)killall $PLAYER ; $PLAYER $URL2 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH2" &
	echo "$PLAYER $URL2 &" > $APP_DIR/last-played
;;
3)killall $PLAYER ; $PLAYER $URL3 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH3" &
	echo "$PLAYER $URL3 &" > $APP_DIR/last-played
;;
4)killall $PLAYER ; $PLAYER $URL4 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH4" &
	echo "$PLAYER $URL4 &" > $APP_DIR/last-played
;; 
5)killall $PLAYER ; $PLAYER $URL5 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH5" &
	echo "$PLAYER $URL5 &" > $APP_DIR/last-played
;;
6)killall $PLAYER ; $PLAYER $URL6 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH6" &
	echo "$PLAYER $URL6 &" > $APP_DIR/last-played
;; 
7)killall $PLAYER ; $PLAYER $URL7 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH7" &
	echo "$PLAYER $URL7 &" > $APP_DIR/last-played
;;
8)killall $PLAYER ; $PLAYER $URL8 &   killall xmessage; xmessage -center -timeout 15 "$(gettext 'Now playing') $CH8" &
	echo "$PLAYER $URL8 &" > $APP_DIR/last-played
;; 
T1)killall $PLAYER ; $PLAYER  $OPT $TURL1 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH1" &
;;
T2)killall $PLAYER ; $PLAYER $OPT $TURL2 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH2" &
;;
T3)killall $PLAYER ; $PLAYER $OPT $TURL3 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH3" &
;;
T4)killall $PLAYER ; $PLAYER $OPT $TURL4 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH4" &
;; 
T5)killall $PLAYER ; $PLAYER $OPT $TURL5 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH5" &
;;
T6)killall $PLAYER ; $PLAYER $OPT $TURL6 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH6" &
;;
T7)killall $PLAYER ; $PLAYER $OPT $TURL7 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH7" &
;;
T8)killall $PLAYER ; $PLAYER $OPT $TURL8 &  xmessage -center  -timeout 5 "$(gettext 'please wait, starting') $TCH8" &
;; 
apply) sed -i -e "s%$OLD_NAME$%$NEW_NAME%" $APP_DIR/stations
           sed -i -e "s%$OLD_URL$%$NEW_URL%" $APP_DIR/urls
;;
Tapply) sed -i -e "s%$OLD_TNAME$%$NEW_TNAME%" $APP_DIR/tel-stations
            sed -i -e "s%$OLD_TURL$%$NEW_TURL%" $APP_DIR/tel-urls
;;
playnow)killall $PLAYER ; $PLAYER $RSURL &
	echo "$PLAYER $RSURL &" > $APP_DIR/last-played
;;
Tplaynow)killall $PLAYER ; $PLAYER $OPT $SURL &
;;
radd)STAT=`echo "$STATION" | awk '{print $1}'`
		echo "$RSURL" >> $BOOK_DIR/$STAT
;;
tadd)CHAN=`echo "$CHANNEL" | awk '{print $1}'`
		echo "$SURL" >> $TBOOK_DIR/$CHAN
;;
rplay) TOPLAY=`cat $BOOK_DIR/"$PLAYSTATION"`
          killall $PLAYER; $PLAYER $TOPLAY &
          echo "$PLAYER $TOPLAY &"  > $APP_DIR/last-played
;;
tplay) TOWATCH=`cat $TBOOK_DIR/"$PLAYCHANNEL"`
          killall $PLAYER; $PLAYER $OPT $TOWATCH &
;;
restart)killall $PLAYER &
	    killall xmessage &
			for i in `ps | grep "PUPRADIO" | awk '{print $1}'`
				do kill -9 $i
				done
			$APP_DIR/pupradio &
;;
stop) if [ "PLAYER" = "gnome-mplayer" ];then for i in `ps | grep "gnome-mplayer" | awk '{print $1}'`
			do kill -9 $i
			done 2>/dev/null
			else killall $PLAYER &
	  fi
		killall xmessage &
;;
help)xmessage -center   "$HELP" &
;;
quit) if [ "PLAYER" = "gnome-mplayer" ];then for i in `ps | grep "gnome-mplayer" | awk '{print $1}'`
			do kill -9 $i
			done 2>/dev/null
			else killall $PLAYER &
	  fi
	   killall xmessage &
	  
		for i in `ps | grep "PUPRADIO" | awk '{print $1}'`
			do kill -9 $i
			done 2>/dev/null
		
		exit 
;;

esac
