#!/bin/sh
#
# Install script for the Darwin Streaming Server source/binary release
#
# $Id$
#

# ---------------------------------------------------------
# Copyright (c) 2001 Peter Darren Bray
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the developer nor the names of contributors
#    may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------

#
# Special Note to Developers : Use only Bourne Shell features
#   /bin/sh on Linux is GNU BASH not the original Bourne Shell
#

#
# Configuration - General
#

VERSION=cvs-20011130

ENABLE_NON_ROOT_INSTALL=1

# Never manually uncomment the following line
#AUTO_REMOVED#FORCE_BINARY_MODE=1

#
# Configuration - Debugging and Output Level
#

# Debugging - set DO=echo or DO=: to enable debugging
#DO=echo

# To make the install quieter, uncomment the following line
#QUIETLY=:

#
# Usage
#

Usage()
{
    echo "Usage : ${0} install|package|generateWindowsConfig server|proxy"
    exit 1
}

#
# Configuration - Mode
#

PACKAGING_MODE=Packaging
SOURCE_INSTALL_MODE=SourceInstall
BINARY_INSTALL_MODE=BinaryInstall
WINDOWS_CONFIG_MODE=GenerateWindowsConfiguration

DetermineMode()
{
    # Try to automatically determine what MODE and PRODUCT in binary package 
    # distributions, ie avoids the need for command line arguments altogether
    if [ -n "${FORCE_BINARY_MODE}" ]
    then
	MODE="${BINARY_INSTALL_MODE}"
	MODE_DESCRIPTION="Installing"
        # Note: The following file location is dependent on the binary
        #       release packaging structure - update as required
	if [ -f ./sbin/DarwinStreamingServer ]
	then
	    PRODUCT=Server
	else
	    PRODUCT=Proxy
	fi
    else
	if [ "${#}" -eq 2 ]
	then
	    case "${1}" in
		install)
			MODE="${SOURCE_INSTALL_MODE}"
			MODE_DESCRIPTION="Installing (from Source)"
			;;
		package)
			MODE="${PACKAGING_MODE}"
			MODE_DESCRIPTION="Packaging up"
			;;
		generateWindowsConfig)
			MODE="${WINDOWS_CONFIG_MODE}"
			MODE_DESCRIPTION="Generating Windows Configuration"
			;;
		*)
		    Usage
	    esac

	    case "${2}" in
		proxy)
			PRODUCT=Proxy
			;;
		server)
			PRODUCT=Server
			;;
		*)
			Usage
	    esac
	else
	    Usage
	fi
    fi
}

# ---------------------------------------------------------------------------

#
# Configuration - Platform
#

DeterminePlatform()
{
    #TODO# Fix this
    #TODO# On Solaris this produces SunOS-5.8-i386 or SunOS-5.8-sparc
    #TODO# but what should we have in general ?

    PLATFORM=`uname -srp | sed -e 's/ /-/g'`
}

#
# Configuration - Packaging Area
#

DeterminePackagingArea()
{
    if [ "${MODE}" = "${BINARY_INSTALL_MODE}" ]
    then
	PACKAGING=.
    else
	PACKAGING=./packaging-${PRODUCT}-${PLATFORM}
    fi

    #
    # Note: Support for the old style flattening option, would simply
    #       be a matter of removing the subdirectories in the definitions below
    #       but there is probably little point
    #

    PA_SBIN_DIR=${PACKAGING}/sbin
    PA_SSM_DIR=${PACKAGING}/ssm
    PA_BIN_DIR=${PACKAGING}/bin
    PA_MOV_DIR=${PACKAGING}/movies
    PA_MP3_DIR=${PACKAGING}/mp3s
    PA_ETC_DIR=${PACKAGING}/etc
    PA_DOC_DIR=${PACKAGING}/doc
    PA_VAR_DIR=${PACKAGING}/var

    PA_LOG_DIR=${PA_VAR_DIR}/logs
    PA_ADMIN_HTML_DIR=${PA_VAR_DIR}/AdminHtml
    PA_PLAY_LIST_DIR=${PA_VAR_DIR}/Playlists
}

#
# Configuration - Normal Installation
#

DetermineConfiguration()
{
    #TODO# Fix this - Darwin case

    if	[ "${MODE}" = "${WINDOWS_CONFIG_MODE}" ]
    then
	WindowsConfiguration
    elif false			#TODO# [ "${PLATFORM}" = "?" ]
    then
	DarwinConfiguration
    else
	UnixConfiguration
    fi

    # PrintConfiguration
}

#
# Configuration - Normal Installation - UNIX
#
UnixConfiguration()
{
    PREFIX="/tmp/Streaming${PRODUCT}-${VERSION}"	# Development/Testing
    #PREFIX="/usr/local"
    #PREFIX="/pkgs/Streaming${PRODUCT}-${VERSION}"

    SBIN_DIR="${PREFIX}/sbin"
    SSM_DIR="${PREFIX}/sbin/StreamingServerModules"
    BIN_DIR="${PREFIX}/bin"
    MOV_DIR="${PREFIX}/movies"
    MP3_DIR="${PREFIX}/mp3s"

    ETC_DIR="${PREFIX}/etc"
    DOC_DIR="${PREFIX}/doc"
    VAR_DIR="${PREFIX}/var"
    #ETC_DIR="/etc/streaming"
    #VAR_DIR="/var/streaming"

    LOG_DIR="${VAR_DIR}/logs"

    ADMIN_HTML_DIR="${VAR_DIR}/AdminHtml"
    PLAY_LIST_DIR="${VAR_DIR}/Playlists"

    #TODO# Has this been replaced by relayconfig.xml ? 
    #TODO# If so delete all references to SR_CONFIG and streamingrelay.conf/streamingrelay.cfg
    SR_CONFIG=streamingrelay.conf
    SP_CONFIG=streamingproxy.conf
    SLT_CONFIG=streamingloadtool.conf
    SPRO_CONFIG=spampro.conf
    SAS_CONFIG=streamingadminserver.conf
    SAS_PEM=streamingadminserver.pem

    SS_XML=streamingserver.xml
    RC_XML=relayconfig.xml

    DSS_EXECUTABLE=DarwinStreamingServer
    QTPASSWD_EXECUTABLE=qtpasswd

    PERL_PATH=/usr/bin/perl
}

#
# Configuration - Normal Installation - Darwin / MacOS X
#
DarwinConfiguration()
{
    #TODO# Would the Darwin maintainer put in the definitive paths
    #TODO# various files had differing approaches. I personally would
    #TODO# like to see the Darwin case disappear in favour of a single
    #TODO# approach on UN*X platforms - but consistency has alway been 
    #TODO# a problem for UN*X flavours. :-(

    PREFIX="/tmp/Streaming${PRODUCT}-${VERSION}"	# Development/Testing
    #PREFIX="/usr/local"
    #PREFIX="/pkgs/Streaming${PRODUCT}-${VERSION}"

    SBIN_DIR="${PREFIX}/sbin"
    SSM_DIR="${PREFIX}/sbin/StreamingServerModules"
    BIN_DIR="${PREFIX}/bin"
    MOV_DIR="/Library/QuickTimeStreaming/Movies"
    MP3_DIR="${PREFIX}/mp3s"

    #ETC_DIR="${PREFIX}/etc"
    #VAR_DIR="${PREFIX}/var"
    ETC_DIR="/etc/streaming"
    DOC_DIR="${PREFIX}/doc"
    VAR_DIR="/var/streaming"

    LOG_DIR="${VAR_DIR}/logs"
    #LOG_DIR="/Library/QuickTimeStreaming/Logs"

    ADMIN_HTML_DIR="${VAR_DIR}/AdminHtml"
    #ADMIN_HTML_DIR="/Library/QuickTimeStreaming/AdminHtml"
    PLAY_LIST_DIR="${VAR_DIR}/Playlists"
    #PLAY_LIST_DIR="/Library/QuickTimeStreaming/Playlists"

    #TODO# Has this been replaced by relayconfig.xml ? 
    #TODO# If so delete all references to SR_CONFIG and streamingrelay.conf/streamingrelay.cfg
    SR_CONFIG=streamingrelay.conf
    SP_CONFIG=streamingproxy.conf
    SLT_CONFIG=streamingloadtool.conf
    SPRO_CONFIG=spampro.conf
    SAS_CONFIG=streamingadminserver.conf
    SAS_PEM=streamingadminserver.pem

    SS_XML=streamingserver.xml
    RC_XML=relayconfig.xml

    DSS_EXECUTABLE=DarwinStreamingServer
    QTPASSWD_EXECUTABLE=qtpasswd

    PERL_PATH=/usr/bin/perl
}

#
# Configuration - Normal Installation - Windows
#
WindowsConfiguration()
{
    PREFIX="C:/Program Files/Darwin Streaming ${PRODUCT}"

    SBIN_DIR="${PREFIX}"
    SSM_DIR="${PREFIX}/StreamingServerModules"
    BIN_DIR="${PREFIX}"
    MOV_DIR="${PREFIX}/Movies"
    MP3_DIR="${PREFIX}/MP3s"

    ETC_DIR="${PREFIX}"
    DOC_DIR="${PREFIX}"
    VAR_DIR="${PREFIX}"

    LOG_DIR="${VAR_DIR}/Logs"

    ADMIN_HTML_DIR="${VAR_DIR}/AdminHtml"
    PLAY_LIST_DIR="${VAR_DIR}/Playlists"

    #TODO# Has this been replaced by relayconfig.xml ? 
    #TODO# If so delete all references to SR_CONFIG and streamingrelay.conf/streamingrelay.cfg
    SR_CONFIG=streamingrelay.cfg
    SP_CONFIG=streamingproxy.cfg
    SLT_CONFIG=streamingloadtool.cfg
    SPRO_CONFIG=spampro.cfg
    SAS_CONFIG=streamingadminserver.cfg
    SAS_PEM=streamingadminserver.pem

    SS_XML=streamingserver.xml
    RC_XML=relayconfig.xml

    DSS_EXECUTABLE=DarwinStreamingServer.exe
    QTPASSWD_EXECUTABLE=qtpasswd.exe

    PERL_PATH=Unknown
}

#
# Configuration - Debugging Aid
#
PrintConfiguration()
{
    echo "PREFIX                  = ${PREFIX}"
    echo "SBIN_DIR                = ${SBIN_DIR}"
    echo "SSM_DIR                 = ${SSM_DIR}"
    echo "BIN_DIR                 = ${BIN_DIR}"
    echo "MOV_DIR                 = ${MOV_DIR}"
    echo "MP3_DIR                 = ${MP3_DIR}"
    echo "ETC_DIR                 = ${ETC_DIR}"
    echo "DOC_DIR                 = ${DOC_DIR}"
    echo "VAR_DIR                 = ${VAR_DIR}"
    echo "LOG_DIR                 = ${LOG_DIR}"
    echo "ADMIN_HTML_DIR          = ${ADMIN_HTML_DIR}"
    echo "PLAY_LIST_DIR           = ${PLAY_LIST_DIR}"
    echo "SR_CONFIG               = ${SR_CONFIG}"
    echo "SP_CONFIG               = ${SP_CONFIG}"
    echo "SLT_CONFIG              = ${SLT_CONFIG}"
    echo "SPRO_CONFIG             = ${SPRO_CONFIG}"
    echo "SAS_CONFIG              = ${SAS_CONFIG}"
    echo "SAS_PEM                 = ${SAS_PEM}"
    echo "SS_XML                  = ${SS_XML}"
    echo "RC_XML                  = ${RC_XML}"
    echo "DSS_EXECUTABLE          = ${DSS_EXECUTABLE}"
    echo "QTPASSWD_EXECUTABLE     = ${QTPASSWD_EXECUTABLE}"
    echo "PERL_PATH               = ${PERL_PATH}"
}

# ---------------------------------------------------------------------------

EnsureUserIsRoot()
{
    if [ "${ENABLE_NON_ROOT_INSTALL}" = 1 ]
    then
	return
    fi

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

    if [ ${USERID} != 0 ]
    then
	echo "Unable to perform install"
	echo
	echo "You must be logged in as root to install Darwin Streaming ${PRODUCT}"
	echo
	exit 1
    fi
}

ValidatePerlPath()
{
    # Allow run-time override
    if [ -n "${PERL}" -a -x "${PERL}" ]
    then
        PERL_PATH="${PERL}"
    fi

    if [ -x "${PERL_PATH}" ]
    then
        : No actions required
    elif [ -x /usr/local/bin/perl ]
    then
        PERL_PATH=/usr/local/bin/perl
    elif [ -x /usr/bin/perl ]
    then
        PERL_PATH=/usr/bin/perl
    else
        echo " - *PROBLEM* can not find the perl executable" 1>&2
        echo " - *PROBLEM* Edit this script OR run with PERL set to the path of perl"  1>&2
        echo " - *PROBLEM* example: PERL=/pkgs/bin/perl ${0} ${*}"
        exit 1
    fi

    echo
    echo "Perl is ${PERL_PATH}"
    echo
}

ValidatePerlVersion()
{
    ${PERL_PATH} -e 'exit ( $] < 5.002 ? 1 : 0 )'

    if [ $? -eq 1 ]
    then
        echo " - *PROBLEM* Detected an old version of perl" 1>&2
        echo " - *PROBLEM*   ${PERL_PATH} is" `${PERL_PATH} -e 'print "perl $]"'` 1>&2
        echo " - *PROBLEM* The Streaming Server Web Admin Interface requires"  1>&2
        echo " - *PROBLEM*   perl 5.002 or better to run"
        echo " - *PROBLEM* Edit this script OR run with PERL set to the path of perl"  1>&2
        echo " - *PROBLEM*   example: PERL=/pkgs/bin/perl ${0} ${*}"
        exit 1
    fi
}

# ---------------------------------------------------------------------------

#
# Mode Independent Functions
#

RemoveFile()
{
    if [ -f "${1}" ]
    then
	echo " - removing ${1}"
	${DO} rm -f "${1}"
    fi
}

RemoveDirectory()
{
    if [ -d "${1}" ]
    then
	echo " - removing directory ${1}"
	${DO} rm -rf "${1}"
    fi
}

CopyTree()
{
    if [ -d "${1}" ]
    then
	${QUIETLY} echo " - copying directory tree ${1} to ${2}"
	${DO} cp -f -r "${1}" "${2}"
    else
	echo " - *PROBLEM* can not install ${1} : Missing Directory" 1>&2
    fi
}

GenerateFromTemplate()
{
    template="${1}"
    generated="${2}"

    if [ -f "${template}" ]
    then
	${QUIETLY} echo " - substituting in ${template}"
	${QUIETLY} echo "   and generating  ${generated}"

	# Debugging Support
	if [ -n "${DO}" ]
	then
	    generated=/dev/null
	fi

	sed								\
	    -e "s|@@SBIN_DIR@@|${SBIN_DIR}|g"				\
	    -e "s|@@SSM_DIR@@|${SSM_DIR}|g"				\
	    -e "s|@@BIN_DIR@@|${BIN_DIR}|g"				\
	    -e "s|@@MOV_DIR@@|${MOV_DIR}|g"				\
	    -e "s|@@MP3_DIR@@|${MP3_DIR}|g"				\
	    -e "s|@@ETC_DIR@@|${ETC_DIR}|g"				\
	    -e "s|@@DOC_DIR@@|${DOC_DIR}|g"				\
	    -e "s|@@VAR_DIR@@|${VAR_DIR}|g"				\
	    -e "s|@@LOG_DIR@@|${LOG_DIR}|g"				\
	    -e "s|@@ADMIN_HTML_DIR@@|${ADMIN_HTML_DIR}|g"		\
	    -e "s|@@PLAY_LIST_DIR@@|${PLAY_LIST_DIR}|g"			\
	    -e "s|@@SR_CONFIG@@|${SR_CONFIG}|g"				\
	    -e "s|@@SP_CONFIG@@|${SP_CONFIG}|g"				\
	    -e "s|@@SLT_CONFIG@@|${SLT_CONFIG}|g"			\
	    -e "s|@@SPRO_CONFIG@@|${SPRO_CONFIG}|g"			\
	    -e "s|@@SAS_CONFIG@@|${SAS_CONFIG}|g"			\
	    -e "s|@@SAS_PEM@@|${SAS_PEM}|g"				\
	    -e "s|@@SS_XML@@|${SS_XML}|g"				\
	    -e "s|@@RC_XML@@|${RC_XML}|g"				\
	    -e "s|@@DSS_EXECUTABLE@@|${DSS_EXECUTABLE}|g"		\
	    -e "s|@@QTPASSWD_EXECUTABLE@@|${QTPASSWD_EXECUTABLE}|g"	\
	    -e "s|@@PERL_PATH@@|${PERL_PATH}|g"				\
	    "${template}" > "${generated}"
	else
	    echo " * PROBLEM: can not generate ${generated}" 1>&2
	    echo "            as ${template} : Missing"      1>&2
	fi
}

GenerateFromTemplateForWindows()
{
    template="${1}"
    generated="${2}"

    GenerateFromTemplate "${template}" "${generated}"

    #TODO# unix2dos conversion on generated windows configuration files 
    #TODO# On Solaris unix2dos is fine, but sed is probably more portable
}

UpdatePerlPathIn()
{
    # Arguments a list of files to update the #! line
    ${QUIETLY} echo " - In-place editing ${@}"
    PERL_PATH=${PERL_PATH} ${DO} ${PERL_PATH} -pi	\
        -e  'if ( m/^#!(\/\S*perl\S*)/ ) { s/$1/$ENV{PERL_PATH}/; }' "${@}"
}

# ---------------------------------------------------------------------------

#
# Mode Dependant Functions
#

MakeDirectory()
{
    mode="${1}"
    packaging_directory="${2}"
    installation_directory="${3}"

    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
	directory="${packaging_directory}"
    else
	directory="${installation_directory}"
    fi

    if [ ! -d "${directory}" ]
    then
	${QUIETLY} echo " - creating directory ${directory}"
	${DO} mkdir -p	      "${directory}"
	${DO} chmod "${mode}" "${directory}"
    fi
}

InstallFile()
{
    mode="${1}"
    source_location="${2}"
    packaging_location="${3}"
    installation_location="${4}"

    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
	source="${source_location}"
	destination="${packaging_location}"
    elif [ "${MODE}" = "${SOURCE_INSTALL_MODE}" ]
    then
	source="${source_location}"
	destination="${installation_location}"
    else
	source="${packaging_location}"
	destination="${installation_location}"
    fi

    if [ -f "${source}" ]
    then
	${QUIETLY} echo " - installing ${source}"
        ${QUIETLY} echo "           as ${destination}"
	${DO} cp -f "${source}" "${destination}"
	${DO} chmod "${mode}"	"${destination}"
    else
	echo " * PROBLEM: can not install ${source} : Missing" 1>&2
    fi
}

InstallExecutable()
{
    InstallFile 755 "${@}"
}

InstallDataFile()
{
    InstallFile 444 "${@}"
}

ReportMissingOptionalFile()
{
    echo " * Missing Optional File : ${1} - Skipping" 1>&2
}

InstallConfigFileSafely()
{
    #TODO# Fix Mode Handling
    mode=600
    installed_location="${1}"
    actual_location="${2}"


    if [ -f "${installed_location}" ]
    then
	if [ ! -f "${actual_location}" ]
	then
	    ${QUIETLY} echo " - installing ${installed_location}"
            ${QUIETLY} echo "           as ${actual_location}"
	    ${DO} cp	"${installed_location}"	"${actual_location}"
	    ${DO} chmod	${mode}			"${actual_location}"
	else
	    echo " * Please manually merge your existing configuration with the current sample" 
	    echo "    - Existing : ${actual_location}"
	    echo "    - Sample   : ${installed_location}"
	fi
    else
	echo " * PROBLEM: can not install ${installed_location} : Missing" 1>&2
    fi
}

InstallConfigFile()
{
    source_location="${1}"
    packaging_location="${2}"
    installation_location="${3}"
    actual_location="${4}"

    InstallFile 644 "${@}"

    if [ "${MODE}" != "${PACKAGING_MODE}" ]
    then
	InstallConfigFileSafely "${installation_location}" "${actual_location}"
    fi
}

# ---------------------------------------------------------------------------

InstallGeneratedFile()
{
    #TODO# Fix Mode Handling
    mode=600
    source_location="${1}"
    packaging_location="${2}"
    installation_location="${3}"
    actual_location="${4}"

    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
	source="${source_location}"
	destination="${packaging_location}"
    elif [ "${MODE}" = "${SOURCE_INSTALL_MODE}" ]
    then
	source="${source_location}"
	destination="${installation_location}"
    else
	source="${packaging_location}"
	destination="${installation_location}"
    fi

    if [ -f "${source}" ]
    then
	${QUIETLY} echo " - installing ${source}"
        ${QUIETLY} echo "           as ${destination}"
	${DO} cp -f "${source}" "${destination}"
	${DO} chmod "${mode}"	"${destination}"
    else
	echo " * PROBLEM: can not install ${source} : Missing" 1>&2
    fi

    if [ "${MODE}" != "${PACKAGING_MODE}" ]
    then
	GenerateFromTemplate	"${source}"	 "${destination}"
	InstallConfigFileSafely	"${destination}" "${actual_location}"
    else
	${DO} cp "${source}" "${destination}"
    fi
}

# ---------------------------------------------------------------------------

AllMatching()
{
    wildcard_expression="${1}"
    installation_routine="${2}"
    source_directory="${3}"
    packaging_directory="${4}"
    installation_directory="${5}"

    # OK - so I'm really hacking here - ode to global variables :-(
    QUIETLY_ORIG_ORIG="${QUIETLY_ORIG}"
    QUIETLY_ORIG="${QUIETLY}"
    QUIETLY=":"

    if [ "${MODE}" != "${BINARY_INSTALL_MODE}" ]
    then
	directory="${source_directory}"
    else
	directory="${packaging_directory}"
    fi

    for source_filename in ${directory}/${wildcard_expression}
    do
	file="`basename ${source_filename}`"
	${installation_routine}	"${source_directory}/${file}"		\
				"${packaging_directory}/${file}"	\
				"${installation_directory}/${file}"
    done

    QUIETLY="${QUIETLY_ORIG}"
    QUIETLY_ORIG="${QUIETLY_ORIG_ORIG}"
}

# ---------------------------------------------------------------------------

InstallWebAdminLanguage()
{
    language=${1}
    language_directory="html_${language}"

    source_dir="WebAdmin/WebAdminHtml/${language_directory}"
    pa_lang_dir="${PA_ADMIN_HTML_DIR}/${language_directory}"
    lang_dir="${ADMIN_HTML_DIR}/${language_directory}"

    echo
    echo "Installing WebAdmin Language - ${language}..."
    echo

    QUIETLY_ORIG="${QUIETLY}"
    QUIETLY=":"

    #
    # WebAdmin Language Directory Structure
    #

    MakeDirectory 755 ${pa_lang_dir}		${lang_dir}

    #
    # WebAdmin Language Specific Base Files
    #
    AllMatching	\*.html							\
		InstallDataFile		${source_dir}			\
					${pa_lang_dir}			\
					${lang_dir}

    InstallDataFile	${source_dir}/messages				\
			${pa_lang_dir}/messages				\
			${lang_dir}/messages

    InstallDataFile	${source_dir}/genres				\
			${pa_lang_dir}/genres				\
			${lang_dir}/genres

    QUIETLY="${QUIETLY_ORIG}"
}

# ---------------------------------------------------------------------------

InstallInstallationScript()
{
    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
	echo
	echo "Installing Installation Script..."
	echo

	source=Install
	destination=${PACKAGING}/Install

	if [ -f "${source}" ]
	then
	    ${QUIETLY} echo " - installing ${source}"
            ${QUIETLY} echo "           as ${destination}"

	    # Debugging Support
	    if [ -z "${DO}" ]
	    then
		${DO} sed -e 's/^#AUTO_REMOVED#//' "${source}" > "${destination}"
	    fi
	    ${DO} chmod 755 "${destination}"
	else
	    echo " * PROBLEM: can not install ${source} : Missing" 1>&2
	fi
    fi
}

# ---------------------------------------------------------------------------

ClearPlatformArchive()
{
    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
        echo
        echo "Clearing ${PLATFORM} archive of the Darwin Streaming ${PRODUCT}..."
        echo

        ${DO} rm -rf ${PACKAGING}
        ${DO} rm -f  ${PACKAGING}.tar ${PACKAGING}.tar.gz
    fi
    
}

CreatePlatformArchive()
{
    if [ "${MODE}" = "${PACKAGING_MODE}" ]
    then
        echo
        echo "Building ${PLATFORM} archive of the Darwin Streaming ${PRODUCT}..."
        echo

        ${DO} tar  -fc ${PACKAGING}.tar ${PACKAGING}
        ${DO} gzip -9v ${PACKAGING}.tar
    fi
}

# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------

#
# Main Program
#

DetermineMode "${@}"
DeterminePlatform
DeterminePackagingArea
DetermineConfiguration

# ---------------------------------------------------------------------------

#
# Windows Configuration File Generation from Templates Section
#

if [ "${MODE}" = "${WINDOWS_CONFIG_MODE}" ]
then
    echo
    echo "Generating Windows Configuration Files from the Templates..."
    echo

    #PrintConfiguration

    if [ "${PRODUCT}" = "Server" ]
    then

        GenerateFromTemplateForWindows				\
            streamingserver.xml-template			\
            WinNTSupport/${SS_XML}

        GenerateFromTemplateForWindows				\
            relayconfig.xml-template				\
            WinNTSupport/${RC_XML}

        GenerateFromTemplateForWindows				\
            WebAdmin/streamingadminserver.conf-template		\
            WinNTSupport/${SAS_CONFIG}

        #TODO# Has this been replaced by relayconfig.xml ? 
        #TODO# If so delete all references to SR_CONFIG and streamingrelay.conf/streamingrelay.cfg
        GenerateFromTemplateForWindows				\
            streamingrelay.conf					\
            WinNTSupport/${SR_CONFIG}

        GenerateFromTemplateForWindows				\
            qtusers						\
            WinNTSupport/qtusers

        GenerateFromTemplateForWindows				\
            qtgroups						\
            WinNTSupport/qtgroups

        GenerateFromTemplateForWindows				\
            qtaccess-template					\
            WinNTSupport/qtaccess

    else

        GenerateFromTemplateForWindows				\
            StreamingProxy.tproj/streamingproxy.conf		\
            WinNTSupport/${SP_CONFIG}

    fi

    echo
    echo "Generation for Darwin Streaming ${PRODUCT} Complete"
    echo

    exit
fi

# ---------------------------------------------------------------------------

#TODO# The BuildIt and StreamingProxy.tproj/BuildProxy should be made to
#TODO# function on all UN*X platforms including Darwin, that way the 
#TODO# build process becomes quite simple.
#TODO# cvs update and cleaning need handling

#TODO#echo
#TODO#echo "Building Darwin Streaming ${PRODUCT}"
#TODO#echo
#TODO#
#TODO#then
#TODO#    ./BuildIt
#TODO#else
#TODO#    ( cd StreamingProxy.tproj; ./BuildProxy )
#TODO#fi
#TODO#
#TODO#if [ $? != 0 ]
#TODO#then
#TODO#    echo " * PROBLEM : Failed to build the Darwin Streaming ${PRODUCT} - Aborting" 1>&2
#TODO#    exit 1
#TODO#fi

# ---------------------------------------------------------------------------

echo
echo "${MODE_DESCRIPTION} Darwin Streaming ${PRODUCT}"
echo

if [ "${MODE}" != "${PACKAGING_MODE}" ]
then
    EnsureUserIsRoot
fi

if [ "${MODE}" != "${PACKAGING_MODE}" -a "${MODE}" != "${WINDOWS_CONFIG_MODE}" ]
then
    echo
    echo "Removing previous versions of Darwin Streaming Server..."
    echo

    #TODO# What case is this handling ? v2.x uninstall ?

    RemoveFile		/usr/bin/DarwinStreamingServer
    RemoveFile		/usr/bin/PlaylistBroadcaster
    RemoveFile		/usr/bin/qtpasswd
    RemoveDirectory	/usr/bin/StreamingServerModules
    RemoveFile		/usr/local/bin/streamingadminserver.pl
    RemoveFile		/usr/local/sbin/SpamPro
fi

ClearPlatformArchive

# ---------------------------------------------------------------------------

#
# Streaming Proxy Section
#

if [ "${PRODUCT}" = "Proxy" ]
then
    echo
    echo "${MODE_DESCRIPTION} new venison of the Darwin Streaming Proxy..."
    echo

    #
    # Directory Hierarchy
    #
    MakeDirectory	755 ${PA_SBIN_DIR}	${SBIN_DIR}
    MakeDirectory	755 ${PA_ETC_DIR}	${ETC_DIR}
    MakeDirectory	755 ${PA_DOC_DIR}	${DOC_DIR}

    #
    # System Executables
    #
    InstallExecutable	StreamingProxy.tproj/StreamingProxy		\
			${PA_SBIN_DIR}/StreamingProxy			\
			${SBIN_DIR}/StreamingProxy

    #
    # Configuration
    #
    InstallConfigFile	StreamingProxy.tproj/streamingproxy.conf		\
			${PA_ETC_DIR}/streamingproxy.conf-sample-${VERSION}	\
			${ETC_DIR}/streamingproxy.conf-sample-${VERSION}	\
			${ETC_DIR}/streamingproxy.conf
    #
    # Documentation
    #
    InstallDataFile	StreamingProxy.tproj/StreamingProxy.html	\
			${PA_DOC_DIR}/StreamingProxy.html		\
			${DOC_DIR}/StreamingProxy.html

    #
    # Installation Script
    #
    InstallInstallationScript

    #
    # Create Package
    #

    CreatePlatformArchive

    echo
    echo "Installation/Packaging Complete"
    echo

    exit
fi

# ---------------------------------------------------------------------------

#
# Streaming Server Section
#

echo
echo "${MODE_DESCRIPTION} new version of the Darwin Streaming Server..."
echo

# Only the Streaming Server Web Interface requires Perl
ValidatePerlPath "${@}"
ValidatePerlVersion "${@}"

#
# Directory Hierarchy
#
MakeDirectory		755 ${PA_SBIN_DIR}	${SBIN_DIR}
MakeDirectory		755 ${PA_SSM_DIR}	${SSM_DIR}
MakeDirectory		755 ${PA_BIN_DIR}	${BIN_DIR}
MakeDirectory		755 ${PA_ETC_DIR}	${ETC_DIR}
MakeDirectory		755 ${PA_DOC_DIR}	${DOC_DIR}
MakeDirectory		755 ${PA_VAR_DIR}	${VAR_DIR}
MakeDirectory		755 ${PA_LOG_DIR}	${LOG_DIR}
MakeDirectory		755 ${PA_MOV_DIR}	${MOV_DIR}
MakeDirectory		755 ${PA_MP3_DIR}	${MP3_DIR}
MakeDirectory		777 ${PA_PLAY_LIST_DIR}	${PLAY_LIST_DIR}

#
# System Executables
#
InstallExecutable	DarwinStreamingServer				\
			${PA_SBIN_DIR}/DarwinStreamingServer		\
			${SBIN_DIR}/DarwinStreamingServer

#
# General Executables
#
InstallExecutable	PlaylistBroadcaster.tproj/PlaylistBroadcaster	\
			${PA_BIN_DIR}/PlaylistBroadcaster		\
			${BIN_DIR}/PlaylistBroadcaster

InstallExecutable	MP3Broadcaster/MP3Broadcaster			\
			${PA_BIN_DIR}/MP3Broadcaster			\
			${BIN_DIR}/MP3Broadcaster

InstallExecutable	qtpasswd.tproj/qtpasswd				\
			${PA_BIN_DIR}/qtpasswd				\
			${BIN_DIR}/qtpasswd

#
# Streaming Server Modules
#
InstallExecutable	./APIModules/QTSSDemoAuthorizationModule.bproj/QTSSDemoAuthorizationModule	\
			${PA_SSM_DIR}/QTSSDemoAuthorizationModule					\
			${SSM_DIR}/QTSSDemoAuthorizationModule

InstallExecutable	./APIModules/QTSSRawFileModule.bproj/QTSSRawFileModule				\
			${PA_SSM_DIR}/QTSSRawFileModule							\
			${SSM_DIR}/QTSSRawFileModule

InstallExecutable	./APIModules/QTSSSpamDefenseModule.bproj/QTSSSpamDefenseModule			\
			${PA_SSM_DIR}/QTSSSpamDefenseModule						\
			${SSM_DIR}/QTSSSpamDefenseModule

#
# Configuration
#

InstallGeneratedFile	streamingserver.xml-template			\
			${PA_ETC_DIR}/${SS_XML}-template		\
			${ETC_DIR}/${SS_XML}-sample-${VERSION}		\
			${ETC_DIR}/${SS_XML}

InstallGeneratedFile	relayconfig.xml-template			\
			${PA_ETC_DIR}/${RC_XML}-template		\
			${ETC_DIR}/${RC_XML}-sample-${VERSION}		\
			${ETC_DIR}/${RC_XML}

InstallGeneratedFile	qtaccess-template			\
			${PA_ETC_DIR}/qtaccess-template		\
			${ETC_DIR}/qtaccess-sample-${VERSION}		\
			${ETC_DIR}/qtaccess

InstallConfigFile	qtusers						\
			${PA_ETC_DIR}/qtusers-sample-${VERSION}		\
			${ETC_DIR}/qtusers-sample-${VERSION}		\
			${ETC_DIR}/qtusers

InstallConfigFile	qtgroups					\
			${PA_ETC_DIR}/qtgroups-sample-${VERSION}	\
			${ETC_DIR}/qtgroups-sample-${VERSION}		\
			${ETC_DIR}/qtgroups

#
# Certificates
#

#TODO# Are these certificates user customisable ?
#TODO# Should this be InstallConfigFile
InstallDataFile		WebAdmin/streamingadminserver.pem		\
			${PA_ETC_DIR}/${SAS_PEM}			\
			${ETC_DIR}/${SAS_PEM}
#TODO# Clean this up
if [ "${MODE}" != "${PACKAGING_MODE}" ]
then
    ${DO} chmod 400 ${ETC_DIR}/${SAS_PEM}
fi

#
# Documentation
#

#TODO# What Documentation should be installed?

InstallDataFile		Documentation/AboutDarwinStreamingSvr.pdf	\
			${PA_DOC_DIR}/usermanual.pdf			\
			${DOC_DIR}/usermanual.pdf
#
# Sample Data
#

InstallDataFile		sample.mov					\
			${PA_MOV_DIR}/sample.mov			\
			${MOV_DIR}/sample.mov

InstallDataFile		sample_56kbit.mov				\
			${PA_MOV_DIR}/sample_56kbit.mov			\
			${MOV_DIR}/sample_56kbit.mov

InstallDataFile		sample_100kbit.mov				\
			${PA_MOV_DIR}/sample_100kbit.mov		\
			${MOV_DIR}/sample_100kbit.mov

InstallDataFile		sample_300kbit.mov				\
			${PA_MOV_DIR}/sample_300kbit.mov		\
			${MOV_DIR}/sample_300kbit.mov

InstallDataFile		sample.mp3					\
			${PA_MP3_DIR}/sample.mp3			\
			${MP3_DIR}/sample.mp3

#
# StreamingLoadTool install
#

#TODO# # Missing From CVS - StreamingLoadTool source
if [ -f SpamPro.tproj/StreamingLoadTool ]
then
    InstallExecutable	SpamPro.tproj/StreamingLoadTool			\
			${PA_BIN_DIR}/StreamingLoadTool			\
			${BIN_DIR}/StreamingLoadTool
else
    ReportMissingOptionalFile SpamPro.tproj/StreamingLoadTool
fi

#TODO# # Missing From CVS - StreamingLoadTool source
if [ -f SpamPro.tproj/streamingloadtool.conf ]
then
    InstallConfigFile	SpamPro.tproj/streamingloadtool.conf		\
			${PA_ETC_DIR}/${SLT_CONFIG}-sample-${VERSION}	\
			${ETC_DIR}/${SLT_CONFIG}-sample-${VERSION}	\
			${ETC_DIR}/${SLT_CONFIG}
else
    ReportMissingOptionalFile SpamPro.tproj/streamingloadtool.conf
fi

#
# SpamPro install
#

#TODO# # Missing From CVS - SpamPro source
if [ -f SpamPro.tproj/SpamPro ]
then
    InstallExecutable	SpamPro.tproj/SpamPro				\
			${PA_BIN_DIR}/SpamPro				\
			${BIN_DIR}/SpamPro
else
    ReportMissingOptionalFile SpamPro.tproj/SpamPro
fi

#TODO# # Missing From CVS - SpamPro source
#TODO# This is really not the place for configuration files
if [ -f SpamPro.tproj/spampro.conf ]
then
    InstallConfigFile	SpamPro.tproj/spampro.conf			\
			${PA_BIN_DIR}/${SPRO_CONFIG}-sample-${VERSION}	\
			${BIN_DIR}/${SPRO_CONFIG}-sample-${VERSION}	\
			${BIN_DIR}/${SPRO_CONFIG}
else
    ReportMissingOptionalFile SpamPro.tproj/spampro.conf
fi

#
# WebAdmin install
#

echo
echo "Installing WebAdmin..."
echo

InstallExecutable	WebAdmin/src/streamingadminserver.pl		\
			${PA_BIN_DIR}/streamingadminserver.pl		\
			${BIN_DIR}/streamingadminserver.pl

InstallGeneratedFile	WebAdmin/streamingadminserver.conf-template	\
			${PA_ETC_DIR}/${SAS_CONFIG}-template		\
			${ETC_DIR}/${SAS_CONFIG}-sample-${VERSION}	\
			${ETC_DIR}/${SAS_CONFIG}


#
# WebAdmin Scripts / CGI Scripts / Associated Code
#

MakeDirectory	755	${PA_ADMIN_HTML_DIR}				\
			${ADMIN_HTML_DIR}

AllMatching	\*.pl							\
	InstallExecutable	WebAdmin/WebAdminHtml			\
				${PA_ADMIN_HTML_DIR}			\
				${ADMIN_HTML_DIR}

AllMatching	\*.cgi							\
	InstallExecutable	WebAdmin/WebAdminHtml			\
				${PA_ADMIN_HTML_DIR}			\
				${ADMIN_HTML_DIR}

AllMatching	\*.html							\
	InstallExecutable	WebAdmin/WebAdminHtml			\
				${PA_ADMIN_HTML_DIR}			\
				${ADMIN_HTML_DIR}

#
# WebAdmin Images
#

MakeDirectory	755	${PA_ADMIN_HTML_DIR}/images			\
			${ADMIN_HTML_DIR}/images

AllMatching	\*.gif							\
	InstallDataFile		WebAdmin/WebAdminHtml/images		\
				${PA_ADMIN_HTML_DIR}/images		\
				${ADMIN_HTML_DIR}/images

#
# WebAdmin JavaScript
#

MakeDirectory	755	${PA_ADMIN_HTML_DIR}/includes			\
			${ADMIN_HTML_DIR}/includes

AllMatching	\*.js							\
	InstallDataFile		WebAdmin/WebAdminHtml/includes		\
				${PA_ADMIN_HTML_DIR}/includes		\
				${ADMIN_HTML_DIR}/includes

#
# Internationalisation Support
#

InstallWebAdminLanguage de
InstallWebAdminLanguage en
InstallWebAdminLanguage fr
InstallWebAdminLanguage ja

#
# Update the location of the perl executable
#

if [ "${MODE}" != "${PACKAGING_MODE}" ]
then
    echo
    echo "Updating Perl Location in the Streaming Server Web Admin Interface"
    echo
    UpdatePerlPathIn ${BIN_DIR}/streamingadminserver.pl
    UpdatePerlPathIn ${ADMIN_HTML_DIR}/parse_xml.cgi
fi

#
# Installation Script
#

InstallInstallationScript

#
# Create Package
#

CreatePlatformArchive

#
# Run the Streaming Admin Server script
#

if [ "${MODE}" != "${PACKAGING_MODE}" ]
then
    echo
    echo "To Launch the Streaming Admin Server, execute the following command:"
    echo
    echo "    ${BIN_DIR}/streamingadminserver.pl -c ${ETC_DIR}/${SAS_CONFIG}"
fi

echo
echo "Installation/Packaging Complete"
echo

exit
