#! /bin/sh
#
#	fmcopytraining
#
#   Copyright (c) 1986-1993 Frame Technology Corp. All rights reserved.
#
#	$Id: fmcopytraining,v 1.5 1992/11/25 01:12:38 adl Exp $
# 
#	This script sets up a training directory in the home directory of
#	the user. It copies over all the files from
#
#		$FMHOME/fminit/<language>/<product>/Training
#
#   to $HOME/<product>training where <language> is any of the installed
#   UI languages, and <product> is any of the installed products.

proddef=Maker				# default product
langdef=usenglish			# default language
prodf=/tmp/prod.$$; >$prodf
langf=/tmp/lang.$$; >$langf
product=#none
dirlang=#none

script=`basename $0`

# assume $FMHOME is set since users should call this script
# through bin/.wrapper

ask="$FMHOME/bin/scripts/fm_ask"
usage="Usage: $script -l <language> -p <product>

<language> may be any of the installed UI languages.
<product>  may be any of the installed Frame products.
"
trap "rm -rf $prodf $langf" 0
trap "exit 1" 1 2 3 15

echo "
$script: Set up training materials for Frame products.

This script creates a training directory in your home directory.
Documents mentioned in the Getting Started manual will be copied 
into a personal training directory so that you can edit them 
according to the exercises."
$ask y n "OK to continue?" || exit 0

##### Look for command line options

while [ $# != 0 ]; do
case $1 in
	-l)
		shift
		case $1 in
		english|us|usenglish)
			dirlang=usenglish ;;
		german|gr|deutsch)
			dirlang=deutsch  ;;
		french|fr|francais)
			dirlang=francais ;;
		ukenglish|uk)
			dirlang=ukenglish ;;
		swedish|sw|svenska)
			dirlang=svenska  ;;
		italian|it|italiano)
			dirlang=italiano  ;;
		irish|ir|gaelic|gaeilge)
			echo "Nil aon Gaeilgeoiri anseo! Slan."; exit 1 ;;
		*)
			echo "Unknown language: $1".
			echo "$usage"
			exit 1 ;;
		esac 
		shift
		;;
	-p)
		shift
		case $1 in
		FrameMaker|framemaker|Maker|maker|FM|fm)
			product=Maker ;;
		FrameWriter|framewriter|Writer|writer|FW|fw)
			product=Writer ;;
		FrameBuilder|framebuilder|Builder|builder|FB|fb)
			product=Builder ;;
		*) 
			echo "Unknown product: $1"
			echo "$usage"
			exit 1 ;;
		esac
		shift
		;;
	*)
		echo "Unknown option: $1"
		echo "$usage"
		exit 1 ;;
	esac
done

# If we're already set with the product and language we
# want, then skip all this rigmarole.

if [ ! -d "$FMHOME/fminit/$dirlang/$product" ]; then

##### Figure out what's installed

	echo "Examining your installation. Please wait..."
	cd $FMHOME/fminit; train=`/bin/ls -d1 ./*/*/Training`
	echo $train | sed 's: :\
:g' | sed 's:\./[^/][^/]*/\([^/][^/]*\)/Training:\1:' | sort | uniq > $prodf

##### Get a valid product

	while [ "$product" = "#none" ]; do
		n=`sed -n '$=' $prodf`
		[ $n -eq 1 ] && { product=`sed '1p' $prodf`; break; }
		echo "
Please select the product for which you wish to install training files:"
		awk '{printf("%5d   Frame%s\n", NR, $0);}' $prodf
		default=`sed -n "/$proddef/=" $prodf`
		echo "Enter number: [${default:=1}]"
		read answer
		: ${answer:=$default}
		if [ "$answer" -le $n -a "$answer" -gt 0 ]; then
			product=`sed -n "${answer}p" $prodf`
		else
			echo "Please enter a number between 1 and $n"
		fi
	done

##### Get a valid language
	
	while [ "$dirlang" = "#none" ]; do
		echo $train | sed 's: :\
:g' | sed -n "s:\./\([^/][^/]*\)/$product/Training:\1:p" >$langf
		n=`sed -n '$=' $langf`
		[ $n -eq 1 ] && { dirlang=`sed '1p' $langf`; break; }
		echo "
Please select a language for which you wish to install Frame$product
training files:"
		awk '{printf("%5d   %s\n", NR, $0);}' $langf
		default=`sed -n "/$langdef/=" $langf`
		echo "Enter number: [${default:=1}]"
		read answer
		: ${answer:=$default}
		if [ "$answer" -le $n -a "$answer" -gt 0 ]; then
			dirlang=`sed -n "${answer}p" $langf`
		else
			echo "Please enter a number between 1 and $n"
		fi
	done
fi

##### Check out source directory

dir=$FMHOME/fminit/$dirlang/$product/Training

[ -d "$dir" ] || {
	echo "Cannot find $dir.

Frame$product is not installed correctly on your machine.
Please see your system administrator and try again after fixing the problem.

$script terminated without completing.
";
	exit 1;
}

[ -r "$dir" ] || {
 	echo "Sorry. You don't have read access to 
    \$FMHOME/fminit/$dirlang/$product/Training
Please see your system administrator and try again after fixing the problem.

$script terminated without completing.
";
	exit 1;
}
echo "Copying $dirlang training files for Frame$product...
"

##### Check out destination directory

destdir=F`echo $product | sed 's:^\(.\).*$:\1:'`Training

if [ -d "$HOME/$destdir" ]; then
	echo "OK to overwrite existing directory"
	if $ask y n "$HOME/$destdir?"; then
		rm -rf $HOME/$destdir
	else
		echo "$HOME/$destdir not removed."
		echo "Rename it and try again."
		echo "$script terminated without completing."
		exit 1
	fi
elif [ -f "$HOME/$destdir" ]; then
	echo "OK to overwrite existing file"
	if $ask y n "$HOME/$destdir?"; then
		rm -f $HOME/$destdir
	else
		echo "$HOME/$destdir not removed."
		echo "Rename it and try again."
		echo "$script terminated without completing."
		exit 1
	fi
fi

##### Everything's cool; do the copy

echo "Making directory $HOME/$destdir...
"
umask 22
(cd $HOME; mkdir $destdir; cd $destdir; cp -r $dir/* .)
echo "$script completed."
exit 0
