#!/bin/sh
#
# Sonnet Software INSTALLATION PROGRAM
#
# Sonnet Software, Inc.
# (C) COPYRIGHT 1993. ALL RIGHTS RESERVED.
#
# Version 1.1
# Sat Jun 26 10:19:54 EDT 1993
#
#
# This program performs the following functions:
#	1. Verifies that this program is being run from the sonnet directory
#	2. Ask the user if this is an upgrade or not.
#	3. Change permissions on files


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

TMP_FILES="
	$ANS
	$RUN_UNLOCKEM
	$CFG
"

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=sonnet.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

####
####    Make sure nocr and testem are executable
####
chmod 755 bin/testem 2> /dev/null
chmod 755 bin/nocr 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


###
###	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

###
###     Record the directory where old software is stored.
###
DATE=`date`
sed "s/InstallDate:.*/InstallDate: $DATE/" sonnet.ver > $CFG
cp $CFG sonnet.ver 
if [ $? -ne 0 ] 
then 
	print_msg ERROR_UPDATE_OLD_SONNET_RELEASE 
fi 
 

print_msg MSG_DONE

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

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

print_msg MSG_RUN_TESTEM

if [ -f $RUN_UNLOCKEM ]
then
	print_msg MSG_RUN_UNLOCKEM
fi

rm -f $TMP_FILES
