#!/bin/bash

# Install script for the Darwin Streaming Server
# source release

DSSROOT=/usr/local/DarwinStreamingServer-5.5.5-dram2

echo;echo Installing Darwin Streaming Server;echo

INSTALL_OS=`uname`

if [ $INSTALL_OS != "SunOS" ]; then
	USERID=`id -u`
else
	USERID=`/usr/xpg4/bin/id -u`
fi

if [ $USERID = 0 ]; then

	echo Checking for and Killing currently running Darwin Streaming Server
	if [ `uname` = "FreeBSD" ]; then
		ps -ax | awk '/DarwinStreamingServer/ {print $1}' | xargs kill -9  
		ps -ax | awk '/streamingadminserver.pl/ {print $1}' | xargs kill -9  
	fi

	if [ `uname` = "Linux" ]; then
		ps -ax | awk '{print $1" " $5}' | awk '/DarwinStreamingServer/ {print $1}' | xargs -r kill -9  
		ps -ax | awk '/streamingadminserver.pl/ {print $1}' | xargs -r kill -9  
	fi

	if [ `uname` = "SunOS" -o `uname` = "IRIX" -o `uname` = "IRIX64" ]; then
		ps -aef | awk '/DarwinStreamingServer/ {print $2}' | xargs -n 2 kill -9  
		ps -aef | awk '/streamingadminserver.pl/ {print $2}' | xargs -n 1 kill -9 
	fi

## REMOVED OLD VERSION ##

	echo Removing previous versions of Darwin Streaming Server
	if [ -f $DSSROOT/sbin/DarwinStreamingServer ]; then
		echo removing $DSSROOT/sbin/DarwinStreamingServer
		rm -f $DSSROOT/sbin/DarwinStreamingServer 
	fi
	
	if [ -f $DSSROOT/bin/PlaylistBroadcaster ]; then
		echo removing $DSSROOT/bin/PlaylistBroadcaster
		rm -f $DSSROOT/bin/PlaylistBroadcaster
	fi
	
	if [ -f $DSSROOT/bin/MP3Broadcaster ]; then
		echo removing $DSSROOT/bin/MP3Broadcaster
		rm -f $DSSROOT/bin/MP3Broadcaster
	fi
	
	if [ -f $DSSROOT/bin/qtpasswd ]; then
		echo removing $DSSROOT/bin/qtpasswd
		rm -f $DSSROOT/bin/qtpasswd
	fi

	if [ -f $DSSROOT/sbin/streamingadminserver.pl ]; then
		echo removing $DSSROOT/sbin/streamingadminserver.pl
		rm -f $DSSROOT/sbin/streamingadminserver.pl
	fi

	if [ -f $DSSROOT/bin/streamingadminserver.pl ]; then
		echo removing /usr/darwin-5.5/local/bin/streamingadminserver.pl
		rm -f $DSSROOT/bin/streamingadminserver.pl
	fi

	if [ -f $DSSROOT/bin/SpamPro ]; then
		echo removing $DSSROOT/bin/SpamPro
		rm -f $DSSROOT/bin/SpamPro
	fi

if [ -e StreamingLoadTool ]; then
	if [ -f $DSSROOT/bin/StreamingLoadTool ]; then
		echo removing $DSSROOT/bin/StreamingLoadTool
		rm -f $DSSROOT/bin/StreamingLoadTool
	fi
fi
	echo
	
## BACKUP OLD CONFIG FILES ##
	echo Backing up previous config files

	if [ -f $DSSROOT/etc/streamingserver.xml ]; then
		echo backing up $DSSROOT/etc/streamingserver.xml to $DSSROOT/etc/streamingserver.xml.backup
		mv $DSSROOT/etc/streamingserver.xml $DSSROOT/etc/streamingserver.xml.backup
	fi

	if [ -f $DSSROOT/etc/streamingadminserver.pem ]; then
		echo backing up $DSSROOT/etc/streamingadminserver.pem to $DSSROOT/etc/streamingadminserver.pem.backup
		mv $DSSROOT/etc/streamingadminserver.pem $DSSROOT/etc/streamingadminserver.pem.backup
	fi
	
	if [ -f $DSSROOT/etc/qtusers ]; then
		echo backing up $DSSROOT/etc/qtusers to $DSSROOT/etc/qtusers.backup
		mv $DSSROOT/etc/qtusers $DSSROOT/etc/qtusers.backup
	fi

	if [ -f $DSSROOT/etc/qtgroups ]; then
		echo backing up $DSSROOT/etc/qtgroups to $DSSROOT/etc/qtgroups.backup
		mv $DSSROOT/etc/qtgroups $DSSROOT/etc/qtgroups.backup
	fi
	
if [ -e StreamingLoadTool ]; then
	if [ -f $DSSROOT/etc/streamingloadtool.conf ]; then
		echo backing up $DSSROOT/etc/streamingloadtool.conf to $DSSROOT/etc/streamingloadtool.conf.backup
		mv $DSSROOT/etc/streamingloadtool.conf $DSSROOT/etc/streamingloadtool.conf.backup
	fi
fi
	if [ -f $DSSROOT/etc/relayconfig.xml ]; then
		echo backing up $DSSROOT/etc/relayconfig.xml to $DSSROOT/etc/relayconfig.xml.backup
		mv $DSSROOT/etc/relayconfig.xml $DSSROOT/etc/relayconfig.xml.backup
	fi

	echo

## CHANGE PERL PATH IN streamingadminserver.pl AND parse_xml.cgi ##

# Look for perl in the default locations
if [ -x /usr/freeware/bin/perl ]; then
	perldef=/usr/freeware/bin/perl
elif [ -x /usr/local/bin/perl ]; then
	perldef=/usr/local/bin/perl
elif [ -x /usr/bin/perl ]; then
	perldef=/usr/bin/perl
else
	perldef=""
fi

# Test if it is really perl
$perldef -e 'print "foobar\n"' 2>/dev/null | grep foobar >/dev/null

# if it isn't really perl
if [ $? != "0" ]; then
	# prompt the user to enter the path to perl
	if [ "$perl" = "" ]; then
		if [ "$perldef" = "" ]; then
			printf "Full path to perl: "
			read perl
			if [ "$perl" = "" ]; then
				echo "ERROR: No path entered!"
    	   		echo ""                                
                	exit 4
        	fi
        else
			printf "Full path to perl (default $perldef): "
            read perl
    		if [ "$perl" = "" ]; then
                	perl=$perldef
            fi
        fi
	fi
	echo ""

	# Test perl
	echo "Testing Perl ..."
	if [ ! -x $perl ]; then
		echo "ERROR: Failed to find perl at $perl"
		echo ""
		exit 1
	fi

	$perl -e 'print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
	if [ $? != "0" ]; then
		echo "ERROR: Failed to run test perl script. Maybe $perl is"
		echo "not the perl interpreter, or is not installed properly"
		echo ""
		exit 1
	fi

	$perl -e 'exit ($] < 5.002 ? 1 : 0)'
	if [ $? = "1" ]; then
		echo "ERROR: Detected old perl version. The streaming server admin requires"
		echo "perl 5.002 or better to run"
		echo ""
		exit 1
	fi

else
	if [ "$perl" = "" ]; then
    	perl=$perldef
	fi
fi

pushd "WebAdmin"
if [ "$noperlpath" = "" ]; then
	echo "Inserting path to perl into scripts.."
	$perl perlpath.pl $perl src/streamingadminserver.pl AdminHtml/parse_xml.cgi 
    echo ""
fi
popd

# Add the unprivileged user qtss as the server's run user
	echo "Creating unprivileged user to run the server = \"qtss\"."
    if [ $INSTALL_OS = "Linux" ]; then
        /usr/sbin/useradd -M qtss > /dev/null 2>&1
    else
        /usr/sbin/useradd qtss > /dev/null 2>&1
    fi
	
## INSTALL NEW VERSION ##
    if [ ! -d $DSSROOT/sbin ]; then
            echo creating "$DSSROOT/sbin" directory
            mkdir -p $DSSROOT/sbin
    fi              
    if [ ! -d $DSSROOT/bin ]; then
            echo creating "$DSSROOT/bin" directory
            mkdir -p $DSSROOT/bin
    fi              

	echo copying "DarwinStreamingServer" to "$DSSROOT/sbin/DarwinStreamingServer"
	cp -f DarwinStreamingServer $DSSROOT/sbin/
	
	pushd "PlaylistBroadcaster.tproj"
	echo copying "PlaylistBroadcaster" to "$DSSROOT/bin/PlaylistBroadcaster"
	cp -f PlaylistBroadcaster $DSSROOT/bin/
	popd
	
	pushd "MP3Broadcaster"
	echo copying "MP3Broadcaster" to "$DSSROOT/bin/MP3Broadcaster"
	cp -f MP3Broadcaster $DSSROOT/bin/
	popd
	
	pushd "qtpasswd.tproj"
	echo copying "qtpasswd" to "$DSSROOT/bin/qtpasswd"
	cp -f qtpasswd $DSSROOT/bin/
	popd

	# For now, do not copy modules as there are no supported dynamic modules
	# echo copying modules to "$DSSROOT/sbin/StreamingServerModules"
	if [ ! -d $DSSROOT/sbin/StreamingServerModules ]; then
		echo creating "$DSSROOT/sbin/StreamingServerModules" directory
		mkdir $DSSROOT/sbin/StreamingServerModules
	fi
	if [ -d StreamingServerModules ]; then
		cp -f StreamingServerModules/* $DSSROOT/sbin/StreamingServerModules/
	fi
	
	if [ -e $DSSROOT/sbin/StreamingServerModules/QTSSHomeDirectoryModule ]; then
		echo copying "createuserstreamingdir" to "$DSSROOT/bin/createuserstreamingdir"
		cp -f createuserstreamingdir $INSTALLROOT$DSSROOT/bin/
	fi
	
	if [ ! -d $DSSROOT/etc ]; then
		echo creating "$DSSROOT/etc" directory
		mkdir -p $DSSROOT/etc
	fi
	
	if [ ! -e $DSSROOT/etc/streamingserver.xml ]; then
		$DSSROOT/sbin/DarwinStreamingServer -x
	fi
	
	chmod 600 $DSSROOT/etc/streamingserver.xml
	chown qtss $DSSROOT/etc/streamingserver.xml
	
	echo;echo copying "relayconfig.xml-Sample" to "$DSSROOT/etc/relayconfig.xml-Sample"
	cp -f relayconfig.xml-Sample $DSSROOT/etc/relayconfig.xml-Sample
	chmod 600 $DSSROOT/etc/relayconfig.xml-Sample
	if [ -e $DSSROOT/etc/relayconfig.xml ]; then
		chown qtss $DSSROOT/etc/relayconfig.xml
	fi
	
	echo;echo copying "qtusers" to "$DSSROOT/etc/qtusers"
	cp -f qtusers $DSSROOT/etc/qtusers
	chmod 600 $DSSROOT/etc/qtusers
	chown qtss $DSSROOT/etc/qtusers

	echo;echo copying "qtgroups" to "$DSSROOT/etc/qtgroups"
	cp -f qtgroups $DSSROOT/etc/qtgroups
	chmod 600 $DSSROOT/etc/qtgroups
	chown qtss $DSSROOT/etc/qtgroups

	echo;echo copying "streamingadminserver.pem" to "$DSSROOT/etc/streamingadminserver.pem"
	pushd "WebAdmin"
	cp -f streamingadminserver.pem $DSSROOT/etc/streamingadminserver.pem
	chmod 400 $DSSROOT/etc/streamingadminserver.pem
	popd

	if [ ! -d /usr/local/logs/darwin ]; then
		echo creating "/usr/local/logs/darwin" directory
		mkdir -p /usr/local/logs/darwin
	fi
	
	pushd "Documentation"
	echo copying "readme.txt" to "$DSSROOT/var/readme.txt"
	cp -f readme.txt $DSSROOT/var/readme.txt
	
	echo copying "3rdPartyAcknowledgements.rtf" to "$DSSROOT/var/3rdPartyAcknowledgements.rtf"
	cp -f 3rdPartyAcknowledgements.rtf $DSSROOT/var/3rdPartyAcknowledgements.rtf
	popd
	
	if [ ! -d /usr/local/logs/darwin ]; then
		echo creating "/usr/local/logs/darwin" directory
		mkdir -p /usr/local/logs/darwin
	fi
	
	if [ ! -d $DSSROOT/movies ]; then
		echo creating "$DSSROOT/movies" directory
		mkdir -p $DSSROOT/movies
	fi
	
	if [ ! -d $DSSROOT/var/playlists ]; then
		echo creating "$DSSROOT/var/playlists" directory
		mkdir -p $DSSROOT/var/playlists
	fi
	chmod 770 $DSSROOT/var/playlists
	chmod 775 $DSSROOT/movies
	
    echo copying "sample_100kbit.mov" into "$DSSROOT/movies/sample_100kbit.mov"
    cp -f sample_100kbit.mov $DSSROOT/movies/

    echo copying "sample_300kbit.mov" into "$DSSROOT/movies/sample_300kbit.mov"
    cp -f sample_300kbit.mov $DSSROOT/movies/

    echo copying "sample_100kbit.mp4" into "$DSSROOT/movies/sample_100kbit.mp4"
    cp -f sample_100kbit.mp4 $DSSROOT/movies/

    echo copying "sample_300kbit.mp4" into "$DSSROOT/movies/sample_300kbit.mp4"
    cp -f sample_300kbit.mp4 $DSSROOT/movies/

    echo copying "sample.mp3" into "$DSSROOT/movies/sample.mp3"
    cp -f sample.mp3 $DSSROOT/movies/

    echo copying "sample_50kbit.3gp" into "$DSSROOT/movies/sample_50kbit.3gp"
    cp -f sample_50kbit.3gp $DSSROOT/movies/

    echo copying "sample_h264_100kbit.mp4" into "$DSSROOT/movies/sample_h264_100kbit.mp4"
    cp -f sample_h264_100kbit.mp4 $DSSROOT/movies/

    echo copying "sample_h264_300kbit.mp4" into "$DSSROOT/movies/sample_h264_300kbit.mp4"
    cp -f sample_h264_300kbit.mp4 $DSSROOT/movies/

    echo copying "sample_h264_1mbit.mp4" into "$DSSROOT/movies/sample_h264_1mbit.mp4"
    cp -f sample_h264_1mbit.mp4 $DSSROOT/movies/

if [ -e StreamingLoadTool ]; then
	echo copying "StreamingLoadTool" to "$DSSROOT/bin/StreamingLoadTool"
	cp -f StreamingLoadTool $DSSROOT/bin/
	
	echo copying "streamingloadtool.conf" to "$DSSROOT/etc/streamingloadtool.conf"
	cp -f streamingloadtool.conf $DSSROOT/etc/
fi
	
	#WebAdmin install
	pushd "WebAdmin/src"
	echo copying "streamingadminserver.pl" into "$DSSROOT/sbin/streamingadminserver.pl"
	cp -f streamingadminserver.pl $DSSROOT/sbin/streamingadminserver.pl
	popd
	
	pushd "WebAdmin"
	if [ -d $DSSROOT/var/AdminHtml/ ]; then
	    echo removing old version of html from "$DSSROOT/var/AdminHtml"
	    rm -r -f $DSSROOT/var/AdminHtml/*
	fi    
	
	if [ ! -d $DSSROOT/var/AdminHtml ]; then
		echo creating "$DSSROOT/var/AdminHtml" directory
		mkdir -p $DSSROOT/var/AdminHtml
	fi
	echo copying Admin HTML to "$DSSROOT/var/AdminHtml" directory
	cp -f -r WebAdminHtml $DSSROOT/var
	popd

	chown -R -f qtss /usr/local/logs/
	chown -R -f qtss $DSSROOT/etc/
	chown -R -f qtss $DSSROOT/movies/

	echo;echo "Launching streamingadminserver.pl"
	$DSSROOT/sbin/streamingadminserver.pl
	
	echo;echo Installation Complete

	# Setup for DSS
	# Prompts for a username and password
	# that can be used to administer the server
	
	echo;echo Darwin Streaming Server Setup;echo
	
	# prompt the user to enter the admin username
	while [ "$username" = "" ]; do
		printf "In order to administer the Darwin Streaming Server you must create an administrator user [Note: The administrator user name cannot contain spaces, or single or double quote characters, and cannot be more than 255 characters long].\n"
		
		printf "Please enter a new administrator user name: "
		read username
		if [ "$username" = "" ]; then
			echo ""
			echo "Error: No username entered!"
			echo ""
		fi
	done
	echo ""
	
	# prompt the user to enter the admin password
	while [ "$password" = "" ]; do
		printf "\nYou must also enter a password for the administrator user [Note: The administrator password cannot contain spaces, or quotes, either single or double, and cannot be more than 80 characters long].\n"

		printf "Please enter a new administrator Password: "
		stty -echo
		read password
		stty echo
		echo ""
		printf "Re-enter the new administrator password: "
		stty -echo
		read password1
		stty echo
		if [ "$password" = "" ]; then
			echo ""
			echo "Error: No password entered!"
			echo ""
		fi
		if [ "$password" != "$password1" ]; then
			echo ""
			echo "Error: passwords entered do not match!"
			echo ""
			password=""
		fi
	
	done
	echo ""
	
	# Add the new admin username to $DSSROOT/etc/qtusers
	$DSSROOT/bin/qtpasswd -p $password $username
	
	# Add the new admin username to $DSSROOT/etc/qtgroups
	# and delete the default admin username
	echo admin: $username > $DSSROOT/etc/qtgroups.tmp
	mv $DSSROOT/etc/qtgroups.tmp $DSSROOT/etc/qtgroups
	
	# Remove the default admin username to $DSSROOT/etc/qtusers
	$DSSROOT/bin/qtpasswd -F -d 'aGFja21l' > /dev/null
		
	chown -R -f qtss $DSSROOT/etc/
	
	echo Setup Complete!
	
else

	echo "Unable to perform install"
	echo "You must be logged in as root to install Darwin Streaming Server";echo
	exit 1
fi
