#!/bin/sh
#
# Sonnet Software INSTALLATION PROGRAM
#
# Sonnet Software, Inc.
# (C) COPYRIGHT 1991. ALL RIGHTS RESERVED.
#
# Version 1.0
# Thu Sep 19 09:52:10 EDT 1991
#
#
# This program performs the following functions:
#	1. Verifies that this program is being run from the sonnet directory
#	2. Verify that there is a .key file
#	3. Ask the user if this is an upgrade or not.  If so see if the
#	   .key file has 120 subsection limit in it.
#	3. Change permissions on files
#	4. Verify installation by running testem


TMP=/tmp/$$
ANS=${TMP}ans
RUN_UNLOCKEM=${TMP}rununlock

TMP_FILES="
	$ANS
	$RUN_UNLOCKEM
"

trap '
	# change installem in case user stop program while fixing permissions
	chmod 755 installem 2> /dev/null
	rm -f $TMP_FILES 
	exit 1
' 1 2 3 14 15

KEYFILE=.key; export KEYFILE

###
###	Print out heading information
###

print_msg HEADER $VERSION

###
###	Execute installem.pre if it exists
###
if [ -f bin/install/installem.pre ]
then
	sh bin/install/installem.pre
	if [ $? -ne 0 ]
	then
		rm -f $TMP_FILES
		exit 1
	fi
fi

###
### Before starting the installation, let make sure that we can:
###	1. Can create files in present directory 
###	2. change permission of files in present directory
###	NOTE: change not check if directory is executable, since this is
###	      not supported on the apollo.
###
if [ ! -w . ]
then
	print_msg ERROR_NO_CREATE
	rm -f $TMP_FILES
	exit 1
	
fi

chmod 755 bin/testem 2> /dev/null
if [ $? -ne 0 ]
then
	print_msg ERROR_NO_CHANGE_PERM
	rm -f $TMP_FILES
	exit 1
fi

###
###	Ask if user is doing an upgrade
###

ask_yes "Are you upgrading the software?" $ANS HELP_UPGRADE
if [ $? -ne 0 ]
then
	rm -f $TMP_FILES
	exit 1
fi
UPGRADE=`cat $ANS`

###
###	If user want to 
###
###
###	If key exists and user is doing an upgrade check to see if there
###	is a 120 subsection limit on the software
###
if [ "$UPGRADE" = TRUE ]
then
	> $RUN_UNLOCKEM
	upgrade_em $RUN_UNLOCKEM
	if [ $? -ne 0 ]
	then
		rm -f $TMP_FILES
		exit 1
	fi
fi


###
###	Make sures that license.ok files exists.
###
if [ ! -f license.ok ]
then
	cat LICENSE \
		| sed -n "1,/If you agree/p" \
		| sed "\$"d 
	ask_yes "Do you agree to these term?" $ANS HELP_LICENSE
	if [ $? -ne 0 ]
	then
		rm -f $TMP_FILES
		exit 1
	fi
	if [  "`cat $ANS`" = "TRUE" ]
	then
		date > license.ok
	else
		print_msg ERROR_DISAGREE_WITH_LICENSE
		rm -f $TMP_FILES
		exit 1
	fi
fi

###
###	Change permission of all of the installed files
###

ERROR=FALSE
echo
echo "-> Setting permissions"
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
chmod 755 bin/*
chmod 755 bin/install/*
chmod 755 installem sonnetsetup unlockem doctor

###
###	Execute installem.post if it exists
###
if [ -f bin/install/installem.post ]
then
	sh bin/install/installem.post
	if [ $? -ne 0 ]
	then
		rm -f $TMP_FILES
		exit 1
	fi
fi

###
### Installation is completed - remove installem.flg
###

if [ -f data/installem.flg ]
then
	rm data/installem.flg
	if [ $? -ne 0 ]
	then
		print_msg ERROR_NO_RM_INSTALLEM_FLG
	fi
fi

print_msg MSG_DONE

###
###	Check if there is a $KEYFILE file
###

if [ ! -f $KEYFILE ]
then
	print_msg ERROR_KEYFILE_NOT_FOUND
	rm -f $RUN_UNLOCKEM
else
	grep "Em	subs	120" .key > /dev/null 2> /dev/null
	if [ $? -eq 0 ]
	then
		> $RUN_UNLOCKEM
	fi
fi

if [ -f $RUN_UNLOCKEM ]
then
	print_msg MSG_RUN_UNLOCKEM
fi

print_msg MSG_RUN_TESTEM

rm -f $TMP_FILES
