#!/bin/sh

DEBUG=$1
BASE=`pwd`
SYSNAME=`uname -s | tr '/' '_'`
SRCTAR="w3_src.tar"
SRCTARPATH="${BASE}/${SRCTAR}"
W3LIB="w3library"
JAVA_IMGS="javaimg"
SRC_W3LIB="${BASE}/${W3LIB}"
BIN_PATH="/usr/bin"
CGI_PATH=""
CGI_NAME="fccgi.exe"
DEBUG_NAME="FCdebug"
CONF_DEFAULT_PATH="/etc"
CONF_NAME="flashconnect.conf"
LOG_NAME="flashconnect.log"
FCSS_NAME="fcsocketserver"
FCSS_JAR="fcss.jar"
FCSS_TEMPLATE="FCSS.template"
FCSERVLET="libFCServlet.so"
JAVA_LIB_PATH="/usr/lib/rocketsoftware"
W3LIB_PATH=""
SERVER_NAME=""
START_PORT=""
NUM_PORTS=""
CGI_ROOT="/var/www/cgi-bin"
DOC_ROOT="/var/www/html"
RET_DIR=""
W3AGENT_DIR=""
RET_RSP=""
ECHO=echo
UNCOMPRESS=uncompress

################################################################################
# clean up system.  this is called during debugging of the script to clean up
# all of the installed files.
################################################################################
clean_up()
{
	[ -f ${SRCTARPATH} ] && rm -f ${SRCTARPATH}
	[ -f ${SRCTARPATH}.Z ] && rm -f ${SRCTARPATH}.Z
	[ -d ${BASE}/src_${SYSNAME} ] && rm -rf ${BASE}/src_${SYSNAME}
	[ -d ${BASE}/${W3LIB} ] && rm -rf ${BASE}/${W3LIB}
	[ -d ${BASE}/${JAVA_IMGS} ] && rm -rf ${BASE}/${JAVA_IMGS}
}

################################################################################
# Beep the terminal $cnt times
################################################################################
beep()
{
	cnt=$1

	num=1
	while [ $num -le $cnt ]
	do
		num=`expr $num + 1`
		$ECHO -n "\007"
	done
}

################################################################################
# Display the message $MESG, call beep for $BEEP number of times and if
# $END = 1 exit the program, otherwise return to the calling function
################################################################################
disp_err()
{
	MESG1=$1
	BEEP=$2
	END=$3

	S10="**********"
	S20=${S10}${S10}

	clear
	echo; echo; echo
	echo "${S20}${S20}${S20}${S20}"
	echo "${S20}  ERROR ERROR ERROR ERROR ERROR ERROR   ${S20}"
	echo "${S20}${S20}${S20}${S20}"
	$ECHO $MESG1
	echo
	beep $BEEP
	if [ $END = 1 ]
	then
		echo "Program exiting"
		echo
		exit 0
	else
		echo "Press return to continue."
		read zxc
		clear
	fi
}
	
################################################################################
# copy a file from x_src to x_tar, change the owner to x_own and the 
# permissions to w_per
################################################################################
cp_chg()
{
	x_src=$1    # The source file includeing path 
	x_tar=$2    # The target directory
	x_own=$3    # The owner of the file
	x_per=$4    # The premissions for the file

	x_base=`basename $x_src`
	x_new="${x_tar}/${x_base}"

	if [ -f ${x_new} ]
	then
		get_resp "N" "The file ${x_new} exists. Do you want to overwrite it? "	
		x_ans=$RET_RSP
		if [ "$x_ans" = "Y" -o "$x_ans" = "y" ]
		then
			if [ "$SYSNAME" = "dgux" ]
			then
				rm -f $x_new
				cp $x_src $x_tar
			else
				cp -f $x_src $x_tar
			fi
			chown ${x_own} $x_new
			chmod ${x_per} $x_new
		else
			echo "The old ${x_new} was left in place"
		fi
	else
		cp $x_src $x_tar
		chown ${x_own} $x_new
		chmod ${x_per} $x_new
	fi
}

################################################################################
# get a value from the user and save it in RET_RSP
################################################################################
get_value()
{
	default=$1
	prompt1=$2

	echo

	if [ -z "$default" ]
	then
		$ECHO "${prompt1}: "
	else
		$ECHO "${prompt1}["${default}"](Q=QUIT): "
	fi

	read ans

	if [ -n "$default" -a -z "$ans" ]
	then
		ans=$default
	fi

	if [ "$ans" = 'Q' -o "$ans" = 'q' ]
	then
		echo "Exiting the program on user request"
		exit 0
	fi

	RET_RSP="$ans"
}

################################################################################
# get a response from the user and save it in RET_RSP
################################################################################
get_resp()
{
	default=$1
	prompt1=$2

	echo

	if [ -z $default ]
	then
		$ECHO "$prompt1(Y/N/Q=QUIT): "
	else
		$ECHO "$prompt1["${default}"](Y/N/Q=QUIT): "
	fi

	read ans

	if [ -n "$default" -a -z "$ans" ]
	then
		ans=$default
	fi

	if [ "$ans" = 'Q' -o "$ans" = 'q' ]
	then
		echo "Exiting the program on user request"
		exit 0
	fi

	RET_RSP=$ans 
}

################################################################################
# create a FlashConnect configuration file
################################################################################
create_conf()
{
	RET_RSP=""
	cnt=0
	do=0

	tmp_srt1="Enter the path to the directory for the\012"
	tmp_str2="FlashConnect configuration file, ${CONF_NAME}. "
	tmp_str="${tmp_srt1}${tmp_str2}"
	get_dir "${CONF_DEFAULT_PATH}" "${tmp_str}" 
	CONF_PATH=$RET_DIR

	if [ -f ${CONF_PATH}/${CONF_NAME} ]
	then
		tmp_str1="The file ${CONF_PATH}/${CONF_NAME} already exists.\012"
		tmp_str2="Do you want to overwrite it? "
		tmp_str=${tmp_str1}${tmp_str2}
		get_resp "Y" "${tmp_str}"
		ans=$RET_RSP
		if [ "$ans" = "Y" -o "$ans" = "y" ]
		then
			do=1
		fi
	else
		do=1
	fi

	if [ $do -eq 1 ]
	then
		tmp_str="Enter the path to the directory for FlashConnect named pipes "
		get_dir "/tmp" "$tmp_str"
		PIPEDIR=$RET_DIR

		get_log_dir
		LOG_PATH="${LOGDIR_ROOT}/${LOG_NAME}"
		touch ${LOG_PATH}
		chmod 666 ${LOG_PATH}

		tmp_str="Enter the number of server slots"
		get_value "16" "${tmp_str}"
		D3SLOTS=$RET_RSP

		tmp_str="Enter the FlashConnect environment size"
		get_value "4096" "${tmp_str}"
		ENVSIZE=$RET_RSP

		tmp_str="Enter the FlashConnect TCP/IP port number"
		get_value "1505" "${tmp_str}"
		FCPORT=$RET_RSP

		tmp_str="Enter the FlashConnect heartbeat in seconds (0 = no heartbeat)"
		get_value "0" "${tmp_str}"
		FCHEART=$RET_RSP

		tmp_str="Enter the FlashConnect translation specification string (empty = no translation)"
		get_value "" "${tmp_str}"
		FCXLATE=$RET_RSP

		sed -es!_PIPEDIR_!${PIPEDIR}! -es!_FCLOGDIR_!${LOGDIR_ROOT}! -es/_D3SLOTS_/${D3SLOTS}/ -es/_ENVSIZE_/${ENVSIZE}/ -es/_FCPORT_/${FCPORT}/ -es/_FCHEART_/${FCHEART}/ -es!_FCXLATE_!${FCXLATE}! ${BASE}/FCconf.template > $CONF_PATH/$CONF_NAME

	else
		echo "The $CONF_NAME file was not produced"
	fi
}

################################################################################
# create java component
################################################################################
create_java()
{
	tmp_str="Do you wish to install the FlashConnect Java component? "
	get_resp "Y" "${tmp_str}"
	ans=$RET_RSP

	if [ "$ans" = "Y" -o "$ans" = "y" ]
	then
		tmp_str="Enter the path to the directory for the Java library files "
		get_dir_ne "$JAVA_LIB_PATH" "$tmp_str"
		FCSS_DIR=$RET_DIR

		if [ ! -d $FCSS_DIR ]
		then
		  	mkdir $FCSS_DIR
		  	chmod a+rwx $FCSS_DIR
		fi

		if [ ! -d $FCSS_DIR/images ]
		then
		  	mkdir $FCSS_DIR/images
		  	chmod a+rwx $FCSS_DIR/images
		fi

		tmp_str1="Enter the path to the directory for the FCSocketServer\012"
		tmp_str2="script used to start and stop the FlashConnect Java component "
		tmp_str=${tmp_str1}${tmp_str2}
		get_dir "$W3AGENT_DIR" "$tmp_str"
		JAVA_DIR=$RET_DIR

		cp_chg ${BASE}/${FCSS_JAR} ${FCSS_DIR} "root" "644"
                cp_chg ${BASE}/examples.jar ${FCSS_DIR} "root" "644"
		cp -r ${BASE}/${JAVA_IMGS}/* ${FCSS_DIR}/images
		cp_chg ${BASE}/src_${SYSNAME}/${FCSERVLET} ${FCSS_DIR} "root" "644"
		sed -es!#FCSSPATH#!${FCSS_DIR}! ${BASE}/${FCSS_TEMPLATE} > $JAVA_DIR/$FCSS_NAME
		chmod 755 $JAVA_DIR/$FCSS_NAME
	else
		echo "Skipping Java component installation."
	fi
}

################################################################################
# copy demonstration files
################################################################################
cp_demo()
{
	RET_RSP=""
	RET_DIR=""
	
	get_resp "Y" "Do you want to load the w3library demonstration files? "
	ans=$RET_RSP

	if [ "$ans" = "Y" -o "$ans" = "y" ]
	then
		tmp_str1="Enter the path relative to the "
        tmp_str2="Document Root (${DOC_ROOT})\012"
		tmp_str3="for the demonstration files directory."
		tmp_str4="\012Do not place '.' or './' at the"
		tmp_str5=" beginning of the path."
		tmp_str="${tmp_str1}${tmp_str2}${tmp_str3}${tmp_str4}${tmp_str5}"
		get_dir_rel "$DOC_ROOT" "" "${tmp_str}"
		W3LIB_PATH=$RET_DIR

		if [ -d $W3LIB_PATH/$W3LIB ]
		then
			tmp_str1="The directory ${W3LIB_PATH}/${W3LIB} exists.\012"
			tmp_str2="Do you want to overwrite the existing directory? "
			tmp_str="${tmp_str1}${tmp_str2}"
			get_resp "Y"  "${tmp_str}"
			ans=$RET_RSP

			if [ "$ans" = "Y" -o "$ans" = "y" ]
			then
				if [ "$SYSNAME" = "dgux" ]
				then
					rm -f "$W3LIB_PATH/$W3LIB"
					cp -r $SRC_W3LIB $W3LIB_PATH
				else
					cp -rf $SRC_W3LIB $W3LIB_PATH
				fi
			else
				echo "The old $W3LIB_PATH directory was left in place"
			fi
		else
			tmp_str1="The directory $W3LIB_PATH/$W3LIB does not\012"
			tmp_str2="exist.  Do you want to create it? "
			tmp_str="${tmp_str1}${tmp_str2}"
			get_resp "Y" "${tmp_str}"
			ans=$RET_RSP
			if [ "$ans" = "Y" -o "$ans" = "y" ]
			then
				mkdir $W3LIB_PATH/$W3LIB
				chmod a+rwx $W3LIB_PATH/$W3LIB
				cp -r $SRC_W3LIB $W3LIB_PATH
			else
				echo "No w3library demonstration files will be loaded"
			fi
		fi
	else
		echo "No w3library demonstration files will be loaded"
	fi
}

################################################################################
# Prompt the user for a info with $prompt, if $default in non null 
# display $default, if $path null and $defualt non null $path = $defualt,
# if $path = Q or q exit script, $RET_DIR = $path
# this is differnt from get_dir() as it will allow a directory that doesn't exist
################################################################################
get_dir_ne()
{
	default=$1
	prompt1=$2

	echo

	if [ -z "$default" ]
	then
		$ECHO "${prompt1}(Q=QUIT): "
	else
		$ECHO "${prompt1}["${default}"](Q=QUIT): "
	fi

	read ans

	if [ -n "$default" -a -z "$ans" ]
	then
		ans=$default
	fi

	if [ "$ans" = 'Q' -o "$ans" = 'q' ]
	then
		echo "Exiting the program on user request"
		exit 0
	fi
	RET_DIR=$ans 
}

################################################################################
# Prompt the user for a info with $prompt, if $default in non null 
# display $default, if $path null and $defualt non null $path = $defualt,
# if $path = Q or q exit script, if $path not exist display error- try again,
# if $path exists $RET_DIR = $path
################################################################################
get_dir()
{
	default=$1
	prompt1=$2

	loop=1
	while [ $loop = 1 ]
	do
		echo

		if [ -z "$default" ]
		then
			$ECHO "${prompt1}(Q=QUIT): "
		else
			$ECHO "${prompt1}["${default}"](Q=QUIT): "
		fi

		read ans

		if [ -n "$default" -a -z "$ans" ]
		then
			ans=$default
		fi

		if [ "$ans" = 'Q' -o "$ans" = 'q' ]
		then
			echo "Exiting the program on user request"
			exit 0
		elif [ ! -d $ans ]
		then
			clear
			disp_err "The directory $ans does not exist." 10 0
		else
			RET_DIR=$ans 
			loop=0
		fi
	done
}

################################################################################
# Prompt the user for a info with $prompt, if $default in non null 
# display $default, if $path null and $defualt non null $path = $defualt,
# if $path = Q or q exit script, if $path exists $RET_DIR = $path
################################################################################
get_dir_rel()
{
	base=$1
	default=$2
	prompt1=$3

	tmp_ans=""

	echo

	if [ -z "$default" ]
	then
		$ECHO "${prompt1}(Q=QUIT): "
	else
		$ECHO "${prompt1}["${default}"](Q=QUIT): "
	fi

	read ans

	if [ -n "$default" -a -z "$ans" ]
	then
		ans=$default
	fi

	if [ "$ans" = "Q" -o "$ans" = "q" ]
	then
		echo "Exiting the program on user request"
		exit 0
	fi
	
	if [ -z "$ans" ]
	then
		tmp_ans=$base
	else
		tmp_ans=$base/$ans
	fi

	RET_DIR=$tmp_ans
}

################################################################################
# Get the cgi root directory
################################################################################
get_cgi_dir()
{
	RET_DIR=""
	
	get_dir $CGI_ROOT "Enter the path to the CGI-BIN Root directory. "
	CGI_ROOT=$RET_DIR
}

################################################################################
# Get the error log root directory
################################################################################
get_log_dir()
{
	RET_DIR=""

	if [ "${SYSNAME}" = "HP-UX" ]
	then
		LOGDIR_ROOT="/usr/adm/log"
	else
		LOGDIR_ROOT="/var/log"
	fi
	
	get_dir $LOGDIR_ROOT "Enter the path to the directory for the FlashConnect log file."
	LOGDIR_ROOT=$RET_DIR
}

################################################################################
# Get the w3agent directory
################################################################################
get_w3agent_dir()
{
	RET_DIR=""

	get_dir $BIN_PATH "Enter the path to the FCService executable directory. "
	W3AGENT_DIR=$RET_DIR

	cp_chg ${BASE}/src_${SYSNAME}/FCService ${W3AGENT_DIR} "root" "755"
    cp_chg ${BASE}/src_${SYSNAME}/${DEBUG_NAME} ${W3AGENT_DIR} "root" "700"
}
	
################################################################################
# Get the document root directory
################################################################################
get_doc_root ()
{
	RET_DIR=""

	x_def=$DOC_ROOT

	get_dir ${x_def} "Enter the path to the Document Root directory. "
	DOC_ROOT=$RET_DIR
}
	
################################################################################
# if $SRCTARPATH not exist - exit script, extract the the system specific 
# code and the w3library directory.
################################################################################
untar()
{
	sysname=$1

	if [ -f ${SRCTARPATH}.Z ]
	then
		${UNCOMPRESS} ${SRCTARPATH}.Z
	fi

	if [ -f ${SRCTARPATH} ]
	then
		cd ${BASE}
		tar xf ${SRCTAR} "src_${sysname}"
		tar xf ${SRCTAR} "${W3LIB}"
		tar xf ${SRCTAR} "${JAVA_IMGS}"
	else
		disp_err "The file ${SRCTAR} does not exists" 10 1
	fi
}

################################################################################
# Main program
################################################################################

# Offer view of End User License Agreement
# ----------------------------------------
get_resp "Y" "View the End User License Agreement? "
x_ans=$RET_RSP
if [ "$x_ans" = "Y" -o "$x_ans" = "y" ]
then
	more ${BASE}/License.txt
fi

# Check for acceptance of End User License Agreement
# --------------------------------------------------
get_resp "Q" "Do you accept the terms of the End User License Agreement? "
x_ans=$RET_RSP
if [ "$x_ans" = "Y" -o "$x_ans" = "y" ]
then
	echo "continuing..."
else
	exit 0
fi

# if on Linux, we need to figure out what release we are on, and if on 7.x
# change the system name accordingly
# ------------------------------------------------------------------------
if [ "${SYSNAME}" = "Linux" ]
then
    UNCOMPRESS=gunzip
    # default "Linux" is Red Hat Enterprise Server 5
	SYSNAME="RHES5"
fi

# if on AIX, we need to figure out what release we are on, and
# change the system name accordingly
# ------------------------------------------------------------------------
if [ "${SYSNAME}" = "AIX" ]
then
    
	AIXVERSION=`uname -a | cut -f 4 -d" "`
		if [ "${AIXVERSION}" != "4" ]
		then
			# AIX5 and AIX6 AND AIX7 use same code.        
			AIXVERSION="5"
		fi
	SYSNAME=${SYSNAME}${AIXVERSION}
fi

# Untar the source files and set the ECHO command
# -----------------------------------------------
untar $SYSNAME

#ECHO="${BASE}/src_${sysname}/fcprt" ; export ECHO
ECHO="echo -e" ; export ECHO

# Get the path for $DOC_ROOT
# --------------------------
get_doc_root

# Get the path for $CGI_ROOT
# --------------------------
get_cgi_dir

# Place w3agent in the path provided by the user 
# ---------------------------------------------------------
get_w3agent_dir

# Prompt the user for the path to install the w3library directory, and install
# the w3library and image directory
# ---------------------------------------------------------------------------
cp_demo

# Place the fccgi.exe in the cgi root directory
# ---------------------------------------------------------------
cp_chg ${BASE}/src_${SYSNAME}/${CGI_NAME} ${CGI_ROOT} "root" "6755"

# Delete old version of FCdebug from cgi root directory.
# ------------------------------------------------------
rm -f ${CGI_ROOT}/${DEBUG_NAME}
# cp_chg ${BASE}/src_${SYSNAME}/${DEBUG_NAME} ${CGI_ROOT} "root" "700"

# Prompt the user for the directory to place the flashconnect.conf file in, the 
# server name, the starting port, and the number of ports. Create the 
# flashconnect.conf file in the correct directory
# ------------------------------------------------------------------------
create_conf

# install fcexec when on AIX
# No. Currently only used by mvBase on Windows.
# -----------------------------------------------------------------------
#if [ "${SYSNAME}" = "AIX4" ]
#then
#       cp_chg ${BASE}/src_${SYSNAME}/fcexec ${BIN_PATH} "root" "755"
#fi
#if [ "${SYSNAME}" = "AIX5" ]
#then
#       cp_chg ${BASE}/src_${SYSNAME}/fcexec ${BIN_PATH} "root" "755"
#fi

# only install java component when not on SCO (still can't get it working)
# ------------------------------------------------------------------------
if [ "${SYSNAME}" != "SCO_SV" ]
then
	create_java
fi

if [ "$DEBUG" != "Y" ]
then
	clean_up
fi

echo "Installation Complete"
