#!/bin/sh

# $Id: cdinstall 59357 2010-10-12 17:34:29Z merge $

echo " "
echo "Micro Focus extend(R) Unix CD install script."
echo " "

current_version=900
installdir=/opt/acucorp/$current_version
TmpListFile=/tmp/listfile$$
TmpAOFile=/tmp/aofile$$

# How can we echo without displaying a newline?
if [ `echo -n "This is a test" | wc -l` = "0" ]; then
    n="-n"
    c=
else
    if [ `echo "This is a test" \\\\c | wc -l` = "0" ]; then
	n=
	c="\\c"
    else
	n=
	c=
    fi
fi

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

# Functions:

#
# A function to get a Y or N answer from the user.
#
ansyn()
{
    while true; do
	echo $n "$1" $c
	read ans
	if [ "$ans" = "" ]; then
	    ans=$2
	fi
	case $ans in
	  y|Y)
	    cmd=true
	    break
	    ;;
	  n|N)
	    cmd=false
	    break
	    ;;
	  *)
	    echo "Please answer \"Y\" or \"N\""
	    ;;
	esac
    done
    eval $cmd
}


#
# Create an entire directory hierarchy.
#
MakeDirHier()
{
    if [ $# -ne 1 ]; then
	echo "Internal error.  Wrong number of arguments to MakeDirHier."
	exit 1
    fi
    localdir=`dirname $1`
    if [ ! -d $localdir ]; then
	MakeDirHier $localdir
    fi
    if [ ! -d $1 ]; then
	mkdir $1
    fi
}


#
# Find a file on disk, case independently.
# First argument is type (file, dir)
# Second argument is the name to search for
# This will "echo" the final value to return the actual name
#
FindFile()
{
    if [ $# -ne 2 ]; then
	echo "Internal error.  Wrong number of arguments to FindFile."
	exit 1
    fi
    for file in *; do
	foundfile=`echo $file | grep -i $2 2> /dev/null`
	if [ ! -z "$foundfile" ]; then
	    case $1 in
	      file)
		if [ -f $file ]; then
		    echo $file
		    break
		fi
		;;
	      dir)
		if [ -d $file ]; then
		    echo $file
		    break
		fi
		;;
	      *)
		echo "Internal error.  Invalid argument to FindFile: $1"
		echo "Please contact Micro Focus."
		exit 1
		;;
	    esac
	fi
    done
}


#
# GetPiece - Extract the numbered field from the passed string
# Arguments:  $1 = separator
#             $2 = piece number
#             $3 = string to extract from
# This echos its result.
#
GetPiece()
{
    if [ $# -ne 3 ]; then
	echo "Internal error.  Wrong number of arguments to GetPiece."
	exit 1
    fi
    echo $3 | awk -F${1} "{print \$$2}"
}


#
# ListContains pattern source source source
# - returns true if one of source == pattern
# - returns false otherwise
#
ListContains()
{
    if [ $# -lt 1 ]; then
	echo "Internal error.  Wrong number of arguments to ListContains."
	exit 1
    fi
    if [ $# -lt 2 ]; then
	false
	return
    fi
    pattern="$1"
    shift
    for x in "$@"; do
	if [ "$pattern" = "$x" ]; then
	    true
	    return
	fi
    done
    false
}


#
# Cleanup time...
#
Cleanup()
{
    rm -f $TmpListFile $TmpAOFile
    trap 0 1 2
}


#
# Process the keyword list
#
ProcessKeywordList()
{
    eval AllKeywordsList=\$$Prod
    for KeywordsList in $AllKeywordsList; do
	val=1
	while true; do
	    KeywordValue=`GetPiece : $val $KeywordsList`
	    if [ "$KeywordValue" = "" ]; then
		break
	    fi
	    Keyword=`GetPiece = 1 $KeywordValue`
	    Value=`GetPiece = 2 $KeywordValue`
	    val=`expr $val + 1`
	    case $Keyword in
	      lst)
		if [ "$ListFileList" = "" ]; then
		    ListFileList=$Value
		else
		    ListFileList="$ListFileList $Value"
		fi
		;;
	      *)
		echo "Unknown Keyword: $Keyword"
		;;
	    esac
	done
    done
}


#
# Avoid overwriting destination files
#
AvoidOverwrite()
{
    if [ $# -ne 2 ]; then
	echo "Internal error.  Wrong number of arguments to AvoidOverwrite."
	exit
    fi

    dir="$1"
    list="$2"

    existing=0
    rm -f $TmpAOFile
    for file in `cat $list`; do
	if test -f "$dir/$file"; then
	    existing=1
	    echo "$dir/$file" >> $TmpAOFile
	fi
    done

    if test $existing -eq 1; then
	echo "Warning: some destination files already exist!"
	if ansyn "Do you want to see a list of the existing files? [N] " N; then
	    echo " "
	    cat $TmpAOFile
	fi
	rm -f $TmpAOFile
	echo " "
	if ansyn "Do you want to overwrite these files? [Y] " Y; then
	    echo " "
	    return 0
	fi
	return 1
    fi

    return 0
} # AvoidOverwrite


# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

# Find important (and possibly mangled) file and directory names.
archives=`FindFile dir archives`
instinfo=`FindFile dir instinfo`
archlist=`FindFile file archlist`

if [ -z "$instinfo" -o -z "$archives" -o -z "$archlist" ]; then
    echo "   *** Install cannot proceed because it cannot find necessary"
    echo "   *** files.  It is important that you run the script from its"
    echo "   *** original directory on the CD.  The script will not work"
    echo "   *** if copied to or run from another location.  If this"
    echo "   *** message persists please contact Micro Focus."
    echo " "
    exit 1
fi

# Get platform info.
echo "Begin by specifying operating system information."

plat_num=0
max_plat=`wc -l < $archlist | sed 's/^ *//'`
split=17
first_half=1
while [ $plat_num -eq 0 ]; do
    echo "Please enter the number corresponding to your platform:"
    echo " "
    if [ $max_plat -le $split ]; then
	awk -F'	' '{ print NR " " $2 }' $archlist
    else 
	if [ $first_half -eq 1 ]; then
	    awk -F'	' 'NR <= '$split' { print NR " " $2 }' $archlist
	    echo " "
	    echo "Press 'Enter' for the second part of the list."
	else
	    awk -F'	' 'NR > '$split' { print NR " " $2 }' $archlist
	    echo " "
	    echo "Press 'Enter' for the first part of the list."
	fi
    fi
    echo " "
    echo $n "Your selection? > " $c
    read plat_num
    echo " "

    if [ "$plat_num" = "" ]; then
	plat_num=0
    fi
    if [ $plat_num -lt 1 -o $plat_num -gt $max_plat ]; then
	plat_num=0
	first_half=`expr 1 - $first_half`
    fi
done

# feedback for the user
plat_info=`awk -F'	' 'NR == '$plat_num' { print $2 }' $archlist`
echo "You have selected number "$plat_num": $plat_info."
echo " "

# Unmangle name of tarfile.
platform_name=`sed -n "${plat_num}s/[ 	].*//p" $archlist`

cd $archives
tarname=`FindFile file $platform_name.tar`
cd ..

if [ -z "$tarname" ]; then
    echo "   *** Installation cannot proceed because the file"
    echo "   *** '$archives/$platform_name.tar' is missing."
    echo "   *** Please contact Micro Focus."
    echo " "
    exit 1
fi

cd $instinfo
platname=`FindFile dir $platform_name`
cd ..

if [ -z "$platname" ]; then
    echo "   *** Installation cannot proceed because the directory"
    echo "   *** '$instinfo/$platform_name' is missing."
    echo "   *** Please contact Micro Focus."
    echo " "
    exit 1
fi

# Get install directory.
echo "Now specify an install directory.  The default install directory is:"
echo "	$installdir."
echo "Please enter another directory or <CR> to accept the default."
echo " "
echo $n "> " $c
read install_entry
if [ "$install_entry" != "" ]; then
    installdir="$install_entry"
fi
echo " "
echo "Products will be installed in: $installdir."
echo " "

# See if $installdir exists.  If not create it.
if [ ! -d "$installdir" ]; then
    echo "$installdir does not exist.  Creating..."
    MakeDirHier "$installdir"
    if [ -d "$installdir" ]; then
	echo "$installdir has been created."
	echo " "
    else
	echo "   *** Could not create $installdir."
	echo "   *** Installation unsuccessful."
	echo " "
	exit 1
    fi
fi

# Load information about this version's bundles/products.
# First unmangle name of 'bndlinfo' file.
cd $instinfo/$platname
bndlinfo=`FindFile file bndlinfo`

if [ -z "$bndlinfo" ]; then
    echo "   *** Installation cannot proceed because the file"
    echo "   *** '$instinfo/$platname/bndlinfo' is missing."
    echo "   *** Please contact Micro Focus."
    echo " "
    exit 1
fi

if [ -s $bndlinfo ]; then
    . ./$bndlinfo
else
    echo "   *** Installation cannot proceed because the file"
    echo "   *** '$instinfo/$platname/$bndlinfo' is empty."
    echo "   *** Please contact Micro Focus."
    echo " "
    exit 1
fi
cd ../..

echo "Now we will determine which products to install.  Answer \"Y\" for each"
echo "product you wish to install.  You will be asked later for the license"
echo "code and key for each product you selected.  If you do not have these"
echo "codes and keys the product will not execute, so you should only install"
echo "products for which you have a license code and key."
echo " "

# list of bundles installed
BundleList=
# list of products to install, including those from bundles
ProductList=

# Find the product bundles for this installation.
AllBundles=`echo $Bundles | sed 's/[ ]*Custom//'`
echo "Available products are:"
for Bundle in $AllBundles; do
    eval name="\$${Bundle}_name"
    echo "	$name"
done
echo " "
echo "For each product, please answer \"Y\" or \"N\":"
for Bundle in $AllBundles; do
    eval name="\$${Bundle}_name"
    if ansyn "Do you want to install ${name}? [N] " N; then
	BundleList="$BundleList $Bundle"

	eval bundleprods="\$${Bundle}"

	for prod in $bundleprods; do
	    if ListContains $prod $ProductList; then
		:;
	    else
		ProductList="$ProductList $prod"
	    fi
	done
    fi
done
echo " "

if [ "$ProductList" = "" ]; then
    echo "You did not select any products to install."
    echo " "
else
    # Set trap.
    trap "Cleanup; exit" 0 1 2
    rm -f $TmpListFile

    echo $n "Gathering file lists..." $c
    ListFileList=
    for Prod in $ProductList; do
	ProcessKeywordList
    done

    # Gather listfiles.
    if [ ! -z "$ListFileList" ]; then
	cd $instinfo/$platname
	for I in $ListFileList; do
	    ListFile=`FindFile file $I`
	    if [ -n "$ListFile" ]; then
		cat $ListFile >> $TmpListFile
	    else
		echo "   *** Installation cannot proceed because file"
		echo "   *** '$instinfo/$platname/$I' is missing."
		echo "   *** Please contact Micro Focus."
		echo " "
		exit 1
	    fi
	done
	cd ../..
    fi

    sort $TmpListFile | uniq > ${TmpListFile}.uniq
    mv ${TmpListFile}.uniq $TmpListFile
    echo "Done."
    echo " "

    if AvoidOverwrite $installdir $TmpListFile; then
	:
    else
	echo " "
	echo "Installation terminated."
	echo " "
	exit 1
    fi

    # Untar the desired files.
    echo "Extracting all relevant files from ${tarname}..."
    if cat $archives/$tarname | \
	    (cd "$installdir"; tar xvf - `cat $TmpListFile`);
    then
	echo "Done."
	echo " "
    else
	echo "   *** Error extracting files from $tarname."
	echo "   *** Installation unsuccessful."
	echo " "
	exit 1
    fi

    echo "Installation to $installdir has completed successfully."
    echo " "

    echo "We now need to activate your licenses.  In order to activate the"
    echo "licenses, we must run an executable from the port that you just"
    echo "installed.  This won't work unless the port you just installed is"
    echo "compatible with the current operating system."
    echo " "

    echo "Is the port you just installed compatible with the current"
    if ansyn "operating system? [Y] " Y; then
	compatible=1
    else
	compatible=0
    fi
    echo " "

    # enable activator to find any shared libraries it may require
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$installdir/lib"
    export LD_LIBRARY_PATH
    LD_LIBRARY_PATH_64="${LD_LIBRARY_PATH_64}:$installdir/lib"
    export LD_LIBRARY_PATH_64
    LIBPATH="${LIBPATH}:$installdir/lib"
    export LIBPATH
    SHLIB_PATH="${SHLIB_PATH}:$installdir/lib"
    export SHLIB_PATH
    DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}:$installdir/lib"
    export DYLD_LIBRARY_PATH

    if [ $compatible -eq 0 ]; then
	echo "Okay, you will need to run $installdir/bin/activator by hand"
	echo "once the files are transferred to a compatible system."
    elif [ -f "$installdir/bin/activator" ]; then
	currdir=`pwd`
	cd "$installdir"
	failure=0
	for Bundle in $BundleList; do
	    while true; do
		echo " "
		eval name="\$${Bundle}_name"
		echo "Activating license for ${name}:"
		if ./bin/activator; then
		    failure=0
		    break
		else
		    if ansyn "License activation failed.  Try again? [Y] " Y;
		    then
			failure=0
		    else
			failure=1
			break 2
		    fi
		fi
	    done
	done
	if [ $failure -eq 1 ]; then
	    echo " "
	    echo "One or more license activations has failed.  You will need"
	    echo "to run $installdir/bin/activator by hand in order to"
	    echo "activate your licenses.  Please contact Micro Focus Technical"
	    echo "Support for assistance if necessary."
	fi
	cd "$currdir"
    else
	echo "The license activator program $installdir/bin/activator is"
	echo "missing.  You need to obtain licenses from some other source."
	echo "Please contact Micro Focus."
    fi
fi

Cleanup

echo " "
echo "All done."
exit 0
