#!/bin/sh 
# TODO: check -f 
#
# WARNING: This software is protected by copyright law and international 
#          treaties. Unauthorized reproduction or distribution of this program,
#          or any portion of it, may result in severe civil and criminal 
#          penalties, and will be prosecuted to the maximum extent possible 
#          under law.
#

DEFAULT_INSTALLATION_DIR=/opt/SYBSasa6


initialize_variables() 
######################
{
    OPT_N_SERVER="NA"
    OPT_P_SERVER="NA"
    OPT_N_CLIENT="NA"
    OPT_DBREMOTE="NA"
    OPT_REPAGENT="NA"

    DONE_OPTIONS=false
    ASANY_REGKEY=
    REGKEY_OPTIONS=
    JUNK=
    JUNK2=
    PACKAGE_NAME="Sybase SQL Anywhere Studio 6.0.4 EBF"

    PWD=`pwd`
    MEDIA_ROOT=`dirname $0`
    case _$MEDIA_ROOT in
	_/* )
	    true
	    ;;

	* )
	    MEDIA_ROOT=`dirname $PWD/$0`
	    ;;
    esac
    PATH=$MEDIA_ROOT/bin:/bin:/usr/bin:$PATH
    export PATH

    OS_NAME=`uname -n`
    OS_TYPE=`uname -s`
    if [ $OS_TYPE = "SunOS" ]; then
	OS_TYPE="${OS_TYPE}_`uname -p`"
    fi
    export OS_TYPE OS_NAME

    ASANY_INSTALL=EBF 

    ASANY_DIR=
    SHARED_DIR=
    SSREMOTE_DIR=
    SYBASE_DIR=

    CREATE_ASANY_DIR=
    CREATE_SHARED_DIR=
    CREATE_SSREMOTE_DIR=
    CREATE_SYBASE_DIR=

    INSTALL_ASA=false
    INSTALL_SHARED=false
    INSTALL_SYBASE=false
    INSTALL_SSREMOTE=false

    USER_NAME=
    COMP_NAME=
    LIC_TYPE=
    NUM_SEATS=

    TMPFILE=
    RESFILE=
    TMPPREFIX="/tmp/SybSetup."
    TICFILE=$MEDIA_ROOT/files.tic
    CONFIGFILE=asa_config

    LDPATH_VARNAME=

    export ASANY_INSTALL
}


clean_up()
##########
{
   for file in /tmp/asa_install.* /tmp/SybSetup.* ; do
        pid=`echo $file|cut -d. -f,2`
        JUNK=`valid_pid $pid`
        if [ "$JUNK" = "1" ]; then
            rm -f $file
        fi 
   done
   rm -f $TMPFILE
}

signal_handler()
################
{
    echo Cleaning up please wait ...
    clean_up
    echo completed
    clear_signal_handler
    exit 1
}

set_signal_handler()
####################
{
    trap signal_handler HUP INT TERM
}

clear_signal_handler()
######################
{
    trap HUP INT TERM
}

check_installed_files()
########################
# $1: OPT_X
# $2: filename
{
    #sets up the TMPFILE var.
    create_new_tmpfile

    JUNK=`echo $ASANY_DIR | sed 's/\//\\\\\//g'`
    cat $MEDIA_ROOT/files/$2 | sed "s/ASA/$JUNK/g" > $TMPFILE
    JUNK=`cat $TMPFILE`
    for installed_file in $JUNK; do
        if [ -f "$installed_file" ]; then
            eval "$1=\" X \""
            break
        fi
    done
}

check_install_options()
#######################
{
    if [ ! -f $MEDIA_ROOT/n_server/files ]; then
    	OPT_N_SERVER="NA"
    fi

    if [ ! -f $MEDIA_ROOT/p_server/files ]; then
    	OPT_P_SERVER="NA" 
    fi

    if [ ! -f $MEDIA_ROOT/n_client/files ]; then
    	OPT_N_CLIENT="NA" 
    fi

    if [ ! -f $MEDIA_ROOT/p_srv_sr/files ]; then
    	OPT_DBREMOTE="NA"
    fi 

    if [ ! -f $MEDIA_ROOT/files/shared ]; then
    	OPT_SHARED="NA"
    fi 

    if [ ! -f $MEDIA_ROOT/repagent/files ]; then
    	OPT_REPAGENT="NA" 
    fi 

    if [ ! -f $MEDIA_ROOT/files/ssremote ]; then
    	OPT_SSREMOTE="NA" 
    fi

    if [ ! -f $MEDIA_ROOT/files/asa_javadocs ]; then
    	OPT_JAVA_DOC="NA" 
    fi

}

generate_filelist()
###################
# PRE : All options should be selected.
# POST: Create a new TMPFILE and put list of all files there without duplicates.
{
    #sets up the TMPFILE var.
    create_new_tmpfile

    if [ "$OPT_N_SERVER" = " X " ]; then
        cat "$MEDIA_ROOT/n_server/files" >> "$TMPFILE"
    fi

    if [ "$OPT_P_SERVER" = " X " ]; then
    	cat "$MEDIA_ROOT/p_server/files" >> "$TMPFILE"
    fi

    if [ "$OPT_N_CLIENT" = " X " ]; then
    	cat "$MEDIA_ROOT/n_client/files" >> "$TMPFILE"
    fi

    if [ "$OPT_DBREMOTE" = " X " ]; then
    	cat "$MEDIA_ROOT/p_srv_sr/files" >> "$TMPFILE"
    fi 

    if [ "$OPT_REPAGENT" = " X " ]; then
    	cat "$MEDIA_ROOT/repagent/files" >> "$TMPFILE"
    fi 

    # remove duplicates and sort	
    sort -u -o "$TMPFILE" "$TMPFILE"
}

check_os()
##########
{
    case "$OS_TYPE" in
	AIX)	
            OS_REL=`oslevel`
            OS_REL_COMP=`oslevel | sed 's?\.??' | sed 's?\.??'`

            OS_MIN_REL="4.3.0.0"
            OS_MIN_COMP="430.0"

            LDPATH_VARNAME=LIBPATH
            ;;

	HP-UX)	
            OS_REL=`uname -r`
            OS_REL_COMP=`uname -r|sed 's?B.??'`

            OS_MIN_REL="11.00"
            OS_MIN_COMP="11.00"

            LDPATH_VARNAME=SHLIB_PATH
            ;;

	SunOS_sparc)	
            OS_REL=`uname -r`
            OS_REL_COMP=`echo $OS_REL |sed 's?\.??'`

            OS_MIN_REL="5.5"
            OS_MIN_COMP="55"

            LDPATH_VARNAME=LD_LIBRARY_PATH
            ;;

	SunOS_i386)	
            OS_REL=`uname -r`
            OS_REL_COMP=`echo $OS_REL |sed 's?\.??'`

            OS_MIN_REL="5.6"
            OS_MIN_COMP="56"

            LDPATH_VARNAME=LD_LIBRARY_PATH
            ;;

       Linux)  
            OS_REL=`uname -r`
            OS_REL_1=`echo $OS_REL | cut -d. -f,1`
            OS_REL_2=`echo $OS_REL | cut -d. -f,2`
            OS_REL_3=`echo $OS_REL | sed 's?\-.*??' | cut -d. -f,3`
            OS_REL_COMP=`expr $OS_REL_1 \* 1000 + $OS_REL_2 \* 100 + $OS_REL_3`

            OS_MIN_REL="2.0.34"
            OS_MIN_COMP="2034"

            LDPATH_VARNAME=LD_LIBRARY_PATH
            ;;
 
       *)   echo "This O/S is not supported"
            exit 1
            ;;
    esac			

    if [ "$OS_REL_COMP" -lt "$OS_MIN_COMP" ]
    then
	echo "Error: 
	Current  O/S release level: ${OS_REL}
	Required O/S release level: ${OS_MIN_REL}\n
	The current O/S level is less then required for running 
	$PACKAGE_NAME.  You will need to upgrade the 
	O/S before attempting to run this product.\n"
        echo "Setup will now terminate without installing $PACKAGE_NAME.";
    fi
    false
}


check_os_options()
##################
{
    true
}

exit_setup_question()
#####################
{
    echo "You have chosen to exit setup. Are you sure? [Y/N]"
    read JUNK
    case $JUNK in 
    	yes | Yes | y | Y )
		exit 0
		;;
    	* )
    esac
}


welcome_screen() 
################
{
    clear
    cat <<-WELCOME
	Welcome to $PACKAGE_NAME Setup program. 

	This program will install $PACKAGE_NAME on your computer.

	WARNING: This software is protected by copyright law and international treaties.

	Unauthorized reproduction or distribution of this program, or any portion of 
	it, may result in severe civil and criminal penalties, and will be prosecuted 
	to the maximum extent possible under law.
	    
WELCOME

    echo "Press ENTER to continue"
    read INP
    clear

    more <<-LICENSE
	IMPORTANT NOTICE.

	Before installing or using this software, please read the Sybase License
	Agreement included in this product package.

	THE ENCLOSED SOFTWARE IS PROVIDED SUBJECT TO THE TERMS OF THE ACCOMPANYING
	SYBASE LICENSE AGREEMENT.  You indicate your acceptance of such Sybase
	License Agreement by installing and using the enclosed software unless you
	have already agreed to license terms for this software by executing a
	separate written license agreement with Sybase or one of its subsidiaries
	or authorized resellers. 

	Copyright 1988-2000 Sybase Inc. All rights preserved. Unpublished rights
	reserved under U.S. copyright laws.

	This software contains confidential and trade secret information of 
	Sybase, Inc.  Use, duplication or disclosure of the software and documentation 
	by the U.S. Government is subject to restrictions set forth in a license 
	agreement between the Government and Sybase, Inc. or other written agreement 
	specifying the Government \'s rights to use the software and any applicable FAR 
	provisions, for example, FAR 52.227-19.

	Sybase, Inc. 6475 Christie Avenue, Emeryville, CA 94608, USA


	Do you accept all the terms of the accompanying License Agreement?  If you
	choose No, setup will close.  To install $PACKAGE_NAME,
	you must accept this agreement.
LICENSE

   echo "Press Y to accept or N to refuse"
    read ACCEPT_LICENSE

    case $ACCEPT_LICENSE in 
        yes | Yes | y | Y  )
		;;
 	* )
	    echo "You have chosen to not agree to the License Agreement.";
	    echo "Setup will now terminate without installing $PACKAGE_NAME.";
	    exit 0
    esac
}



get_dest_dirs() 
###############
{
   DONE=false
   ASANY_DIR=


   while [ "$DONE" = "false" ]; do 
       echo 
       echo "Where is your current installation of Adaptive Server Anywhere? "
       echo "Default location is : $DEFAULT_INSTALLATION_DIR."

       read DEST_DIR
       if [ "_$DEST_DIR" = "_" ]; then
	  DEST_DIR=$DEFAULT_INSTALLATION_DIR
       fi
       if [ -d "$DEST_DIR" ]; then
          DONE=true 
	  ASANY_DIR=$DEST_DIR
       else 
	  echo "The directory '$DEST_DIR' does not exist! Please try again."
       fi
   done

   ASANY_DIR=$DEST_DIR 

   echo 
   echo "Do you want to rename updated files? "
   read JUNK

   case "$JUNK" in 
   	yes | Yes | y | Y )
		echo "A rename suffix can be used to rename old copies."
		echo "(e.g. old version of libdblib7.so will be renamed to libdblib7.so.<your_suffix>)"
		echo "Note that you need to have enough disk space to keep old copies around!."
		echo "Enter rename suffix that you would like to use :  "
		read RENAME_PREFIX
		RENAME_CMD="-tr $RENAME_PREFIX"
		;;
        * )
		RENAME_PREFIX=
		RENAME_CMD="-tw"
		;;
   esac


}


check_installation()
####################
{
    echo "Checking existing installation ... "
    REGKEY_OPTIONS=`dbinstall -q -c -r $ASANY_DIR $TICFILE`

    check_regkey_options
    check_install_options

    echo "completed."
    echo "Press Enter to continue"
    read NOTHING

}

get_parent()
############
# PRE : $1 directory to get it's parent. 
# POST: echos the least parent directory that exists, or nothing if $1 is empty
# OVERWRITES: JUNK
{
    if [ "_$1" = "_" ]; then
	echo ''
    fi 	

    JUNK=$1
    while [ ! -d $JUNK ]; do 
        JUNK=`dirname $JUNK`
    done
 
    echo $JUNK 
}


get_mount()
###########
# $1 directory
{
   JUNK=`get_parent $1` 

   # df -kP $JUNK | egrep -v "^Filesystem" | sed "s/ */-/g" |sed "s/\-\-/ /g" | sed "s/\-//g" | cut -d" " -f,6
   df -kP $JUNK | egrep -v "^Filesystem" | tr -s ' ' | cut -d" " -f,6
}

get_freespace()
###############
# PRE : $1 is the name of the directory to check for disk space.
# POST: echos free disk space used by parent of $1
# OVERWRITES: JUNK
{
    if [ "_$1" = "_" ]; then
    	echo "0"
    fi

    JUNK=`get_parent $1`

    # TODO: is there a better way?
    df -kP $JUNK | egrep -v "^Filesystem" | tr -s ' ' | cut -d" " -f,4
}

bit_and () 
##########
# $1 : number
# $2 : mask
{
  expr $1 / $2 - 2 \* \( $1 / $2 / 2 \)
}

check_regkey_options()
##################
{
    if [ $REGKEY_OPTIONS = "-2" ]; then
        echo
        echo "ERROR: Version mismatch."
        echo "Your current version does not appear to be 6.0.4"
        echo "Please verify the location of your 6.0.4 install, and try again."
        exit 1
    fi

    num_options=0

    if [ "`bit_and $REGKEY_OPTIONS 131072`" = "1" ]; then
        OPT_DBREMOTE=" X "
 	num_options=`expr $num_options + 1`
    fi

    if [ "`bit_and $REGKEY_OPTIONS 65536`" = "1" ]; then
	OPT_REPAGENT=" X "
 	num_options=`expr $num_options + 1`
    fi

    if [ "`bit_and $REGKEY_OPTIONS 32768`" = "1" ]; then
	OPT_N_CLIENT=" X "
 	num_options=`expr $num_options + 1`
    fi

    if [ "`bit_and $REGKEY_OPTIONS 16384`" = "1" ]; then
	OPT_P_SERVER=" X "
 	num_options=`expr $num_options + 1`
    fi
 
    if [ "`bit_and $REGKEY_OPTIONS 8192`" = "1" ]; then
	OPT_N_SERVER=" X "
 	num_options=`expr $num_options + 1`
    fi

    if [ "$num_options" = "0" ]; then
        echo
        echo "ERROR: Could not find any valid file(s) to update!"
        echo
        clear_signal_handler
        exit 1
    fi
}



create_new_tmpfile()
####################
# PRE : none
# POST: new empty file $TMPFILE created.
{
    if [ "_$TMPFILE" != "_" ] && [ -f $TMPFILE ]; then
    	rm -f $TMPFILE
    fi

    TMPFILE="${TMPPREFIX}$$"

    touch $TMPFILE
}

valid_pid()
##########
{
    # $1 : pid
    kill -s 0 $1 > /dev/null 2>&1
    echo $?
}

create_directory()
#################
# 1: variable to check
# 2: directory to create if needed
{
    JUNK='$'$1 
    JUNK=`eval echo $JUNK`
    if [ "$JUNK" = "true" ]; then
        mkdir -p $2  > /dev/null 2>&1 
    fi
    
    # verify that the directory exists
    if [ -d $2 ]; then
        true
    else
        false
    fi
}

is_numeric()
############
# $1: argument to check
{
    if [ "_$1" != "_" ] && [ "_`echo "$1" |grep "[^0-9]"`" = "_" ]; then 
    	true
    else 
    	false
    fi	
}

post_start_installation()
########################
{
    clean_up
}

pre_start_installation()
########################
{
    # create file list
    generate_filelist
}

start_installation()
####################
{
    clear
    echo 
    echo "Setup will now begin to install files."
    echo 
    echo "Please hit ENTER to start."
    read JUNK

    LOG_FILE="$ASANY_DIR/ebf_`date +%Y%m%d-%H%M`.log"

    PWD=`pwd`
    cd $ASANY_DIR
    dbinstall -V -q -tx -r $ASANY_DIR $RENAME_CMD -O $LOG_FILE -tl $TMPFILE $TICFILE | /bin/tar -xf -
    if [ "`tail -1 $LOG_FILE | grep ok`" = "" ]; then
        # something went wrong
        echo "An error occured while attempting to extract files in $ASANY_DIR."
        echo "Please refer to the log file '$LOG_FILE' for more information on the error."
        echo
    else
        echo "The file '$LOG_FILE' contains the List of files installed under '$ASANY_DIR'."
        echo "completed"
        echo
    fi
    cd $PWD
 
}

check_ticfile()
###############
{
    if [ "`dbinstall -q -tv $TICFILE`" != "0" ]; then
        echo 
	echo "ERROR: The $TICFILE seems to be corrupted."
	echo "Please report this problem to Sybase Technical Support."
	exit 1
    fi
    JUNK=`dbinstall -version 2>&1 | sed 2d | cut -d. -f4`
    PACKAGE_NAME="$PACKAGE_NAME($JUNK)" 
}


check_system_dep()
##################
{
    if [ ! -x /bin/tar ]; then
        echo
        echo "The install software needs /bin/tar to proceed with the installation."
        echo "Please make sure you have this utility installed on your system and try again."
        exit 1
    fi

}


install_completed()
###################
{
    echo 
    echo "Setup has completed!"
    echo 
    echo "Thank you for using Sybase products."
    echo 
}


########################
# mainline 
########################

initialize_variables
set_signal_handler
check_os
check_ticfile
check_system_dep

welcome_screen


get_dest_dirs
check_installation

pre_start_installation
start_installation
post_start_installation


install_completed
clear_signal_handler
