#!/bin/bash
#
# oggstream - a little script to stream ogg to the net
#
# Copyright (C) 2004 Ilja Gerhardt <ilja@so36.de>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code 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.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##################
# root werden...
[ "`id -u`" != "0" ] && exec sudo "$0" "$@"
##################
# Global settings:
: ${DIALOG=dialog}
BT="OGGStream"
TI=" OGGStream "
CONFFILE=~/oggstream.cfg
##################
# default settings for host, port and password
server="localhost"
port="9000"
password="streambox"
###############
# 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

case "$LANGUAGE" in
de|at|ch)
M01="Dies ist ein einfaches Script um mit den Programmen\n'Ices 2' oder 'Darkice' von der Soundkarte im OGG Format\nins Internet zu streamen.\n\nWenn Du einfach immer [Enter] drückst, wird dieses\nScript einfach über den Rechner an dem Du gerade\nsitzt ins Internet streamen.\n\nDie Adresse ist dann: http://$myip:$port/streambox.ogg\n\nUm das Streaming zu Beenden drücke bitte einfach\n[Control]-C.\n\nGrüsse von\n\nEgocity + ASCII"
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:"
M08="Passwort:"
M09="Bitte Bitrate angeben"
M10="Bitte Modus angeben"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Bitte Mountpoint angeben"
M15="streambox.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 from\nthe line-in jack of your computer directly in the fileformat OGG\nto the internet.\n\nThe script uses either 'Ices 2' or 'darkice'\n\nIf you don't know what to do\njust press [enter] several times and the\nscript tries to stream from localhost\nto the internet.\n\nThe resulting address is\n\nhttp://$myip:$port/streambox.ogg\n\nTo stop the streaming use the keystroke [Strg]-C.\n\ngreetings from \n\nEgocity + ASCII"
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:"
M08="Password:"
M09="Please choose bitrate"
M10="Please choose mode"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Please enter mountpoint"
M15="streambox.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
##############
# print start dialog
$DIALOG --title "$TI" --backtitle "$BT" --cr-wrap --clear --msgbox "$M01" 0 0
###############
# check if configfile is present
value=1

if [ -f $CONFFILE ]; then
        $DIALOG --title "$M02" --clear --yesno "$M03" 0 0
	value=$?
fi
	if [ $value == 0 ]
	then
		CONFIGS=`cat $CONFFILE | awk {'print $1 " "$3" off \\ \n";'}`

		tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
		trap "rm -f $tempfile" 0 1 2 5 15
		$DIALOG --backtitle "$BT" --nocancel --radiolist "$M04" 0 0 0 \
		localhost "localhost" on $CONFIGS 2> $tempfile
		config=`cat $tempfile`
		if [ $config != "localhost" ]; then
		server=`cat $CONFFILE | grep "^$config" | awk {'print $3;'};`
		port=`cat $CONFFILE | grep "^$config" | awk {'print $5;'};`
		password=`cat $CONFFILE | grep "^$config" | awk {'print $7;'};`
		fi
	else
###############
# if no configfile -> ask for serverdata
                exec 3>&1
                value="`$DIALOG --ok-label "Submit" \
                          --backtitle "$BT" \
                          --form "$M05" \
                10 50 0 \
                        "$M06" 1 1 "$server" 1 20 20 0 \
                        "$M07"   2 1 "$port"  2 20 20 0 \
                        "$M08" 3 1 "$password"  3 20 20 0 \
                2>&1 1>&3`"
                returncode=$?
                exec 3>&-
		server=`echo "$value" | head -1`
		port=`echo "$value" | head -2 | tail -1`
		password=`echo "$value" | tail -1`
	fi
##############
# test if server can be reached on defined port
# first: language settings
case "$LANGUAGE" in
de|at|ch)
P01="\Zb\Z1Der angegebene Server $server ist nicht auf dem Port $port zu erreichen - Sollen wir $BT abbrechen?"
;;
*)
P01="\Zb\Z1The server $server cannot be reached on the specified port $port. Should we stop the program?"
;;
esac
# then run a nmap
if (! nmap -p$port $server | grep open)
then
        $DIALOG --colors --title "$TI" --backtitle "$BT" --cr-wrap --clear --yesno "$P01" 0 0
        if [ $? == 0 ]
        then
                exit 1
        fi
fi
##############
# get bitrate
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --backtitle "$BT" --nocancel --radiolist "$M09" 0 0 0 \
16 "$B16" off \
24 "$B24" off \
32 "$B32" off \
48 "$B48" off \
56 "$B56" on \
64 "$B64" off \
96 "$B96" off \
128 "$B128" off 2> $tempfile
bitrate=`cat $tempfile`
##############
# get mode
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --backtitle "$BT" --nocancel --radiolist "$M10" 0 0 3 1 "$M11" off 2 "$M13" on 2> $tempfile
mode=`cat $tempfile`
##############
# get mountpoint
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --backtitle "$BT" --nocancel --inputbox "$M14" 0 0 "$M15" 2> $tempfile
mountpoint=`cat $tempfile`
##############
# get streamname
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --backtitle "$BT" --nocancel --inputbox "$M16" 8 40 "$M17" 2> $tempfile
streamname=`cat $tempfile`
##############
$DIALOG --title "$M18" --no-collapse --backtitle "$BT" --cr-wrap --clear --msgbox "
BITRATE:	$bitrate
MODE:		$mode
MOUNTPOINT:	$mountpoint
STREAMNAME:	$streamname
SERVER:	$server
PORT:		$port
PASSWORD:	XXXXXXXX

$M19
http://$server:$port/$mountpoint
" 0 0
####################
# determine the program to use
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --title "$M20" --backtitle "$BT" --nocancel --radiolist "$M21" 0 0 0 \
ices "Ices 2" on darkice "Darkice" off 2> $tempfile
prog=`cat $tempfile`
##################
# write configfile
tempfile=/tmp/configfile.txt
##################
# if programm = ices
if [ $prog == ices ]; then

case $bitrate in
16)
	quality=1
;;
24)
	quality=2
;;
32)
	quality=3
;;
56)
	quality=4
;;
64)
	quality=5
;;
96)
	quality=6
;;
128)
	quality=7
;;
esac

cat << ICESEOF > $tempfile
<?xml version="1.0"?>
<ices>
	<background>0</background>
	<logpath>/tmp</logpath>
	<logfile>ices.log</logfile>
	<loglevel>4</loglevel>
        <consolelog>1</consolelog> 
	<stream>
		<metadata>
			<name>$streamname</name>
			<genre>Mixed</genre>
			<description>A mixed stream - related to who the computer is using</description>
		</metadata>
		<input>
			<module>oss</module>
			<param name="rate">22050</param>
			<param name="channels">2</param>
			<param name="device">/dev/dsp</param>
			<param name="metadata">1</param>
		</input>
		<instance>
			<hostname>$server</hostname>
			<port>$port</port>
			<password>$password</password>
			<mount>$mountpoint</mount>
            		<yp>0</yp>  
			<encode>  
				<quality>$quality</quality>
				<samplerate>22050</samplerate>
				<channels>$mode</channels>
			</encode>
            <downmix>0</downmix>
            <resample>
                <in-rate>22050</in-rate>
                <out-rate>22050</out-rate>
            </resample>
		</instance>
	</stream>
</ices>

ICESEOF

clear
ices2 $tempfile

################

else

cat << DARKICEEOF > $tempfile
[general]
duration        = 0     
bufferSecs      = 5      
[input]
device          = /dev/dsp
sampleRate      = 22050  
bitsPerSample   = 16      
channel         = $mode
[icecast2-0]
bitrateMode     = abr    
format          = vorbis  
bitrate         = $bitrate
server          = $server
port            = $port
password        = $password
mountPoint      = $mountpoint
name            = $streamname
description     = $streamname
url             = http://egocity.net
genre           = mixed
public          = no

DARKICEEOF

clear
darkice -c $tempfile

fi

