#!/bin/bash
#
# dstream - a little script to record from the soundcard and stream 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.

# Global settings:
: ${DIALOG=dialog}
CONFFILE=~/dstream.cfg

# What is the name how the program was called?
# then set title and backtitle
if (echo $0 | grep tstream)
then
	BT="TStream"
	TI=" TStream "
else
	BT="DStream"
	TI=" DStream "
fi

# default settings for host, port and password
server="localhost"
port="8000"
password="streambox"

# determine the IP-address of the localsystem
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 im MP3 Format ins\nInternet 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:\n\nhttp://$myip:$port/streambox\n\nUm das Streaming zu beenden, drücke bitte einfach\n[Strg]-C.\n\nGrüsse von\n\nEgocity + ASCII\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:"
M08="Passwort:"
M09="Bitte Bitrate angeben"
M10="Bitte Modus angeben"
M11="Mono"
M12="J-Stereo"
M13="Stereo"
M14="Bitte Mountpoint angeben"
M15="streambox"
M16="Bitte Streambeschreibung angeben"
M17="StreamBOX Live"
M18="Bitte Dateiname für Dumpdatei angeben"
M19="/home/streambox/streamdump.mp3"
M20=" Zusammenfassung "
M21="Die resultierende Adresse lautet:"
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"
P02="\Zb\Z1'Rawrec' scheint noch zu laufen - sollen wir $BT abbrechen [Ja], oder soll rawrec getötet werden [Nein]"
;;
*)
M01="This is a simple script to stream from\nthe line-in jack of your computer directly\nto the internet.\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\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"
M16="Please enter streamdescription"
M17="StreamBOX Live"
M18="Please choose filename for dumpfile"
M19="/home/streambox/streamdump.mp3"
M20=" Summary "
M21="The final address for the stream is:"
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"
P02="\Zb\Z1'rawrec' seems to run in background - should we stop $BT [Yes], or should we kill rawrec [No]"
;;
esac 

##############
# print start dialog
$DIALOG --title "$TI" --backtitle "$BT" --cr-wrap --clear --msgbox "$M01" 0 0

###############
# check if config file 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 m "$M11" off j "$M12" on s "$M13" off 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`
##############
# if progname = tstream -> dump
if (echo $0 | grep tstream)
then
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --backtitle "$BT" --nocancel --inputbox "$M18" 8 40 "$M19" 2> $tempfile
filename=`cat $tempfile`
fi
##############
# print summary
$DIALOG --title "$M20" --no-collapse --backtitle "$BT" --cr-wrap --clear --msgbox "
BITRATE:	$bitrate
MODE:		$mode
MOUNTPOINT:	$mountpoint
STREAMNAME:	$streamname
SERVER:	$server
PORT:		$port
PASSWORD:	XXXXXXXX

$M21
http://$server:$port/$mountpoint
" 0 0
####################
# is rawrec still running?! 
if (ps ax | grep rawrec | grep -v grep)
then
        $DIALOG --colors --title "$TI" --backtitle "$BT" --cr-wrap --clear --yesno "$P02" 0 0 
        if [ $? == 0 ]
        then
                exit 1
	else
		killall -9 rawrec
		sleep 1
        fi
fi
####################
# start teestream if the programm was called tstream
if (echo $0 | grep tstream)
then
	/usr/local/bin/teestream "$bitrate" "$mode" "$mountpoint" "$streamname" "$server" "$port" "$password" "$filename"
else
	/usr/local/bin/livestream "$bitrate" "$mode" "$mountpoint" "$streamname" "$server" "$port" "$password"
fi
# end
