#!/bin/bash

##################
# default settings for host, port and password
server="localhost"
port="9000"
passwordu="hackme"
passworda="hackme"
passwordr="hackme"
CONFFILE=$HOME/icecast.conf
su root -c "chmod -R 777 /var/log/icecast*"
###############
# find out the ip-address
myip=`/sbin/ifconfig | grep inet | grep -v 127.0.0 | awk {'print $2;'} | sed s/.*://`

if [ "$myip" == "" ]
then
	myip=$server;
fi
###############
# getting the strings in relation to the settings

[ -f /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n

title="icecast2 script"

case "$LANGUAGE" in
fr_FR:fr)
M01="Script permet de lancer le streamer icecast1 ou 2 \n pour mp3 ou OGG \n\n[Control]-C pour sortir\n\n"
M02=" Configuration trouvee "
M03=" Configuration trouvee, devons nous l'utiliser ?"
M04="Veuillez choisir des serveurs"
M05="Veuillez suggérer des données de serveur"
M06="Server:"
M07="Port:"
M08u="User  Password:"
M08a="Admin Password:"
M09="Veuillez indiquer un débit binaire"
M10="Veuillez indiquer un mode"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Veuillez indiquer un point de montage (mountpoint)"
M15="live.ogg"
M16="Veuiller indiquer la description du stream"
M17="Stream Live"
M18=" Resume "
M19="L'adresse résultant est:"
M20=" Choix de programme "
M21="Veuillez choisir le programme:"
B16="16Kbit - Radio, Voix - tres mauvais"
B24="24Kbit - Radio, Voix - mauvais"
B32="32Kbit - Radio, Voix"
B48="48Kbit - Radio, Voix"
B56="56Kbit - Radio, qualitee moyenne"
B64="64kbit - Radio, musique"
B96="96kbit - Bonne qualitee"
B128="128kbit - CD-Qualite"
;;
de|at|ch)
M01="Dies ist ein einfaches Script um mit den Programmen\n'Icecast 1 oder 2' \nim mp3 oder OGG Format\n\nWenn Du einfach immer [Enter] drückst, wird dieses\n\n[Control]-C.\n"
M02=" Konfigurationsdatei gefunden "
M03="Es wurde eine Konfigurationsdatei gefunden,\nsollen wir diese benutzen?"
M04="Bitte Server auswählen"
M05="Bitte Serverdaten eingeben"
M06="Server:"
M07="Port:"
M08u="User Passwort:"
M08a="AdminPasswort:"
M09="Bitte Bitrate angeben"
M10="Bitte Modus angeben"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Bitte Mountpoint angeben"
M15="stream.ogg"
M16="Bitte Streambeschreibung angeben"
M17="StreamBOX Live"
M18=" Zusammenfassung "
M19="Die resultierende Adresse lautet:"
M20=" Programmwahl "
M21="Bitte Programm auswählen:"
B16="16Kbit - Radio, Sprache - sehr schlecht"
B24="24Kbit - Radio, Sprache - schlecht"
B32="32Kbit - Radio, Sprache"
B48="48Kbit - Radio, Sprache"
B56="56Kbit - Radio, bedingt Musik"
B64="64kbit - Radio, Musik"
B96="96kbit - gute Qualität"
B128="128kbit - CD-Qualität"
;;
*)
M01="This is a simple script to stream with Icecast 1 or 2 \nin mp3 or OGG format\nTo stop the streaming use the keystroke [Strg]-C.\n\n"
M02=" Configfile found "
M03="A configuration file was found,\n should we use it?"
M04="Please choose server"
M05="Please enter the servercoordinates"
M06="Server:"
M07="Port:"
M08u="User Password:"
M08a="AdminPassword:"
M09="Please choose bitrate"
M10="Please choose mode"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Please enter mountpoint"
M15="stream.ogg"
M16="Please enter streamdescription"
M17="StreamBOX Live"
M18=" summary "
M19="The resulting address is:"
M20=" choose program : "
M21="Please choose the program:"
B16="16Kbit - radio, voice - very bad"
B24="24Kbit - radio, voice - bad"
B32="32Kbit - radio, voice"
B48="48Kbit - radio, voice"
B56="56Kbit - radio, music"
B64="64kbit - radio, music"
B96="96kbit - fair quality"
B128="128kbit - CD-quality"
;;
esac
##############
# check if configfile is present
if [ ! -f $CONFFILE ]; then
	echo "server=127.0.0.1= IP_adresse of the streaming server
port=8000= Port for the streaming 
passworda=hackme= Admin_Password
passwordr=hackme= Relay_Password
passwordu=hackme= User_Password
">$CONFFILE
fi
CONF=`awk '{ print $0 "\r" }' $CONFFILE`
gxmessage -title "$title" -button OK,Simple,XML,Stop-all,Status,Exit $CONF
RET=$?
if [ $RET = 106 ];then
	exit 0
fi
if [ $RET = 105 ];then
	zenity --warning --text "administration console on http://127.0.0.1:$port/admin/
with user=admin password=$passworda "
	exit 0
fi
if [ $RET = 104 ];then
	killall -9 icecast
	su root -c 'rxvt -e "killall -9 icecast"'
	zenity --warning --text "kill icecast server ok"
	exit 0
fi
if [ $RET = 102 ];then
	gnp $CONFFILE
fi
if [ $RET = 103 ];then
	CONFFILE=$HOME/icecast.xml
	if [ ! -f $CONFFILE ] ;then
		cp /etc/icecast.xml $HOME
	fi
	gnp $CONFFILE
	su root -c "/usr/bin/icecast -b -c $CONFFILE 2>&1 > /tmp/icecast2.$$ &"
	passwords=`grep password $CONFFILE`
	zenity --warning --text "administration console on http://127.0.0.1:$port/admin/ $passwords "
fi

server=`cat $CONFFILE | grep "server" | awk -F"=" {'print $2;'};`
port=`cat $CONFFILE | grep "port" | awk -F"=" {'print $2;'};`
port1=$(($port+1))
passworda=`cat $CONFFILE | grep "passworda" | awk -F"=" {'print $2;'};`
passwordr=`cat $CONFFILE | grep "passwordr" | awk -F"=" {'print $2;'};`
passwordu=`cat $CONFFILE | grep "passwordu" | awk -F"=" {'print $2;'};`
###############

echo "server="$server"= IP_adresse of the streaming server
port="$port"= Port for the streaming
passworda="$passworda"= Admin_Password
passwordr="$passwordr"= Relay_Password
passwordu="$passwordu"= User_Password
">/tmp/popo
#">$CONFFILE
#################
tempfile=/tmp/icecast.xml
cat << ICECAST2EOF > $tempfile

<icecast>
	<location>Not Currently Used</location>
	<admin>Not Currently Used</admin>

	<limits>
		<clients>100</clients>
		<sources>5</sources>
		<threadpool>5</threadpool>
        	<queue-size>102400</queue-size>
		<client-timeout>30</client-timeout>
		<header-timeout>15</header-timeout>
		<source-timeout>10</source-timeout>
		<!-- If enabled, this will provide a burst of data when a client
			first connects, thereby significantly reducing the startup
			time for listeners that do substantial buffering. However,
			it also significantly increases latency between the source
			client and listening client.  For low-latency setups, you
			might want to disable this. -->
		<burst-on-connect>1</burst-on-connect>
		<!-- same as burst-on-connect, but this allows for being more
			specific on how much to burst. Most people won't need to
			change from the default 64k. Applies to all mountpoints  -->
		<burst-size>65535</burst-size>
	</limits>

    <authentication>
        <!-- Sources log in with username 'source' -->
    	<source-password>$passwordu</source-password>
        <!-- Relays log in username 'relay' -->
	<relay-password>$passwordr</relay-password>
        <!-- Admin logs in with the username given below -->
        <admin-user>admin</admin-user>
        <admin-password>$passworda</admin-password>
    </authentication>

    <!-- Uncomment this if you want directory listings -->
    <!-- directory>
        <yp-url-timeout>15</yp-url-timeout>
        <yp-url>http://www.oddsock.org/cgi-bin/yp-cgi</yp-url>
    </directory -->

	<!-- This is the hostname other people will use to connect to your server.
	It affects mainly the urls generated by Icecast for playlists and yp
	listings. -->
	<hostname>$server</hostname>

    <!-- You can use these two if you only want a single listener -->
	<!--<port>8000</port> -->
	<!--<bind-address>127.0.0.1</bind-address>-->

    <!-- You may have multiple <listener> elements -->
    <listen-socket>
        <port>$port</port>
        <!-- <bind-address>127.0.0.1</bind-address> -->
    </listen-socket>
    <listen-socket>
        <port>$port1</port>
    </listen-socket>
	<!--<master-server>127.0.0.1</master-server>-->
	<!--<master-server-port>8001</master-server-port>-->
	<!--<master-update-interval>120</master-update-interval>-->
	<!--<master-password>hackme</master-password>-->
<!--
    <relay>
        <server>127.0.0.1</server>
        <port>8001</port>
        <mount>/example.ogg</mount>
        <local-mount>/different.ogg</local-mount>

        <relay-shoutcast-metadata>0</relay-shoutcast-metadata>
    </relay>
    -->
 <!-- Only define a <mount> section if you want to use advanced options,
         like alternative usernames or passwords
    <mount>
        <mount-name>/example-complex.ogg</mount-name>

        <username>othersource</username>
        <password>hackmemore</password>

        <max-listeners>1</max-listeners>
        <dump-file>/tmp/dump-example1.ogg</dump-file>
        <burst-size>65536</burst-size>
        <fallback-mount>/example2.ogg</fallback-mount>
        <authentication type="htpasswd">
                <option name="filename" value="/usr/share/icecast/myauth"/>
                <option name="allow_duplicate_users" value="0"/>
        </authentication>
    </mount>
    -->


	<fileserve>1</fileserve>

	<paths>
		<basedir>/tmp</basedir>

        <!-- Note that if <chroot> is turned on below, these paths must both
             be relative to the new root, not the original root -->
		<logdir>/var/log/icecast</logdir>
		<webroot>/usr/share/icecast/web</webroot>
          -->
        <!-- Aliases: treat requests for 'source' path as being for 'dest' path
             May be made specific to a port or bound address using the "port"
             and "bind-address" attributes.
          -->
	<!-- Aliases: can also be used for simple redirections as well,
             this example will redirect all requests for http://server:port/ to
             the status page
          -->
        <!--
        <alias source="/foo" dest="/bar"/>
          -->
	</paths>

	<logging>
		<accesslog>access.log</accesslog>
		<errorlog>error.log</errorlog>
      	<loglevel>3</loglevel> <!-- 4 Debug, 3 Info, 2 Warn, 1 Error -->
	</logging>

	<security>
		<chroot>0</chroot>
		<changeowner>
			<user>icecast</user>
			<group>icecast</group>
		</changeowner>  
	</security>
</icecast>

ICECAST2EOF

clear
#trap "rm -f $tempfile;exit 1" 0 1 2 5 15
/usr/bin/icecast -b -c $tempfile 2>&1 > /tmp/icecast2.$$ &
zenity --warning --text "administration console on http://127.0.0.1:$port/admin/
with user=admin password=$passworda "

#rm -f $tempfile
#rm -f /tmp/icecast2$$

exit 0
