#!/bin/sh
#
# Copyright(C) 1993-2008 Adobe Systems Incorporated. All rights reserved.
#
# Adobe Flash Media Server Installer
#

PRODUCT="Adobe Flash Media Server"
VERSION="3.0.1"

# Environment variables
PATH=.:/bin:/usr/bin:/usr/local/bin:/sbin:$PATH
export PATH

# Get the path of this script
cwd=`dirname $0`

# setup NLS path
NLSPATH=$cwd/tcSrvMsg:$NLSPATH
export NLSPATH

# License text file
LICENSE=License.txt


##############################
# Subroutines
##############################

# the os is not supported
exit_wrong_platform () {
  echo ""
  echo "ERROR: You are running the $PRODUCT installer"
  echo "       on the wrong platform."
  echo ""
  exit 1
}

# the os is not supported
exit_os () {
  echo ""
  echo "ERROR: Your operating system is not supported by the"
  echo "       $PRODUCT installer."
  echo ""
  exit 1
}
warn_os () {
  echo ""
  echo "WARNING: Your operating system is not supported by the"
  echo "         $PRODUCT installer."
  echo ""
}

# the architecture is not supported
exit_cpu () {
  echo ""
  echo "ERROR: Your architecture, \'$1\', is not supported by the"
  echo "       $PRODUCT installer."
  echo ""
  exit 1
}
warn_cpu () {
  echo ""
  echo "WARNING: Your architecture, \'$1\', is not supported by the"
  echo "         $PRODUCT installer."
  echo ""
}

# the recognized distribution is not supported
exit_unsupported () {
  echo ""
  echo "ERROR: Your distribution, $1, is not supported by this"
  echo "       $PRODUCT installer."
  echo ""
  exit 1
}
warn_unsupported () {
  echo ""
  echo "WARNING: Your distribution, $1, is not supported by this"
  echo "         $PRODUCT installer."
  echo ""
}

# check distribution
check_distro () {
  os="`uname -s`"
  arch="`uname -m`"

  cd_result () {
    echo "$1"
    exit
  }

  cd_check () {
    grep "$1" "$2" >/dev/null 2>&1
    if [ $? = 0 ]; then
      cd_result "$3"
    fi
  }

  case $os in
    SunOS) if [ "$arch" != "sun4u" ]; then
             cd_result unknown
           fi

           release="`uname -r`"

           case $release in
             5.7 | 5.7.*) cd_result solaris-7-sun4
                          ;;
             5.8 | 5.8.*) cd_result solaris-8-sun4
                          ;;
             5.9 | 5.9.*) cd_result solaris-9-sun4
                          ;;
                       *) cd_result unknown
                          ;;
           esac
           ;;
    Linux) case $arch in
             i[3456]86) # Red Hat Linux
                        if [ -f /etc/redhat-release ]; then
			  cd_check "Red Hat Enterprise Linux [WAE]S release 3" /etc/redhat-release redhat-RHEL3-i686
			  cd_check "Red Hat Enterprise Linux [WAE]S release 4" /etc/redhat-release redhat-RHEL4-i686
                        fi

                        # Unknown Red Hat Linux 
                        cd_result unknown
                        ;;
           esac
           ;;
        *) # Unknown OS
           cd_result unknown
           ;;
  esac
}


##############################
# Main Section
##############################

WARN=0

# check command-line arguments
while [ $# -gt 0 ]; do
  case $1 in
    -platformWarnOnly) WARN=1
                       shift
                       ;;
                    *) echo ""
                       echo "Usage: installFMS [OPTION]..."
                       echo "  -platformWarnOnly    warn if running installer on invalid platform"
                       echo "  -usage               display this help and exit"
                       echo ""
                       exit 1
                       ;;
  esac
done

# check user
USERID=`id | sed -e 's/).*//; s/^.*(//;'`
if [ "X$USERID" != "Xroot" ]; then
  echo ""
  echo "ERROR: You must be logged in as the root user to install"
  echo "       the $PRODUCT."
  echo ""
  exit 1
fi

# check platform correctness of exec
RETVAL=`$cwd/fmsini -help`
if [ $? -ne 0 ]; then
  exit_wrong_platform
fi

# check OS
os=`uname -s`
if [ "X$os" != "XLinux" -a "X$os" != "XSunOS" ]; then
  if [ $WARN -eq 1 ]; then
    warn_os
  else
    exit_os
  fi
fi

# check architecture
TEMPARCH=`uname -m`
case $TEMPARCH in
  i[3456]86) ARCH=i386
             ;;
      sun4*) ARCH=sun4
             ;;
          *) if [ $WARN -eq 1 ]; then
               warn_cpu $TEMPARCH
             else
               exit_cpu $TEMPARCH
             fi
             ;;
esac

# check distribution
DISTRO=`check_distro`

case $DISTRO in
    solaris-8-sun4) DISTRO_STRING='solaris-8-sun4'
                    PLATFORM="Solaris"
                    ;;
    solaris-9-sun4) DISTRO_STRING='solaris-9-sun4'
                    PLATFORM="Solaris"
                    ;;
    redhat-73-i386) DISTRO_STRING='redhat-73-i386'
                    PLATFORM="Linux"
                    ;;
    redhat-80-i386) DISTRO_STRING='redhat-80-i386'
                    PLATFORM="Linux"
                    ;;
    redhat-RHEL3-i686)    DISTRO_STRING='redhat-RHEL3-i686'
                    PLATFORM="Linux"
                    ;;
    redhat-RHEL4-i686)    DISTRO_STRING='redhat-RHEL4-i686'
                    PLATFORM="Linux"
                    ;;
                 *) if [ $WARN -eq 1 ]; then
                      warn_unsupported $DISTRO
                    else
                      exit_unsupported $DISTRO
                    fi
                    ;;
esac


##################
# Welcome user
##################
echo ""
echo "Copyright(C) 1993-2008 Adobe Systems Incorporated. All rights reserved."
echo ""
echo "$PRODUCT $VERSION for $PLATFORM"
echo ""
echo "$PRODUCT $VERSION will be installed on this machine."
echo ""
echo "You will be asked a series of questions during the install"
echo "process and will be presented with the defaults for these questions."
echo ""
echo "Support is available at http://www.adobe.com/go/flashmediaserver_en"
echo ""
echo "To install $PRODUCT now, press ENTER."
echo ""
echo "To cancel the installation at any time, press Control-C."
echo ""
read cont < /dev/tty

#########################
# Show license agreement
#########################
if [ -f "$cwd/$LICENSE" ]; then
more "$cwd/$LICENSE"
get_agreement () {
  echo ""
  printf "Do you agree with the license agreement? (y/n): "
  read yn

  case $yn in
    y | Y) break
           ;;
    n | N) exit
           ;;
        *) echo ""
           echo "Please enter 'y' or 'n'."
           get_agreement
           ;;
  esac
}
get_agreement
fi

ask_mm_fms () {
	  echo ""
	  echo ""
	  echo "WARNING: A previous installation has been found at /opt/macromedia/fms"
	  echo ""
	  echo "Do you want to continue installing $PRODUCT without deleting the old version?"
	  echo "Continue [y] or Quit [q] ? (y/q)"
	  printf "Default [y]: "
	  read yn

	  # if user pressed return, use default
	  if [ -z "$yn" ]; then
		yn=Y
	  fi

	  case $yn in
		y | Y) MM_FMS=1
			   ;;
		q | Q) exit
			   ;;
			*) echo ""
			   echo "Please enter 'y' or 'q'."
			   ask_mm_fms
			   ;;
	  esac
}

if [ -e "/opt/macromedia/fms" ]; then
	ask_mm_fms
fi

#Loop until user is comfortable with their choices
okToProceed=0
ROOT_USER=0
export LD_LIBRARY_PATH="$cwd${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
while [ $okToProceed -eq 0 ]; do

  # default variables
  INSTALLDIR=/opt/adobe/fms
  SERVICES_DIR=/etc/adobe/fms/services
  CREATE_ETC_MACR_DIR=0
  RUN_AS_DAEMON=1
  START=1
  SWITCH_ADMIN=0
  OVERWRITE=0
  INSTALL_EXAMPLES=0
  PREV_INSTALL_FOUND=0


	#############################
	# ask for serial number
	#############################
	SERIALNUM=""
	endGetSN=0
	get_serialnumber() {
		read in
		if [ ! -z "$in" ]; then
			`$cwd/checksn $in $cwd/licenses/sif.bin`
			ret=$?
			currentSN=$in
		else
			ret=253
		fi
		return $ret
	}

	tryagain () {
	    printf "Would you like to try again? y/n:  " 
	    printf "Default [n]: "
	    read yn
	
	    if [ -z "$yn" ]; then
		yn="n"
	    fi
	
	    case $yn in
	      y | Y) endGetSN=0
	             ;;
	      n | N) endGetSN=1
	             ;;
	          *) echo ""
	             echo "Please enter 'y' or 'n'"
		     tryagain
	             ;;
	    esac
	}

	while [ $endGetSN -eq 0 ]; do
		echo ""
		printf "Please enter your serial number."
		echo ""
		get_serialnumber
		case $? in
		  2)	echo ""
			echo "You have entered an Upgrade serial number.  Please enter a"
			echo "valid fms2 serial number"
			echo ""
			SERIALNUM=$currentSN
			get_serialnumber
	 		if [ $? -eq "1" ]; then
				echo "Congratulations, you have enabled the"
				echo "Adobe Flash Media Interactive Server!"	
				SERIALNUM="$currentSN;$SERIALNUM"
				endGetSN=1
			else
				echo "An invalid Flash Media Server 2 serial number was entered during the"
				echo "upgrade process. Falling back to the "
				echo "Adobe Flash Media Development Server."
				echo ""
				tryagain	
			fi	
			;;
	    	3)  echo ""
			echo "You have entered an Upsell serial number. Please enter a"
			echo "valid Flash Media Streaming Server 3 serial number"
			echo ""
			SERIALNUM=$currentSN
			get_serialnumber
			if [ $? -eq "118" ]; then
				echo "Congratulations.  You have enabled the"
				echo "Adobe Flash Media Interactive Server!"
				endGetSN=1
				SERIALNUM="$currentSN;$SERIALNUM"
			else
				echo "An invalid Flash Media Server 3 serial number was entered during the"
				echo "upsell process.  Falling back to the"
				echo "Adobe Flash Media Development Server."
				echo ""
				tryagain
			fi
			;;
		  117)  echo ""
			echo "Congratulations.  You have enabled the"
			echo "Adobe Flash Media Interactive Server!"
			SERIALNUM=$currentSN
			endGetSN=1
			;;
		  118)  echo ""
			echo "Congratulations.  You have enabled the"
			echo "Adobe Flash Media Streaming Server!"
			SERIALNUM=$currentSN
			endGetSN=1
			;;
		  253)  echo ""
			echo "You have not entered a serial number.  Falling back to"
			echo "the Adobe Flash Media Development Server!"
			tryagain
			;;
		  254)  echo ""
			echo "There was an error trying to determine the"
			echo "serial number type.  Falling back to the"
			echo "Adobe Flash Media Development Server."
			echo ""
			tryagain
			;;
		  255)  echo ""
			echo "You have entered an invalid serial number.  Falling back to"
			echo "the Adobe Flash Media Server Developer Edition."
			echo ""
			tryagain
			;;
		  *) echo ""
		  	echo "An an unexpected error occurred."
			echo "Please seek help from Adobe.  Falling back to the"
			echo "Adobe Flash Media Development Server."
			echo ""
			tryagain
			;;
		esac
	done

  ############################
  # Get destination directory
  ############################
  echo ""
  echo ""
  echo "$PRODUCT $VERSION requires approximately 200MB of"
  echo "disk space."
  echo ""
  echo "The installer will install $PRODUCT $VERSION in the"
  echo "following directory"
  printf "Default [$INSTALLDIR]: "
  read dir

  if [ ! -z "$dir" ]; then
    INSTALLDIR="$dir"
  fi

	ask_to_overwrite () {
	  echo ""
	  echo ""
	  echo "WARNING: The installer has detected a previous installation."
	  echo ""
	  echo "Do you want the installer to overwrite the previous installation with this"
	  echo "installation or quit this installer? (y/q)"
	  printf "Default [y]: "
	  read yn

	  # if user pressed return, use default
	  if [ -z "$yn" ]; then
		yn=Y
	  fi

	  case $yn in
		y | Y) OVERWRITE=1
			   ;;
		q | Q) exit
			   ;;
			*) echo ""
			   echo "Please enter 'y' or 'q'."
			   ask_to_overwrite
			   ;;
	  esac
	}

  # check directory
  if [ \( -d "$INSTALLDIR" \) -a \( -f "$INSTALLDIR/fmsmaster" \) ]; then
    ask_to_overwrite
  else
    # check for prev installation (allow overwrite)
	if [ -s "$SERVICES_DIR" ]; then
		ask_to_overwrite
	else
		# check if there is an old installation (don't allow overwrite)
		if [ -s "/etc/adobe/fcs/services" ]; then
		  prev_install_found() {
			echo ""
			echo ""
			echo "WARNING: The installer has detected an old installation."
			echo ""
			echo "You must uninstall the old installation before continuing."
			exit
		  }
		  prev_install_found
		fi
	fi
  fi


  ############################
  # Check /etc/adobe/fms
  ############################
  SERVICE_NAME="fms"
  if [ ! -d "/etc/adobe/fms/services" ]; then
    # use default service name
    CREATE_ETC_MACR_DIR=1
  else
    # setup /etc/adobe/fms dir
    # check for fmsadmin service
    if [ -f "$SERVICES_DIR/fmsadmin" ]; then
      SWITCH_ADMIN=1
    fi
  fi

  ############
  # Get ports
  ############
  # Get Server port
  echo ""
  echo ""
  echo "The $PRODUCT communicates on the IANA-assigned"
  echo "port of 1935, which is the port most Flash applications expect."
  get_serverport () {
    FMS_SERVER_PORT=1935
    echo ""
    echo "Please enter the $PRODUCT port"
    printf "Default [$FMS_SERVER_PORT]: "
    read in

    if [ ! -z "$in" ]; then
      FMS_SERVER_PORT=$in
    fi

    # check if valid ports have been entered; if multiple ports are specified
    # they need to be comma delimited
    echo $FMS_SERVER_PORT|grep " " > /dev/null 2>&1
    RET=$?
    if [ $RET -eq 0 ]; then
	   echo ""
	   echo "ERROR: Spaces are not permitted in the server port."
	   echo ""
	   get_serverport
    fi 


    `$cwd/fmsini -checkports $FMS_SERVER_PORT`
    RET=$?
    if [ $RET -ne 0 ]; then
	if [ $RET -eq 1 ]; then
	    ROOT_USER=1
            echo ""
            echo "WARNING: You entered a port number less than 1024."
            echo "         The $PRODUCT service with a port"
            echo "         number less that 1024 must be run by the root user."
            echo ""
	else
	    echo ""
	    echo "ERROR: You have entered an invalid port : $FMS_SERVER_PORT"
	    echo "       Ports must be greater than 0 and less than 65536."
	    echo "       If multiple ports are specified, they must be"
	    echo "       separated by a comma. Please enter a valid port."
	    echo ""
	    get_serverport
	fi
    fi

  }
  get_serverport

  # Get Admin port
  get_adminport () {
    ADMIN_SERVER_PORT=1111
    echo ""
    echo "Please enter the port to use for the Admin service. You can only specify one"
    echo "admin port."
    printf "Default [$ADMIN_SERVER_PORT]: "
    read in

    if [ ! -z "$in" ]; then
      ADMIN_SERVER_PORT=$in
    fi

    # check if valid ports have been entered; if multiple ports are specified
    # they need to be comma delimited
    echo $ADMIN_SERVER_PORT|grep " " > /dev/null 2>&1
    RET=$?
    if [ $RET -eq 0 ]; then
	   echo ""
	   echo "ERROR: Spaces are not permitted in the admin port."
	   echo ""
	   get_adminport
    fi 

    `$cwd/fmsini -checkports $ADMIN_SERVER_PORT`
    RET=$?
    if [ $RET -ne 0 ]; then
	if [ $RET -eq 1 ]; then
	    ROOT_USER=1
            echo ""
            echo "WARNING: You entered a port number less than 1024."
            echo "         The $PRODUCT service with a port"
            echo "         number less that 1024 must be run by the root user."
            echo ""
	else
	    echo ""
	    echo "ERROR: You have entered an invalid port : $ADMIN_SERVER_PORT"
	    echo "       Ports must be greater than 0 and less than 65536."
	    echo "       If multiple ports are specified, they must be"
	    echo "       separated by a comma. Please enter a valid port."
	    echo ""
	    get_adminport
	fi
    fi
  }
  get_adminport

  #############################
  # Get admin user information
  #############################
  echo ""
  echo ""
  echo "The administrative user name and password you provide here is required to use"
  echo "the $PRODUCT Management Console for"
  echo "administration, monitoring, and debugging."

  # get administrative username
  get_username () {
    echo ""
    printf "Please enter the administrative username: "
    read in

    if [ ! -z "$in" ]; then
      ADMINNAME=$in
    else
      echo ""
      echo "Please do not enter a blank username."
      get_username
    fi
  }
  get_username

  # get administrative password
  stty -echo
  while [ 1 ]; do
    echo ""
    printf "Please enter the administrative password: "
    read in
    if [ ! -z "$in" ]; then
      echo ""
      printf "Confirm password: "
      read in_confirm
      if [ "X$in" != "X$in_confirm" ]; then
        echo ""
        echo "ERROR: Passwords do not match."
      else
        break
      fi
    else
      echo ""
      echo "Please do not enter a blank password."
    fi
  done
  stty echo

  ADMINPASSWORD=$in

  ################
  # Get run user 
  ################
  echo ""
  get_run_user () {
    echo ""
    echo ""
    echo "When the $PRODUCT service is started, the service"
    echo "can be run as a user other than \"root\". The server would change to this user"
    echo "when the server is started and has acquired its ports."
    echo ""
    echo "Please enter the user that the $PRODUCT service will run as"
    printf "Default user [$PROCESS_OWNER]: "
    read in

    if [ ! -z "$in" ]; then
      PROCESS_OWNER="$in"
    fi

    # check if valid id
    id $PROCESS_OWNER > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo ""
      echo "ERROR: \"$PROCESS_OWNER\" is not a valid user."
      echo "       Please enter a valid user."
      get_run_user
    fi
  }

  if [ $ROOT_USER -eq 1 ]; then
    PROCESS_OWNER=root
    echo ""
    echo ""
    echo "You have chosen a server or admin server port that is less than 1024."
    echo "The $PRODUCT service will run as the \"root\""
    echo "user."
  else
    PROCESS_OWNER=nobody
    get_run_user
  fi

  # Process owner is also owner of the files
  OWNER=$PROCESS_OWNER

  #######################
  # Get run user's group
  #######################
  ask_for_group () {
    OWNER_GROUP=""
    echo ""
    printf "Please enter a valid user group for the \"$PROCESS_OWNER\" user: "
    read in

    if [ -z "$in" ]; then
      echo ""
      echo "ERROR: Please do not enter a blank user group." 
      ask_for_group
    fi

    # validate user group
    groups $PROCESS_OWNER | grep "\b$in\b" > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo ""
      echo "ERROR: \"$PROCESS_OWNER\" is not in the user group \"$in\""
      ask_for_group
    else
      OWNER_GROUP="$in"
    fi
  }
  ask_for_group

  # run as daemon
  ask_daemon () {
    echo ""
    echo ""
    echo "Do you want the $PRODUCT service to run as a"
    echo "daemon? (y/n)"
    printf "Default [y]: "
    read yn

    if [ -z "$yn" ];then
      yn="Y"
    fi

    case $yn in
      y | Y) RUN_AS_DAEMON=1
             ;;
      n | N) RUN_AS_DAEMON=0
             ;;
          *) echo ""
             echo "Please enter 'y' or 'n'."
             ask_daemon
             ;;
    esac
  }
  ask_daemon
  
  # start after installation
  ask_start () {
    echo ""
    echo ""
    echo "Do you want to start the $PRODUCT"
    echo "after the installation is done? (y/n)"
    printf "Default [y]: "
    read yn

    if [ -z "$yn" ];then
      yn="Y"
    fi

    case $yn in
      y | Y) START=1
             ;;
      n | N) START=0
             ;;
          *) echo ""
             echo "Please enter 'y' or 'n'."
             ask_start
             ;;
    esac
  }
  ask_start


  ##########
  # Summary
  ##########
  echo ""
  echo ""
  echo "----------- Install Action Summary -----------"
  echo ""
  if [ $OVERWRITE -eq 1 ]; then
  echo "WARNING: You have chosen to overwrite a previous installation."
  echo ""
  fi
  echo "Installation directory         = $INSTALLDIR"
  echo ""
  echo "Flash Media Server Port        = $FMS_SERVER_PORT"
  echo "Flash Media Admin Server Port  = $ADMIN_SERVER_PORT"
  echo ""
  echo "Administrative username        = $ADMINNAME"
  echo "Administrative password        = (suppressed)"
  echo ""
  echo "service owner                  = $OWNER"
  echo ""
  echo "service user                   = $PROCESS_OWNER"
  echo "service group                  = $OWNER_GROUP"
  if [ $RUN_AS_DAEMON -eq 1 ]; then
  echo ""
  echo "Run as daemon                  = Yes"
  fi
  if [ $START -eq 1 ]; then
  echo "Start Flash Media Server       = Yes"
  fi
  echo ""

  # okay to continue?
  get_installagreement () {
    printf 'Proceed with the installation? (y/n/q): '
    read yn

    case $yn in
      y | Y) okToProceed=1
             ;;
      n | N) continue
             ;;
      q | Q) exit 1
             ;;
          *) echo ""
             echo "Please enter 'y', 'n', or 'q'."
             get_installagreement
             ;;
    esac
  }
  get_installagreement

done


#######################
# Perform installation
#######################

# create installation directory
mkdir -p $INSTALLDIR

# copy files
echo ""
echo "Installing $PRODUCT files..."

cp -f $cwd/fmsmaster $INSTALLDIR
cp -f $cwd/fmsedge $INSTALLDIR
cp -f $cwd/fmscore $INSTALLDIR
cp -f $cwd/fmsadmin $INSTALLDIR
cp -f $cwd/fmsmgr $INSTALLDIR
cp -f $cwd/libasneu.so.1 $INSTALLDIR
cp -f $cwd/uninstallFMS $INSTALLDIR
cp -f $cwd/tcSrvMsg $INSTALLDIR
cp -rf $cwd/conf $INSTALLDIR
cp -rf $cwd/licenses $INSTALLDIR
cp -r $cwd/samples $INSTALLDIR
cp -f $cwd/adminserver $INSTALLDIR
cp -f $cwd/server $INSTALLDIR
cp -r $cwd/modules $INSTALLDIR
cp -f $cwd/License.txt $INSTALLDIR
cp -f $cwd/License.htm $INSTALLDIR
cp -f $cwd/Readme.htm $INSTALLDIR
cp -f $cwd/logo.gif $INSTALLDIR
cp -f $cwd/fms_adminConsole.swf $INSTALLDIR
cp -f $cwd/fms_adminConsole.htm $INSTALLDIR
cp -f $cwd/AC_RunActiveContent.js $INSTALLDIR
cp -f $cwd/shmrd $INSTALLDIR

# copy tools
mkdir -p "$INSTALLDIR/tools"
cp -f $cwd/far $INSTALLDIR/tools
cp -f $cwd/tools/* $INSTALLDIR/tools

# copy components
cp -rf $cwd/scriptlib $INSTALLDIR

# copy docs
cp -rf $cwd/docs $INSTALLDIR

# create applications directory
mkdir -p "$INSTALLDIR/applications"
#cp -rf $cwd/applications/* $INSTALLDIR/applications

# configure FMS configuration files
echo "Configuring $PRODUCT..."

# set license profile
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.LICENSEINFO $SERIALNUM > /dev/null 2>&1

# set admin info
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.ADMIN_USERNAME $ADMINNAME > /dev/null 2>&1
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.ADMIN_PASSWORD $ADMINPASSWORD > /dev/null 2>&1

# set admin server port
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.ADMINSERVER_HOSTPORT :$ADMIN_SERVER_PORT > /dev/null 2>&1

# set server port
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini ADAPTOR.HOSTPORT :$FMS_SERVER_PORT > /dev/null 2>&1

# set scriptlib path
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini APP.JS_SCRIPTLIBPATH $INSTALLDIR/scriptlib > /dev/null 2>&1

# set applications path
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini VHOST.APPSDIR $INSTALLDIR/applications > /dev/null 2>&1

# set live path
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini LIVE_DIR $INSTALLDIR/applications/live > /dev/null 2>&1

# set vod path
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini VOD_DIR $INSTALLDIR/applications/vod/media > /dev/null 2>&1

# set uid and gid
OWNER_ID=`id $PROCESS_OWNER | sed -e 's/).*//; s/(.*//; s/^.*=//;'`
OWNER_GROUP_ID=`cat /etc/group | grep "^$OWNER_GROUP:" | cut -f3 -d:`
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.PROCESS_UID $OWNER_ID > /dev/null 2>&1
$cwd/fmsini -chgtag $INSTALLDIR/conf/fms.ini SERVER.PROCESS_GID $OWNER_GROUP_ID > /dev/null 2>&1

# modify help_data.txt files
if [ $INSTALL_EXAMPLES -eq 1 ]; then
  if [ "$INSTALLDIR" = "$WEBPUB" ]; then
    echo "targetSamples=$INSTALLDIR&targetHelp=$INSTALLDIR&adminInstall=1&inFlash=0&helpProtocol=file:///" > $INSTALLDIR/fms_help/html/help_data.txt
    echo "targetSamples=$INSTALLDIR&targetHelp=$INSTALLDIR&adminInstall=1&inFlash=0&helpProtocol=file:///" > $INSTALLDIR/fms_help/html/admin/help_data.txt
  else
    echo "targetSamples=$WEBPUB/fms&targetHelp=$INSTALLDIR&adminInstall=1&inFlash=0&helpProtocol=file:///" > $INSTALLDIR/fms_help/html/help_data.txt
    echo "targetSamples=$WEBPUB/fms&targetHelp=$INSTALLDIR&adminInstall=1&inFlash=0&helpProtocol=file:///" > $INSTALLDIR/fms_help/html/admin/help_data.txt
  fi
fi

mkdir -p "$INSTALLDIR/applications/vod" > /dev/null 2>&1
cp -rf $cwd/applications/vod $INSTALLDIR/applications

mkdir -p "$INSTALLDIR/applications/live" > /dev/null 2>&1
cp -rf $cwd/applications/live $INSTALLDIR/applications

# create /etc/adobe/fms/services dir
if [ $CREATE_ETC_MACR_DIR -eq 1 ]; then
  mkdir -p "$SERVICES_DIR"
fi

# add FMS service
$INSTALLDIR/fmsmgr add $SERVICE_NAME $INSTALLDIR

# user chooses to use this Admin Server
if [ ! -f "$SERVICES_DIR/fmsadmin" ]; then
  $INSTALLDIR/fmsmgr setAdmin $SERVICE_NAME
else
  if [ $SWITCH_ADMIN -eq 1 ]; then
    # set FMS Admin server as Primary Admin
    $INSTALLDIR/fmsmgr setAdmin $SERVICE_NAME
  fi
fi

# modify uninstaller
cat $INSTALLDIR/uninstallFMS | sed -e "s%^SERVICE_NAME=%SERVICE_NAME=$SERVICE_NAME%g" | sed -e "s%^INSTALLDIR=%INSTALLDIR=$INSTALLDIR%g" | sed -e "s%^WEBPUB=%WEBPUB=$WEBPUB%g" | sed -e "s%^APPDEST=%APPDEST=$INSTALLDIR%g" > $INSTALLDIR/uninstallFMS.tmp
mv $INSTALLDIR/uninstallFMS.tmp $INSTALLDIR/uninstallFMS
chmod +x $INSTALLDIR/uninstallFMS

# create services location file
echo "$SERVICES_DIR" > "$INSTALLDIR/.services"

# change ownership of files
chown -R $OWNER $INSTALLDIR

# change group of files
chgrp -R $OWNER $INSTALLDIR

# change ownership of files if necessary
chmod -R 755 $INSTALLDIR
chmod -R 751 $INSTALLDIR/conf

# run as daemon
if [ $RUN_AS_DAEMON -eq 1 ]; then
  # install rc2.d script
  cp -f $cwd/fms /etc/init.d
  # Linux only
  /sbin/chkconfig --add fms
  $INSTALLDIR/fmsmgr setAutostart $SERVICE_NAME
fi

# if overwrite, shutdown services in case
if [ $OVERWRITE -eq 1 ]; then
  cd "$INSTALLDIR"
  fmsmgr server $SERVICE_NAME stop
  fmsmgr adminserver stop
fi

# start server service
if [ $START -eq 1 ]; then
  cd "$INSTALLDIR"
  fmsmgr server $SERVICE_NAME start
  fmsmgr adminserver start
fi


echo ""
echo "The $PRODUCT installation is complete."
echo ""

