#!/bin/bash
# ices_cream host port passwd mount channels stream_name stream_genre stream_descrpt path_archiv
#
 
# bretzel@apo33.org
# copyright (C) 2005 $apo33
#
# 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.
#


title="ices_cream icecast ogg jack client"
ask_host="server adress ? ex: 127.0.0.1"
ask_port="server port ? ex: 8000"
ask_password="server password ?"
ask_mount="name for the mount point ? ex:exemple_ogg"
ask_chnl="nbr of channel ex :2"
ask_quality="Ogg/Vorbis compression quality (1)"
ask_resample="Ogg/Vorbis resample bitrate ? "
ask_name="name in the stream file ?"
ask_genre="genre in the stream file ?"
ask_descrpt="description in the stream file ?"
ask_archiv="archive in file ?"
ask_path_archiv="where archiv ?"
ask_go="GO !  or Exit "
ask_ices="Yes     = new client
Last    = reload the last one
killOne = kill an old one
KillAll = kill all ices client
Exit    = ... exit"

case $LANG in
	fr*)
	title="ices_cream icecast ogg jack client Streaming avec ices"
	ask_host="Adresse du Serveur ? ex: 127.0.0.1"
	ask_port="No du port ? ex: 8000"
	ask_password="mot de passe sur le serveur ?"
	ask_mount="nom pour le point de montage ? ex:exemple_ogg"
	ask_chnl="nbr de canaux ex :2"
	ask_quality="Qualite de compression Ogg/Vorbis (1)"
	ask_resample="Bitrate pour un resample Ogg/Vorbis ? "
	ask_name="nom presente dans le stream ?"
	ask_genre="genre ?"
	ask_descrpt="description ?"
	ask_archiv="archiver dans un fichier ?"
	ask_path_archiv="Ou archiver ?"
	ask_go="GO ! ou Sortir "
	ask_ices="Yes     = Nouveau client
Last    = Relancer le dernier
killOne = Tuer un clients ices
killAll = Tuer tout les clients ices
Exit    = Sortir"
	;;
esac

mkdir -p $HOME/.ices
cd $HOME/.ices

gxmessage -center -title "$title" -button Yes,Last,kill_One,Kill_all,Exit "$ask_ices"
RET=$?
if [ $RET = 105 ];then
	exit 0
fi
if [ $RET = 102 ];then

	if [ -f ices.xml ]; then
		PIDFILE=` grep "<pidfile>" ices.xml|sed -e "s/<pidfile>//
s/<\/pidfile>//" | awk -F "_" '{ print $2 }' `
		PIDFILE=`ps -ef|grep "$PIDFILE"|awk '{ print $2 }' `
		if [ "$PIDFILE" ]; then
			gxmessage -center -button KILL,NO "Kill old $PIDFILE Yes/No ?"
			RET=$?
			if [ $RET = 102 ];then
				exit 0
			fi
			if [ $RET = 101 ];then
				kill -9 "$PIDFILE"
				zenity --warning --text="kill -9 $ICES"
			fi
		fi
		ices ices.xml &
		exit 0
	else
		zenity --warning --text="no ices process"
		exit 0
	fi
fi

if [ $RET = 104 ];then
	killall -9 ices
	su root -c "rxvt -e 'killall -9 ices'"
	exit 0
fi
if [ $RET = 103 ];then
	for i in `ls -1 /var/log/ices/ices*pid`;do
		ICES=`cat $i`
		gxmessage -center -button KILL,NO "Kill $ICES $i Yes/No ?"
		RET=$?
		if [ $RET = 101 ];then
			kill -9 "$ICES"
			rm -f $i 
			zenity --warning --text="kill -9 $ICES"
		fi
	done
	exit 0
fi

host=`zenity --entry --title="$title" --text="$ask_host"`
port=`zenity --entry --title="$title" --text="$ask_port"`
password=`zenity --entry --title="$title" --text="$ask_password"`
mount=`zenity --entry --title="$title" --text="$ask_mount"`
channels=`zenity --title="$title" --list --radiolist --column="Nbr" --column="$ask_chnl" FALSE "1" TRUE "2" FALSE "3" FALSE "4" `

quality=`zenity --height=300 --title="$title" --list --radiolist --column="" --column="$ask_quality" -- "-1" FALSE "0" TRUE "1" FALSE "2" FALSE "3" FALSE "4" FALSE "5" FALSE "6" FALSE "7" FALSE "8" FALSE "9" FALSE "10"`

resample=`zenity --height=250 --title="$title" --list --radiolist --column="" --column="$ask_resample" FALSE "11025" FALSE "22050" TRUE "44100" FALSE "44800" `


name=`zenity --entry --title="$title" --text="$ask_name"`
genre=`zenity --entry --title="$title" --text="$ask_genre"`
descrpt=`zenity --entry --title="$title" --text="$ask_descrpt"`

gxmessage -center -button Go,Exit "$ask_go"
RET=$?
if [ $RET = 102 ];then
	exit 0
fi

gxmessage -center -button Yes,No "$ask_archiv"
RET=$?
if [ $RET = 101 ];then
	archiv=1
	path_archiv=`zenity --entry --title="$title" --text="$ask_path_archiv"`
else
	archiv=0
fi


###on test quelque entrees
 
if [ -z "$host" ] ;then host=127.0.0.1;fi
if [ -z "$port" ] ;then port=8000 ;fi
if [ -z "$password" ] ;then password=hackme ;fi
if [ -z "$mount" ] ;then mount="ices.ogg"    ;fi
if [ -z "$name" ] ;then name="exemple stream"    ;fi
if [ -z "$genre" ] ;then genre="genre stream"   ;fi
if [ -z "$descrpt" ] ;then descrpt="stream description" ;fi
if [ -z "$quality" ] ;then quality=1  ;fi
if [ -z "$channels" ] ;then channels=2  ;fi
if [ -z "$resample" ] ;then resample=44100  ;fi
 
###on fabrique le fichier xml
 
echo '<?xml version="1.0"?>
<ices>
    <background>0</background>
    <realtime>0</realtime>
   <user>root</user>
    <logpath>/var/log/ices</logpath>
    <logsize>2048</logsize>
    <logfile>ices.log</logfile>
    <loglevel>4</loglevel>
    <consolelog>0</consolelog>
    <pidfile>/var/log/ices/ices_'$mount'_'$$'.pid</pidfile>
        <stream>
        <name>'$name'</name>
        <genre>'$genre'</genre>
        <description>'$descrpt'</description>
                <input>
                        <module>jack</module>
                        <param name="channels">'$channels'</param>
                        <param name="clientname">ices_'$mount'_'$$'</param>
'>>/tmp/ices_"$mount"_"$$".xml
if [ "$channels" -eq "1" ];then
	echo '
			<param name="connect">alsa_pcm:capture_1</param>
'>>/tmp/ices_"$mount"_"$$".xml
fi
if [ "$channels" -eq "2" ];then
	echo '
			<param name="connect">alsa_pcm:capture_1,alsa_pcm:capture_2</param>
'>>/tmp/ices_"$mount"_"$$".xml
fi
if [ "$channels" -eq "3" ];then
	echo '
			<param name="connect">alsa_pcm:capture_1,alsa_pcm:capture_2,alsa_pcm:capture_3</param>
'>>/tmp/ices_"$mount"_"$$".xml
fi
if [ "$channels" -eq "4" ];then
	echo '
			<param name="connect">alsa_pcm:capture_1,alsa_pcm:capture_2,alsa_pcm:capture_3,alsa_pcm:capture_4</param>
'>>/tmp/ices_"$mount"_"$$".xml
fi
echo '
                        <param name="metadatafilename">metadata</param>
                </input>
        <runner>
            <instance>
                <name>'$name'</name>
                <genre>'$genre'</genre>
                <description>'$descrpt'</description>
                <shout>
                    <hostname>'$host'</hostname>
                    <port>'$port'</port>
                    <password>'$password'</password>
                    <mount>'$mount'</mount>
                    </shout>
 
                <resample>
                    <out-rate>'$resample'</out-rate>
                </resample>
 
                <!--
                <downmix>1</downmix>
                -->
                <encode>
                    <quality>'$quality'</quality>
                </encode>
            </instance>
'>>/tmp/ices_"$mount"_"$$".xml
if [ "$archiv" = 1 ] ;then
            echo '<instance>
                <encode>
                    <quality>1.1</quality>
                </encode>
                <savestream>
                    <filename>'$path_archiv'/%X/stream-%M.ogg</filename>
 
                    <fmask>0600</fmask>
                    <dmask>0700</dmask>
                    <duration>7200</duration>
                    <on-metadata>1</on-metadata>
                    <reset-time>0</reset-time>
                </savestream>
            </instance>
        '>>/tmp/ices_"$mount"_"$$".xml
fi
echo '
        </runner>
        </stream>
</ices>
'>>/tmp/ices_"$mount"_"$$".xml
 
### on lance ices
 
ices /tmp/ices_"$mount"_"$$".xml &

### on test si la glace a fondue
 
sleep 1

if [ -f /var/log/ices/ices_"$mount"_"$$".pid ]; then 
	zenity --warning --text "ices client on $$ ok"
	cp /tmp/ices_"$mount"_"$$".xml .
	ln -sf ices_"$mount"_"$$".xml ices.xml

else
	zenity --warning --text "ices client on $$ down"
fi
 
##########################
