#!/bin/sh
# Script to un-tar and install WordPerfect
if [ "`echo -n`" = "" ]; then N="-n"; else C="\c"; fi; Work=0;
echo
echo $N "  Did you unzip and untar the files you downloaded? (y/n) $C";
read resp;
if [ "$resp" != "y" -a "$resp" != "Y" ]; then sleep 1;
	echo;
	echo "  Please refer to the Readme file if you have questions.";
	echo;
	exit;
fi;

# See if the file need to be renamed
if [ -f Moves ]; then
	cat Moves | while read From To; do
		if [ -f "$From" ]; then ln $From $To; fi;
	done;
	rm -f Moves Name;
fi;

do_bk() { for File in `ls -1 *_* | sed '/\.bk/d'`; do
	if [ -f $File ]; then mv $File $File.bk; fi; done; }

do_cln() {
	for FD in manual netscape readme sdk $Platform \
	remove.wp install.wp shared; do
		rm -rf $FD; done; }

do_menu() {
	while [ "$Work" -lt 1 -o "$Work" -gt 9 ]; do echo; echo; echo;
		echo "  FTP Installation Menu"; echo;
		echo "  Normal Install Options";
		echo "  1) GUI and Character-based";
		echo "  2) GUI only";
		echo "  3) Character-based only"; echo;
		echo "  Language Only Install Selections";
		echo "  4) GUI and Character-based";
		echo "  5) GUI only";
		echo "  6) Character-based only"; echo;
		echo "  Additional Selections";
		echo "  7) Online GUI Manual";
		echo "  8) GUI Graphics Package";
		echo "  9) Software Developers Kit"; echo;
		echo $N "  Enter Selection: $C"; read Work;
		if [ "$Work" = "" ]; then exit; fi;
	done;

	do_bk; do_cln;

	case $Work in
		1)Prod="b g c";;
		2)Prod="b g";;
		3)Prod="b c";;
		4)Lang="b g c";;
		5)Lang="b g";;
		6)Lang="b c";;
		7)Extr="man";;
		8)Extr="gra";;
		9)Extr="sdk";;
	esac;

	if   [ "$Prod" != "" ]; then
		for P in $Prod; do
			for file in `ls ${P}* 2>/dev/null | sed 's/\.bk//'`; do
				if [ -f $file.bk ]; then mv $file.bk $file; fi;
			done;
		done;
	elif [ "$Lang" != "" ]; then
		for L in $Lang; do
			for file in `ls b_ins* 2>/dev/null | sed 's/\.bk//'`; do
				if [ -f $file.bk ]; then mv $file.bk $file; fi;
			done;
		done;
		for P in $Prod; do
			for L in ce cf de es fr it nl oz uk us; do
				for file in `ls ${P}_${L}* 2>/dev/null | sed 's/\.bk//'`; do
					if [ -f $file.bk ]; then mv $file.bk $file; fi;
				done;
			done;
		done;
	elif [ "$Extr" = "man" ]; then
		for file in `ls b_ins* 2>/dev/null | sed 's/\.bk//'`; do
			if [ -f $file.bk ]; then mv $file.bk $file; fi;
		done;
		for file in `ls ?_$Extr* 2>/dev/null | sed 's/\.bk//'`; do
			if [ -f $file.bk ]; then mv $file.bk $file; fi;
		done;
	elif [ "$Extr" != "man" ]; then
		for file in `ls ?_$Extr* 2>/dev/null | sed 's/\.bk//'`; do
				if [ -f $file.bk ]; then mv $file.bk $file; fi;
		done;
	fi;
}

if [ "$1" = menu ]; then do_menu; fi;

for TarFile in `ls -1 *_* 2>/dev/null | sed '/\.bk/d'`; do
	TarFiles="$TarFiles $TarFile"; done
if [ "$TarFiles" = "" ]; then
	echo; echo "Error: Nothing new to install."; echo; exit; fi;

echo; echo "  Extracting Files..."; SWD=`pwd`;
for File in $TarFiles; do tar xf $File; done;

Platform=`ls -1d */ins 2>/dev/null \
| sed -n '1p' | sed 's|\./||' | sed 's|/.*$||'`;

for File in `find . -name \*aa -print | sed 's/aa//g'`; do
	cat $File?? > $File; rm -f $File??; done;	# join split files

ck_man() {
	if [ ! -f g_man00 ]; then		# take it out of the list
		grep -v "=WP Online Manual" shared/l_extr > shared/l_extr.new;
		mv shared/l_extr.new shared/l_extr; fi;
}

ck_net() {
	if [ ! -f g_net00 ]; then		# take it out of the list
		grep -v "=Netscape Browser" shared/l_extr > shared/l_extr.new;
		mv shared/l_extr.new shared/l_extr; fi;
}

if [ -f b_ins00 -a -f b_req00 ]; then		# if normal install
	if [ ! -f g_req00 -o ! -f c_req00 ]; then
		if   [ -f g_req00 ]; then echo "2 " > shared/wpprod;
		elif [ -f c_req00 ]; then echo "3 " > shared/wpprod; fi;
	fi;

	lngs="zz";
	for lng in `ls -d */lng/* | sed 's|.*lng/||' | sed 's|/||'`; do
		lngs="$lngs|$lng"; done;
	if [ ! -f shared/wplngs.bk ]; then mv shared/wplngs shared/wplngs.bk; fi;
	egrep "$lngs" shared/wplngs.bk > shared/wplngs;

	echo "skip" > shared/wpterm;		# disable terminal selection

	if [ ! -f g_gra00 -a -f shared/ship ]; then	# remove from shiplist
		grep -v "graphics\.tar" shared/ship > shared/ship.bk;
		mv shared/ship.bk shared/ship; fi;

	ck_man; ck_net; DOINST=1;
elif [ -f b_ins00 ]; then
	lngs="zz";
	for lng in ce cf de es fr it nl oz uk us; do
		if [ ! -f g_"$lng"00 -o ! -f c_"$lng"00 ]; then
			if   [ -f g_"$lng"00 ]; then echo "2 " > shared/wpprod;
			elif [ -f c_"$lng"00 ]; then echo "3 " > shared/wpprod; fi;
		fi;
		if [ -f g_"$lng"00 -o -f c_"$lng"00 -o -f b_"$lng"00 ]; then
			lngs="$lngs|$lng"; lflg="-l"; DOINST=1; fi;
	done;

	if [ "x$lflg" != "x" ]; then
		if [ ! -f shared/wplngs.bk ]; then
			mv shared/wplngs shared/wplngs.bk; fi;
		egrep "$lngs" shared/wplngs.bk > shared/wplngs; fi; 

	if [ -f g_man00 ]; then eflg=-e; DOINST=1; else ck_man; fi;
	if [ -f g_net00 ]; then eflg=-e; DOINST=1; else ck_net; fi;

	grep -v "=-CDE Desktop Icon" shared/l_extr > shared/l_extr.new;
		mv shared/l_extr.new shared/l_extr;
fi;

# Start the install
if [ "$DOINST" = 1 ]; then
	cp shared/install.wp .; ln install.wp remove.wp;
	./install.wp $eflg $lflg; Ret=$?;
fi;

if [ "$Ret" = 1 ]; then do_cln; exit 1; fi;

if [ -f .wpdata ]; then		# Get the installation directory
	Idir=`grep '^'idir= .wpdata | sed 's/idir=//'`; fi;
while [ "$Idir" = "" ]; do	# Prompt for it if missing
	echo $N "Enter your installation directory: $C"; read Idir;
	if [ -d $Idir/shlib10 ]; then break;
	else echo; echo "Error: Invalid Install area"; echo; fi;
done
		
# See if any files need to be manually moved to the install area.
if [ -f g_gra00 -a ! -f b_ins00 ]; then
	$Platform/ins/wpdecom $Platform/dat/graph.tar $Platform/dat/graphics;
	(cd $Idir/wpgraphics; tar xf $SWD/$Platform/dat/graphics)
fi;

if [ -f g_sdk00 ]; then (cd $Idir; tar xf $SWD/g_sdk00); fi;

cp *.all $Idir/shlib10 2>/dev/null;

do_bk; do_cln;
