#!/bin/sh
# Copyright (c) 1989-1997 Adobe Systems, Incorporated. All rights reserved.

# This script copies tutorial/tour files to the user's home directory.
# The specific files which are copied depend, in part, on the name by
# which the script is invoked:
# 
#     copytutorial    - FrameMaker tutorial files
#     copymakertour   - maker tour files
#     copyfmsgmltour  - fmsgml tour files
# 
# The only other option is the choice of language, English by default.
# 
# Adobe internal documentation only, ECO (Engineering Change Order):
#     title  - "New scripts to copy tutorial and tour files for UNIX"
#     file   - /usr/dim60/doc/eco/ScriptECO.fm (Adobe internal documentation)

# Initialization, trivial constants, etc.
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:/usr/bsd:/etc ; export PATH
set -u
CNAME=`basename $0`

EXIT_NO_ERROR=0
EXIT_EXEC_ERROR=1
EXIT_USAGE_ERROR=2

# All /bin/sh functions used in this script are defined here in a solid block.
# There is no code inside this block other than function defining, and there
# are no tricks anywhere in the script involving function re-defining, function
# undefining, or delaying the defining of functions.  (Isn't that nice?)

# BEGIN block which defines all functions

# Translate user input language into a standard value for that language.
# Requires the table $LANGUAGE_TRANS_TABLE.
trans_lang_to_standard()
{
	USER_REQ="$1"

	# Convert to lower case before lookup, and change non-alpha
	# characters into "#" to avoid false matches in the lookup process.
	LOOKUP=`echo "$USER_REQ" | tr '[A-Z]' '[a-z]' | sed 's/[^a-z]/#/g'`

	TABLE_LINE=`echo "$LANGUAGE_TRANS_TABLE" | grep " $LOOKUP "`
	if [ -z "$TABLE_LINE" ]
	then
		return 1
	fi
	
	# Output the standard value, the first column in the table.
	# The sed components are:
	#     s/^ //		# get rid of initial blank
	#     s/ .*//		# delete from the next blank onward
	echo "$TABLE_LINE" | head -1 | sed 's/^ //;s/ .*//'

	return 0
}


# Create a directory, where multiple components of the path may need
# to be created, that is, the parent of the new directory may not yet
# exist, nor the parent's parent, etc.
mk_dir_path()
{
	FULL_PATH="$1"

	# On sunos/sol/hp/ibm/sgi "mkdir -p" worked okay.
	mkdir -p "$FULL_PATH"

	# Test for success by actually looking for the new directory.
	if [ -d "$FULL_PATH/." ]
	then
		return 0
	else
		return 1
	fi
}

# Do file name translation on the directories which live in $FMHOME/fminit.
dir_name_trans()
{
	case ${FM_LANG}:$1 in

	  usenglish*)
		echo $1
		;;

	  ukenglish*)
		echo $1
		;;

	  japanese*)
		echo $1
		;;

	  deutsch:Tour)
		echo Kurs
		;;

	  deutsch:Tutorial)
		echo Uebungen
		;;

	  francais:Tour)
		echo Visite
		;;

	  francais:Tutorial)
		echo Didacticiel
		;;

##	  svenska:Tour)
##		echo MAKE_ME_RIGHT_PLEASE
##		;;
##
##	  svenska:Tutorial)
##		echo MAKE_ME_RIGHT_PLEASE
##		;;
##
##	  italiano:Tour)
##		echo MAKE_ME_RIGHT_PLEASE
##		;;
##
##	  italiano:Tutorial)
##		echo MAKE_ME_RIGHT_PLEASE
##		;;
	esac
}

# END block which defines all functions

# Significant constants, tables, etc.
DEFAULT_LANGUAGE="usenglish"

# Make sure that $HOME is an absolute path.
# The sed components are:
#     s/./&|/		# follow the first character with a "|"
#     s/|.*//		# get rid of "|" and everything following it
# The "|" character is arbitrary, anything but "/" would do just as well.
FIRST_HOME_CHAR=`echo "$HOME" | sed 's/./&|/;s/|.*//'`
if [ "x$FIRST_HOME_CHAR" != "x/" ]
then
	echo "
$CNAME ERROR: the \$HOME environment variable must be an absolute path,
that is, it must begin with the \"/\" character.
	\$HOME = \"$HOME\""
	exit $EXIT_EXEC_ERROR
fi

# Verify that $FMHOME is set, which it should be since users should call
# this script through bin/.wrapper.
if [ -z "${FMHOME:-}" ]
then
	echo "
$CNAME ERROR: the \$FMHOME environment variable must be set.  You
need to invoke $CNAME from the FrameMaker product installation
directory, which will set \$FMHOME for you automatically."
	exit $EXIT_EXEC_ERROR
fi

# The following table, $LANGUAGE_REQ_INPUT_TABLE, lists all acceptable
# user inputs for specifying a language.  It is also used directly "as is"
# as output in a usage message.
#
# The format is one line per language, with a white-space-separated list
# of the acceptable aliases for that language appearing on each line.
# Case (upper/lower) is ignored.  The first entry on each line is the
# "standard" alias for that language.
#
# Currently only alpha characters are allowed in the languages listed.
# If this changes then the logic used in the trans_lang_to_standard
# function must also be changed.
#
# A second table $LANGUAGE_TRANS_TABLE is created from this table.  The
# only reformatting which takes place is that white-space is compressed
# into a single blank character, and everything is translated to lower case.
# A blank is added to the start and finish of each line, so that all items
# are delimited by blanks on both sides.
#
# Note that "japanese" being the standard rather than "nihongo" is
# inconsistent with all other non-english languages.

LANGUAGE_REQ_INPUT_TABLE="
	usenglish	us	english
	ukenglish	uk
	japanese	ja	nihongo

	deutsch		de	german
	francais	fr	french
	svenska		sv	swedish
	italiano	it	italian
	"

# Create normalized version of the above table, $LANGUAGE_TRANS_TABLE,
# with white-space standardized to make it easy to use programmatically.
# Upper/lower case is ignored.
#
# The sed components are:
#     s/	/ /g	# tabs to blanks
#     s/  */ /g		# multiple blanks to one blank
#     s/^ */ /		# one blank at the start of each line
#     s/ *$/ /		# one blank at the end of each line
#     /^ $/d		# get rid of empty lines

LANGUAGE_TRANS_TABLE=`echo "$LANGUAGE_REQ_INPUT_TABLE" | \
  sed 's/	/ /g;s/  */ /g;s/^ */ /;s/ *$/ /;/^ $/d' | tr '[A-Z]' '[a-z]'`

# Look at the name by which this script is invoked.  The creation of
# $INSTALL_REQ creates an internal standard, which will remain constant
# in spite of any future changes made to the names by which the script is
# invoked.

case $CNAME in
  copyfmsgmltour)
	DESC_OF_INSTALLED="FrameMaker SGML tour files"
	INSTALL_REQ=sgml_tour
	;;

  copymakertour)
	DESC_OF_INSTALLED="FrameMaker tour files"
	INSTALL_REQ=maker_tour
	;;

  copytutorial)
	DESC_OF_INSTALLED="FrameMaker tutorial files"
	INSTALL_REQ=maker_tutorial
	;;

  *)
	echo "
Known names by which this script can be invoked:

	copyfmsgmltour		(install FrameMaker SGML tour files)
	copymakertour		(install FrameMaker tour files)
	copytutorial		(install FrameMaker tutorial files)

$CNAME ERROR: script invoked by unknown name: \"$CNAME\"" >& 2
	exit $EXIT_USAGE_ERROR
	;;
esac

# Process the command line arguments.  The $USAGE_MSG string documents what
# options are available.

USAGE_MSG="
usage: $CNAME [ -l language ]"

# Treat a single "?" on the command line as a request for the usage message,
# same for "-h", since unix users often think to try these.
if [ $# -eq 1 ]
then
	if [ "x$1" = "x?" -o "x$1" = "x-h" ]
	then
		echo "$USAGE_MSG" >& 2
		exit $EXIT_USAGE_ERROR
	fi
fi

PROC_ERROR=""			# any error while processing the command line
USER_LANG_REQ=""		# language requested by user

while [ $# -gt 0 ]
do
	case "$1" in
	  -l)
		if [ $# -lt 2 ]
		then
			echo $CNAME ERROR: missing language specification after \""$1"\" >& 2
			PROC_ERROR=true
			break
		fi
		shift

		USER_LANG_REQ="$1"
		shift
		;;

	  *)
		echo $CNAME ERROR: unknown command line option: \""$1"\" >& 2
		PROC_ERROR=true
		shift
		;;
	esac
done

if [ $PROC_ERROR ]
then
	echo "$USAGE_MSG" >& 2
	exit $EXIT_USAGE_ERROR
fi

# Translate the user-requested language (if any) into a standard value,
# or else use the default.  We use $FM_LANG (FrameMaker LANG) to avoid
# conflict with $LANG, which has established uses.

if [ -z "$USER_LANG_REQ" ]
then
	FM_LANG="$DEFAULT_LANGUAGE"
else
	FM_LANG=`trans_lang_to_standard "$USER_LANG_REQ"`
	if [ $? -ne 0 ]
	then
		echo "
List of available language choices:
$LANGUAGE_REQ_INPUT_TABLE

$CNAME ERROR: unknown language: \"$USER_LANG_REQ\"" >& 2
		exit $EXIT_EXEC_ERROR
	fi
fi

# The rest is a simple directory tree copy.  First we determine the source
# and destination directories, based upon the type of copy requested and
# the language specified.

case $INSTALL_REQ in
  sgml_tour)
	DIR_FROM=$FMHOME/fminit/$FM_LANG/`dir_name_trans Tour`
	DIR_TO=$HOME/`dir_name_trans Tour`
	;;

  maker_tour)
	DIR_FROM=$FMHOME/fminit/$FM_LANG/`dir_name_trans Tour`
	DIR_TO=$HOME/`dir_name_trans Tour`
	;;

  maker_tutorial)
	DIR_FROM=$FMHOME/fminit/$FM_LANG/`dir_name_trans Tutorial`
	DIR_TO=$HOME/`dir_name_trans Tutorial`
	;;
esac

# Verify that the source exists for this language.
if [ ! -d $DIR_FROM/. ]
then
	echo "
$CNAME ERROR: the $DESC_OF_INSTALLED are not available for
this language.  The missing directory is:
	$DIR_FROM" >& 2
	exit $EXIT_EXEC_ERROR
fi

# Adhere to the precedent set by the old fmcopytutorial script, giving a
# verbose description of what is being installed.

echo "
$CNAME: Set up training materials for FrameMaker products.
This script installs the $DESC_OF_INSTALLED into your home directory.

FROM:	$DIR_FROM
TO:	$DIR_TO
"

# Create the destination directory.
( mk_dir_path $DIR_TO )
if [ $? -ne 0 ]
then
	echo "
$CNAME ERROR: cannot create install directory:
	$DIR_TO" >& 2
	exit $EXIT_EXEC_ERROR
fi

# Make any existing installed files writable.  The paranoid check for
# cd's success is only done because we are about to clobber an entire
# directory tree's modes recursively, and that makes it worth checking
# that we're starting from the right place.
cd $DIR_TO
if [ $? -ne 0 ]
then
	echo "
$CNAME PANIC: cannot cd to directory which was successfully created:
	$DIR_TO" >& 2
	exit $EXIT_EXEC_ERROR
fi

find . \( -type d -o -type f \) -print | xargs chmod u+w

# Do the copy.  Unconditionally overwrite existing files.
cd $DIR_FROM
find . -depth -print | cpio -pdmu $DIR_TO/.

# Make sure all installed files are writable.
cd $DIR_TO
if [ $? -ne 0 ]
then
	echo "
$CNAME PANIC: cannot cd to directory which was successfully created:
	$DIR_TO" >& 2
	exit $EXIT_EXEC_ERROR
fi

find . \( -type d -o -type f \) -print | xargs chmod u+w
