#! /bin/sh
#
# ss-install
#
# Written by Mark Harris.
# Copyright 1993 Design Acceleration, Inc.
#
# Usage: ss-install [ signalscan-tar-file ]
#
# $Id:$
#
# $Log:$
#

dir_exe_def=/usr/local/bin
dir_man_def=/usr/local/man
dir_other_def=/usr/local/lib/signalscan

umask 022

part_list=""
rm_list=""
trap 'echo " "; rm -rf $rm_list; exit 1' 0 1 2 3 15

if [ "$#" -eq 0 ]; then
    set - signalscan*.tar*

    if [ "$#" -gt 1 ]; then
	echo "There are multiple signalscan tar files in the current directory."
	echo "Please re-execute this script and specify the desired file as an argument."
	echo "For example:  $0 $1"
	exit 1
    fi

    src_file="$1"
    if [ ! -f "$src_file" ]; then
	set - signalscan.part.[0-9][0-9]
	if [ "$#" -le 1 ]; then
	    cat <<EOM
Cannot find the signalscan distribution.

If you received the distribution in multiple parts, make sure that the files
are named signalscan.part.nn, where nn is a two digit number, and are located
in the current directory.  Otherwise, make sure that the signalscan tar file
has been extracted from the distribution media and execute this script in
the directory where it was extracted.
EOM
	    exit 1
	fi

	if [ -w . ]; then
	    create_dir=""
	else
	    create_dir=/tmp/
	fi

	dist_file=ss-total-$$
	rm -rf ${create_dir}${dist_file} ${create_dir}signalscan*.tar*

	echo "Putting together the signalscan distribution - please wait..."

	# make sure that we have all the parts and put them together
	part_list="signalscan.part.00"
	part_num=01
	rm_list="${create_dir}${dist_file}"
	>${create_dir}${dist_file}
	while [ -f "signalscan.part.${part_num}" ]
	do
	    if [ ! -r "signalscan.part.${part_num}" ]; then
		rm -rf $rm_list
		cat <<EOM

You do not have read permission to all parts of the signalscan distribution.
Please correct the problem and re-execute this script.
EOM
		exit 1
	    fi
	    if grep "^start of part ${part_num}$" \
		    < signalscan.part.${part_num} >/dev/null 2>&1; then
		(echo " "; cat signalscan.part.${part_num}) | \
		sed -e "1,/^start of part ${part_num}$/d" \
		    -e "/^end of part ${part_num}$/,"\$d \
		    >> ${create_dir}${dist_file}
	    else
		rm -rf $rm_list
		cat <<EOM

The file signalscan.part.${part_num} does not appear to contain part ${part_num} of
the signalscan distribution.  Each file should contain lines of the form
'start of part nn' and 'end of part nn', where nn is the two digit part number
from the file name.  Please check to make sure that you have used the correct
number for each file, and rename any files that have incorrect names.  Then
re-execute this script.
EOM
		exit 1
	    fi
	    part_list="${part_list} signalscan.part.${part_num}"
	    part_num=`expr substr \( $part_num + 101 \) 2 2`
	done

	if [ ! -w . ]; then
	    part_list=""
	fi

	if grep '^end$' ${create_dir}${dist_file} >/dev/null 2>&1; then
	    :
	else
	    rm -rf $rm_list
	    cat <<EOM

I can't seem to find part ${part_num} of the signalscan distribution.  Please
check to make sure that you have all parts and that they are all in the
current directory, and then re-execute this script.  The last part should have
an 'end' line just before the 'end of part nn' line at the end of the file.
EOM
	    exit 1
	fi

	if (cd ${create_dir}.; uudecode < ${dist_file}); then
	    :
	else
	    rm -rf $rm_list
	    cat <<EOM

The signalscan distribution could not be uudecoded.  Please correct the
problem and re-execute this script, or contact Design Acceleration for
assistance.
EOM
	    exit 1
	fi

	rm -rf $rm_list
	rm_list=""
	echo " "

	set - ${create_dir}signalscan*.tar*
	src_file="$1"
	if [ "$#" -ne 1 -o ! -f "${src_file}" ]; then
	    cat <<EOM
Can't find the signalscan tar file after uudecoding.
Please contact Design Acceleration for assistance.
EOM
	    exit 1
	fi
    fi
elif [ "$#" -eq 1 ]; then
    src_file="$1"
    if [ ! -f "$src_file" ]; then
	echo "$0: Cannot find ${src_file}." >&2
	exit 1
    fi
else
    echo "Usage: $0 [ signalscan-tar-file ]"
    exit 1
fi

if [ ! -r "$src_file" ]; then
    cat <<EOM
You do not have read permission to ${src_file}.

Please correct the problem and try again, or contact Design Acceleration for
assistance.
EOM
    exit 1
fi

if [ -w . ]; then
    if [ -d "ss-dist" ]; then
	src_dir=ss-dist$$
    else
	src_dir=ss-dist
    fi
else
    src_dir=/tmp/ss-dist$$
fi

rm_list="$src_dir"

#
#  Unpack distribution
#
if mkdir $src_dir; then
    :
else
    rm_list=""
    cat <<EOM
I can't unpack the signalscan distribution in this directory.

Please move this script and the signalscan distribution to a directory
where it can be unpacked, and execute this script from there.
EOM
    exit 1
fi

case "$src_file" in
*.Z|*.z|*.gz)
    echo "Uncompressing distribution - please wait..."
    src_cat=zcat
    ;;
*)
    src_cat=cat
    ;;
esac

#
#  Unpack distribution
#
if ($src_cat "$src_file" | (cd $src_dir; tar xf -)); then
    :
else
    rm -rf $rm_list
    cat <<EOM

Could not unpack signalscan distribution ${src_file}.
Please contact Design Acceleration for assistance.
EOM
    exit 1
fi

sub_dir=`echo $src_dir/signalscan*`
if [ -d "$sub_dir" ]; then
    src_dir="$sub_dir"
fi

# make sure that we now have the distribution
if [ ! -f "${src_dir}/signalscan" -o ! -f ${src_dir}/README ]; then
    rm -rf $rm_list
    cat <<EOM

The file ${src_file} does not seem to contain the complete
signalscan distribution, or it was not unpacked properly.
Please contact Design Acceleration for assistance.
EOM
    exit 1
fi

rm -rf ${part_list}

cat <<EOM


This script will install signalscan and related files.  By default, the
signalscan executable will be installed in ${dir_exe_def}, the manual
pages will be installed in subdirectories under ${dir_man_def}, and all
other files including the license manager daemon and sample files will
be installed in ${dir_other_def}.  You will be given a chance
to choose different directories.  All questions have default answers
which are displayed in square brackets.  Press return to accept the
default answer for any question.

EOM
if [ "x`whoami`" = "xroot" ]; then
    root_user=yes
    cat <<EOM
Since you have executed this script as super-user, you should be able
to install all files in their default directories.

EOM
else
    root_user=no
    cat <<EOM
You did not execute this script as super-user.  Therefore, you will
probably not be able to install the files in the default directories.
If you would like to quit and re-execute this script later, use your
interrupt key (usually Control-C).  If you continue, you can skip steps
that you wish to do later or have already done, or you may install the
files in different directories.

EOM
fi

#
#  Check whether to use -n or \c to inhibit newline.
#
echo "test\c" >/tmp/.echo$$
if grep c /tmp/.echo$$ >/dev/null 2>&1 ; then
	ecn='-n'
	ecc=''
else
	ecn=''
	ecc='\c'
fi
rm -f /tmp/.echo$$

#
#  Get a directory name into shell variable $1.
#  The prompt must have already been printed, and $2 is the default answer.
#
get_directory ()
{
    answer=""
    while [ -z "$answer" ]
    do
	read answer
	case "$answer" in
	~|~/*)
	    answer=`echo "$answer" | sed "s;~;${HOME-$LOGDIR};"`
	    ;;
	~*)
	    answer=`/bin/csh -f -c "glob $answer"`
	    if [ "$?" -ne 0 ]; then
		echo " "
		echo $ecn "Please enter another directory or 'none'. [$2] $ecc"
		answer=""
	    fi
	    ;;
	none|NONE|None)
	    answer=none
	    ;;
	"")
	    answer="$2"
	    ;;
	*)
	    ;;
	esac
	if [ "x$answer" != "x" -a "x$answer" != "xnone" ]; then
	    if [ -d "$answer" ]; then
		if [ "$root_user" != "yes" -a ! -w "$answer" ]
		then
		    echo " "
		    echo "Sorry, you do not have write permission to ${answer}."
		    echo $ecn "Please enter a directory name or 'none'. [$2] $ecc"
		    answer=""
		fi
	    elif [ -f "$answer" ]; then
		echo " "
		echo "$answer is a file, not a directory."
		echo $ecn "Please enter a directory name or 'none'. [$2] $ecc"
		answer=""
	    elif mkdir -p "$answer"; then
		: created it
	    else
		echo " "
		echo "The directory $answer doesn't exist and I can't"
		echo "seem to create it.  Please enter another directory or 'none'."
		echo $ecn "[$2] $ecc"
		answer=""
	    fi
	fi
    done
    eval $1="$answer"
    echo " "
}

#
#  Get a "yes", "no", or "abort" answer into shell variable $1.
#  The prompt must have already been printed, and $2 is the default answer.
#
get_yesno ()
{
    answer=""
    while [ -z "$answer" ]
    do
	read answer
	case "$answer" in
	[yY]*)  answer=yes;;
	[nN]*)  answer=no;;
	[aA]*)  answer=abort;;
	"")     answer="$2";;
	*)
		echo " "
		echo "Please enter 'yes', 'no', or 'abort', or press return to accept the"
		echo $ecn "default answer. [$2] $ecc"
		answer=""
		;;
	esac
    done
    eval $1="$answer"
    echo " "
}


echo "In which directory would you like to install the signalscan executable"
echo $ecn "(enter 'none' if none)? [${dir_exe_def}] $ecc"
get_directory dir_exe $dir_exe_def

while [ "x$dir_exe" != "xnone" ]
do
    if cp "$src_dir/signalscan" "$dir_exe/signalscan"; then
	break
    fi

    echo "Could not install signalscan executable in directory ${dir_exe}."
    echo $ecn "Enter a new directory, or 'none' [${dir_exe_def}] $ecc"
    get_directory dir_exe $dir_exe_def
done
rm -f "$src_dir/signalscan"

cat <<EOM
Manual pages for signalscan and license manager utilities will be installed
in the subdirectory manx of the specified directory, where x is the manual
section (1 or 5).  In which directory would you like to install the manual
EOM
echo $ecn "pages (enter 'none' if none)? [${dir_man_def}] $ecc"
get_directory dir_man $dir_man_def

while [ "x$dir_man" != "xnone" ]
do
    goterr=no
    for sect in 1 5
    do
	if [ -d "${dir_man}/man${sect}" ]; then
	    :
	elif mkdir "${dir_man}/man${sect}"; then
	    :
	else
	    goterr=yes
	    echo "Could not create ${dir_man}/man${sect} subdirectory."
	    break
	fi
	    
	if cp ${src_dir}/*.${sect} "${dir_man}/man${sect}"; then
	    :
	else
	    goterr=yes
	    echo "Could not install manual pages in directory ${dir_man}/man${sect}."
	    break
	fi
    done
    if [ "$goterr" = "yes" ]; then
	echo "Enter a new parent manual page directory, or 'none'."
	echo $ecn "[${dir_man_def}] $ecc"
	get_directory dir_man $dir_man_def
    else
	break
    fi
done
rm -f $src_dir/*.1 $src_dir/*.5

echo "In which directory would you like to install all other signalscan"
echo "files, such as the license manager and sample files (enter 'none'"
echo $ecn "if none)? [${dir_other_def}] $ecc"
get_directory dir_other $dir_other_def

while [ "x$dir_other" != "xnone" ]
do
    if cp $src_dir/* "$dir_other"; then
	break
    fi

    echo "Could not install files in directory ${dir_other}."
    echo $ecn "Enter a new directory, or 'none' [${dir_other_def}] $ecc"
    get_directory dir_other $dir_other_def
done

if [ "x$dir_other" = "xnone" ]; then
    dir_other_sug="$dir_other_def"
else
    dir_other_sug="$dir_other"
fi

case "${dir_other_sug}" in
.|./*)
    cur_dir=`pwd`
    dir_other_sug_full=`echo "${dir_other_sug}" | sed "s;\.;${cur_dir};"`
    ;;
/*)
    dir_other_sug_full="${dir_other_sug}"
    ;;
*)
    dir_other_sug_full="`pwd`/${dir_other_sug}"
    ;;
esac

elmd_dir=''
elmd_path=''
for check_dir in "${dir_other_sug}" "${dir_other_def}" /usr/lib/dai_elm /usr/lib/DAI_elm /usr/lib/elm .
do
    if [ -x ${check_dir}/dai_elmd ]; then
	elmd_dir="${check_dir}"
	elmd_path="${check_dir}/dai_elmd"
	break
    elif [ -x ${check_dir}/DAI_elmd ]; then
	elmd_dir="${check_dir}"
	elmd_path="${check_dir}/DAI_elmd"
	break
    fi
done

if [ -z "${elmd_path}" ]; then
    elmd_sug_dir="${dir_other_sug_full}"
    elmd_sug_path="${dir_other_sug_full}/dai_elmd"
else
    case "${elmd_path}" in
    ./*)
	cur_dir=`pwd`
	elmd_dir=`echo "$elmd_dir" | sed "s;\.;${cur_dir};"`
	elmd_path=`echo "$elmd_path" | sed "s;\.;${cur_dir};"`
	;;
    /*)
	;;
    *)
	cur_dir=`pwd`
	elmd_dir="${cur_dir}/${elmd_dir}"
	elmd_path="${cur_dir}/${elmd_path}"
	;;
    esac

    elmd_sug_dir="$elmd_dir"
    elmd_sug_path="$elmd_path"
fi

if [ -f /etc/rc.local ]; then
    etcrc_file=/etc/rc.local
else
    etcrc_file=/etc/rc
fi

cat <<EOM
The license manager daemon must be running in order to use signalscan.  If
signalscan is used on multiple systems at your site, the license manager
will only need to be run on one system which is designated as the license
server.  Later you may set up multiple license servers for redundancy, if
desired.  The license manager daemon is typically executed at system
startup from ${etcrc_file} with a command such as the following (but on a
single line):

  ${elmd_sug_path} -e ${elmd_sug_dir}
      -m 0.25m -l ${elmd_sug_dir}/elmd.log

See the dai_elmd manual page for details.

EOM

if grep -i dai_elmd "${etcrc_file}" >/dev/null 2>&1 ; then
    cat <<EOM
It looks like you may have already modified ${etcrc_file}.  You may want
to check it out to make sure that the path is accurate.

EOM
elif [ -z "${elmd_path}" ]; then
    cat <<EOM
The license manager daemon (dai_elmd) does not seem to be installed.
Therefore, you will have to add this command to ${etcrc_file} at a later
time, or the license manager daemon will have to be started manually.

EOM
elif [ -w "${etcrc_file}" ]; then
    echo "Would you like the above command to be added to the end of ${etcrc_file} so"
    echo $ecn "that the license manager will be started on this system at boot time? [yes] $ecc"
    get_yesno add_to_rc yes
    if [ "$add_to_rc" = "yes" ]; then
	(echo "#";
	echo "# start the Design Acceleration Elan License Manager";
	echo "#";
	echo "if [ -f ${elmd_path} ]; then";
	echo "	${elmd_path} -e ${elmd_dir} -m 0.25m -l ${elmd_dir}/elmd.log";
	echo "	echo 'Design Acceleration Elan License Manager'";
	echo "fi") >> ${etcrc_file}
    else
	echo "You may add this command to ${etcrc_file} at a later time, or the license"
	echo "manager daemon may be started manually."
	echo " "
    fi
else
    cat <<EOM
You do not have write permission to ${etcrc_file}.  This command may
be added to ${etcrc_file} at a later time, or the license manager daemon
may be started manually.

EOM
fi

if [ -z "${elmd_path}" ]; then
    echo $ecn "Press return to continue. $ecc"
    read answer
else
    echo $ecn "Would you like the license manager daemon to be started now? [yes] $ecc"
    get_yesno start_lic yes

    if [ "$start_lic" = "yes" ]; then
	${elmd_path} -e ${elmd_dir} -m 0.25m -l ${elmd_dir}/elmd.log -s 0 &
	if [ "$?" -ne 0 ]; then
	    echo " "
	    echo "There seems to be a problem starting the license manager daemon now."
	    echo "You'll have to start it yourself later."
	fi
	sleep 2
    fi
fi

cat <<EOM

In order to use signalscan, you must have an activation key.  To obtain
your activation key, send your name, phone number, and server code to
Design Acceleration by e-mail, fax, or phone.

EOM

elmadmin_dir=''
elmadmin_path=''
elmadmin_name=dai_elmadmin

for check_dir in "${dir_other_sug}" "${src_dir}" "${dir_other_def}" /usr/lib/dai_elm /usr/lib/DAI_elm /usr/lib/elm . .. "${src_dir}/.."
do
    if [ -x ${check_dir}/dai_elmadmin ]; then
	elmadmin_dir="${check_dir}"
	elmadmin_path="${check_dir}/dai_elmadmin"
	break
    elif [ -x ${check_dir}/DAI_elmadmin ]; then
	elmadmin_dir="${check_dir}"
	elmadmin_name=DAI_elmadmin
	elmadmin_path="${check_dir}/DAI_elmadmin"
	break
    fi
done

if [ -z "${elmadmin_path}" ]; then
    cat <<EOM

I can't seem to find the dai_elmadmin program.  If you've moved it, you'll
have to run it yourself to find your server code.  When you have your server
code, send it to Design Acceleration.
EOM
    got_code=no
    disp_word=display
else
    case "${elmadmin_dir}" in
    .|./*)
	cur_dir=`pwd`
	elmadmin_dir=`echo "${elmadmin_dir}" | sed "s;\.;${cur_dir};"`
	;;
    /*)
	;;
    *)
	elmadmin_dir="`pwd`/${elmadmin_dir}"
	;;
    esac

    rm_list="$rm_list /tmp/.daisc$$"
    ${elmadmin_path} -e ${elmadmin_dir} -n -x -c -r -1 > /tmp/.daisc$$
    if [ "$?" -ne 0 ]; then
	rm -f /tmp/.daisc$$
	cat <<EOM

I can't seem to get your server code.  You'll have to use the supplied
dai_elmadmin program to display it yourself.  When you have your server
code, send it to Design Acceleration.
EOM
	got_code=no
	disp_word=display
    else
	cat /tmp/.daisc$$
	got_code=yes
	disp_word=redisplay
    fi
fi

cat <<EOM

Send by e-mail to:  sales@designacc.com
or by fax to:       +1 408 356 6249
or call:            +1 408 356 5009

EOM

prompt_cont=yes
if [ $got_code = yes -a -x /usr/lib/sendmail ]; then
    echo "Would you like the above server code to be sent by e-mail to"
    echo $ecn "sales@designacc.com now? [yes] $ecc"
    get_yesno send_mail yes
    send_correct=no
    fullname=""
    phonenumber=""

    while [ "$send_mail" = yes -a "$send_correct" = no ]
    do
	if [ "$send_mail" = yes ]; then
	    old_fullname="${fullname}"
	    if [ "x${fullname}" = "x" ]; then
		def_fullname="(no default)"
	    else
		def_fullname="[${fullname}]"
	    fi
	    echo $ecn "Please enter your name ${def_fullname}: $ecc"
	    read fullname
	    if [ "x${fullname}" = "x" ]; then
		if [ "x${old_fullname}" = "x" ]; then
		    echo " "
		    echo "Send aborted."
		    send_mail=no
		else
		    fullname="${old_fullname}"
		fi
	    fi
	fi

	if [ "$send_mail" = yes ]; then
	    old_phonenumber="${phonenumber}"
	    if [ "x${phonenumber}" = "x" ]; then
		def_phonenumber="(no default)"
	    else
		def_phonenumber="[${phonenumber}]"
	    fi
	    echo $ecn "Please enter your phone number ${def_phonenumber}: $ecc"
	    read phonenumber
	    if [ "x${phonenumber}" = "x" ]; then
		if [ "x${old_phonenumber}" = "x" ]; then
		    echo " "
		    echo "Send aborted."
		    send_mail=no
		else
		    phonenumber="${old_phonenumber}"
		fi
	    fi
	fi

	if [ "$send_mail" = yes ]; then
	    echo " "
	    echo "Name: ${fullname}"
	    echo "Phone: ${phonenumber}"
	    echo " "
	    echo "Enter 'abort' to abort send."
	    echo $ecn "Are these correct? [yes] $ecc"
	    get_yesno send_correct yes
	    if [ "$send_correct" = "abort" ]; then
		send_mail=no
	    fi
	fi
    done

    if [ "$send_mail" = yes ]; then
	(echo "To: sales@designacc.com";
	echo "Subject: Automatic server code registration";
	echo "";
	echo "Name: ${fullname}";
	echo "Phone: ${phonenumber}";
	echo "";
	cat /tmp/.daisc$$) | /usr/lib/sendmail -oi sales@designacc.com
	mail_stat="$?"
	if [ "${mail_stat}" -ne 0 ]; then
	    cat <<EOM

It seems as though the mail was not properly sent.  Your system may not be
set up to send Internet mail.  You will have to supply the server code to
Design Acceleration yourself.

EOM
	else
	    prompt_cont=no
	fi
    else
	echo "You will have to supply the code to Design Acceleration yourself."
	prompt_cont=no
    fi
fi

rm -rf $rm_list
rm_list=""

if [ "${prompt_cont}" = yes ]; then
    echo $ecn "Press return to continue. $ecc"
    read answer
fi

cat <<EOM


The following command may be used to ${disp_word} your server code and/or
enter your activation key:

  ${dir_other_sug_full}/${elmadmin_name} -e ${dir_other_sug_full} -n -c

Your activation key must be entered before signalscan can be used.
EOM

if [ $got_code = no -o "x$elmadmin_dir" != "x$dir_other_sug_full" ]; then
    echo "You may have to substitute a different directory for ${dir_other_sug_full}"
    echo "if you have moved the license manager somewhere else."
fi

cat <<EOM

If you wish to use signalscan on a different host than the one on which
the license manager daemon is running, you may need to set the environment
variable DAI_ELMHOST to be the name of the host on which the license
manager daemon is running.  This can be set in the .login or .profile of
each user who will be using signalscan.  For example, a csh or tcsh user
may put the line 'setenv DAI_ELMHOST license-server-host' in their .login
file in their home directory, and a sh, ksh, or bash user may put the line
'DAI_ELMHOST=license-server-host export DAI_ELMHOST' in their .profile file.

Signalscan installation complete.  You may remove the files ss-install and
${src_file} if you do not plan to run this installation script again,
or you may copy them to another directory, such as ${dir_other_sug_full}.
EOM

if [ "x$dir_other" != "xnone" -a -r ${dir_other}/README ]; then
    echo "Please read the README file in ${dir_other}/README."
fi

exit 0
