#!/bin/sh
#
# YYonX install kit
#

IMAKEFILE='./Imakefile'
LISPCONFIG='./client/config.lisp'
CCONFIG='./conf.h'

yyclient_src_directory=''

yyserv_exec_directory=''
yyserv_data_directory=''

use_wnn_kinput='no'
wnn_jserver_host='localhost'
wnn_data_directory=''
wnn_include_directory=''
wnn_library_file=''

if [ ! -d ./script ]; then
	mkdir ./script
fi

cat <<ENDSCRIPT1 >./script/yyread
yyans='!'
while expr "YY\$yyans" : "YY!" >/dev/null; do
	echo -n "\$yyquestion"
	read yyans
	case "\$yyans" in
	!)	/bin/sh
	        echo "!"
	        ;;
	!*)	set \`expr "YY\$yyans" : "YY!\(.*\)\$"\`
	        /bin/sh -c "\$*"
        	echo "!"
	        ;;
	esac
done
case "\$yyans" in
'') yyans="\$yydefault";;
-) yyans="";;
esac
ENDSCRIPT1

cat <<ENDSCRIPT2 >./script/yyyesorno
yyans='!'
while expr "YY\$yyans" : "YY!" >/dev/null; do
	echo -n "\$yyquestion"
	read yyans
	case "\$yyans" in
	!)	/bin/sh
	        echo "!"
	        ;;
	!*)	set \`expr "YY\$yyans" : "YY!\(.*\)\$"\`
	        /bin/sh -c "\$*"
        	echo "!"
	        ;;
	y*|Y*)	yyans='yes';;
	n*|N*)	yyans='no';;
	'')	yyans="\$yydefault";;
	*)	echo "Please answer yes or no"
		yyans='!'
		;;
	esac
done
ENDSCRIPT2


: 
cat <<ENDMESSAGE


	<<YYonX version 1.5  Install Tool>>
					
			1992.10.30  Keisuke 'Keiko' Tanaka and YY Project

This package contains the following modules:
ENDMESSAGE
cat ./MODULES
echo ""
echo ""
echo ""

if [ -f ./config.def ]; then
	yydefault='no'
	yyquestion="Are you using defult values saved in config.def? [$yydefault]>"
	. ./script/yyyesorno
	if [ "$yyans" = "yes" ];then
		. ./config.def
	fi
fi



: Questions for client...
yyclient_src_directory=`pwd`
if [ "YY$yyclient_src_directory" = "YY" -o ! -d $yyclient_src_directory ]; then
	echo "I can't get path name of current directory"
	while expr "YY$yyclient_src_directory" : "YY" >/dev/null; do
		: path name
		yydefault="$yyclient_src_directory"
		yyquestion="Please input path name of the current direcory [$yydefault]>"
		. ./script/yyread
	done
fi


: path name for executables
cat <<ENDMESSAGE


When you execute 'make install', several commands are installed under
the directory for public executables and several data files are
created under database directory

ENDMESSAGE
yydefault="$yyserv_exec_directory"
yyquestion="Please input directory name for your public executables [$yydefault]>"
. ./script/yyread
yyserv_exec_directory="$yyans"


: Directory for Databases
yydefault="$yyserv_data_directory"
yyquestion="Please input path name for database directory for YYonX [$yydefault]>"
. ./script/yyread
yyserv_data_directory="$yyans"


: Wnn Kinput
cat <<ENDMESSAGE

'yyserv' can communicate with WNN Jserver to convert ASCII Characters
into KANA/Kanji Strings.
WNN Jserver itself is not included in this package.
But to build 'yyserv', definition files and one archive library of WNN
are needed.

CAUTION:
This is alpha release of YYonX version1.5.
On this release you can't use WNN KANA/Kanji conversion feature.
Please do not answer 'yes' for the following question.
ENDMESSAGE

yydefault="$use_wnn_kinput"
yyquestion="Are you plan to use Kana-Kanji conversion function on your 'yyserv'? [$yydefault]>"
. ./script/yyyesorno
use_wnn_kinput="$yyans"

: If you use Kinput you must fix a directory path for definition files
case "$use_wnn_kinput" in
y*)	loop='yet';;
*)	loop=ok;;
esac
loop_count=0
while expr "YY$loop" : "YYyet" >/dev/null; do
	: path name
	yydefault="$wnn_include_directory"
	yyquestion="Please input path name for WNN definition files [$yydefault]>"
	. ./script/yyread

	case "$yyans" in
	/*)	: check two files
		if [ ! -d $yyans ]; then
			echo "$yyans: No such Directory"
		elif [ ! -f $yyans/jllib.h ]; then
			echo "$yyans/jllib.h: No such File"
		elif [ ! -f $yyans/rk_spclval.h ]; then
			echo "$yyans/rk_spclval.h: No such File"
		else
			wnn_include_directory=$yyans
			loop='ok'
		fi
		;;
	*)	echo "Please input full path name.."
		continue;;
	esac
	loop_count=`expr $loop_count + 1`
	if [ $loop_count -gt 3 ]; then
		yydefault='yes'
		yyquestion="Do you skip questions about WNN? [$yydefault]>"
		. ./script/yyyesorno
		case "$yyans" in
		y*)	use_wnn_kinput='no'
			loop='ok';;
		*)	loop_count=0;;
		esac
	fi
done

: Wnn library file
case "$use_wnn_kinput" in
y*)	loop='yet';;
*)	loop='ok';;
esac
loop_count=0
while expr "YY$loop" : "YYyet" >/dev/null; do
	: path name
	yydefault="$wnn_library_file"
	yyquestion="Please input path name for WNN library [$yydefault]>"
	. ./script/yyread

	case "$yyans" in
	/*)	: check file
		if [ ! -f $yyans ]; then
			echo "$yyans: No such directory"
		else
			wnn_library_file="$yyans"
			loop='ok'
		fi
		;;
	*)	echo "Please input full path name.."
		continue;;
	esac
	loop_count=`expr $loop_count + 1`
	if [ $loop_count -gt 3 ]; then
		yydefault='yes'
		yyquestion="Do you skip questions about WNN? [$yydefault]>"
		. ./script/yyyesorno
		case "$yyans" in
		y*)	use_wnn_kinput='no'
			loop='ok';;
		*)	loop_count=0;;
		esac
	fi
done

: directory for Wnn database
case "$use_wnn_kinput" in
y*)	loop='yet';;
*)	loop='ok';;
esac
loop_count=0
while expr "YY$loop" : "YYyet" >/dev/null; do
	: path name
	yydefault="$wnn_data_directory"
	yyquestion="Please input the directory path name under which 'wnnenvrc' file exists [$yydefault]>"
	. ./script/yyread

	case "$yyans" in
	/*)	: check two files
		wnn_data_directory="$yyans"
		loop='ok'
		;;
	*)	echo "Please input full path name.."
		continue;;
	esac
	loop_count=`expr $loop_count + 1`
	if [ $loop_count -gt 3 ]; then
		yydefault='yes'
		yyquestion="Do you skip questions about WNN? [$yydefault]>"
		. ./script/yyyesorno
		case "$yyans" in
		y*)	use_wnn_kinput='no'
			loop='ok';;
		*)	loop_count=0;;
		esac
	fi
done




cat <<SAVEDEFAULTS >./config.def
yyserv_exec_directory='$yyserv_exec_directory'
yyserv_data_directory='$yyserv_data_directory'
use_wnn_kinput='$use_wnn_kinput'
wnn_data_directory='$wnn_data_directory'
wnn_jserver_host='$wnn_jserver_host'
wnn_include_directory='$wnn_include_directory'
wnn_library_file='$wnn_library_file'
SAVEDEFAULTS


echo ''
echo 'Now we are ready to create several configuration files'
echo ''
yydefault='yes'
yyquestion="Are you OK? [$yydefault]>"
. ./script/yyyesorno
case "$yyans" in
n*)	echo "Well... Please restart $0"
	echo "Bye"
	exit 2
	;;
esac

case "$use_wnn_kinput" in
y*)	d=`expr "$wnn_library_file" : "\(.*\)/lib[a-zA-Z]*.a$"`
	l=`expr "$wnn_library_file" : ".*/lib\([a-zA-Z]*\).a$"`
	KINPUTLIB="-L$d -l$l"
	WNNKINPUT='define'
	;;
*)	KINPUTLIB=''
	WNNKINPUT='undef'
	;;
esac

: directory under which executables are puts
if [ ! -d ./bin ]; then
	mkdir ./bin
fi


# $wnn_include_directory

echo -n "creating $IMAKEFILE..."
if [ -f $IMAKEFILE ]; then
	mv $IMAKEFILE $IMAKEFILE.bak
fi
sed -e "s;@@KINPUT_LIB@@;$KINPUTLIB;" \
	-e "s;@@WNNKINPUT@@;$WNNKINPUT;" \
	-e "s;@@WNNINCLUDE@@;$wnn_include_directory;" \
	-e "s;@@YYBINDIR@@;$yyserv_exec_directory;" \
	-e "s;@@YYLIBDIR@@;$yyserv_data_directory;" \
	Imakefile-dist >$IMAKEFILE
echo "Done"

echo -n "creating $LISPCONFIG for YY-client..."
if [ -f $LISPCONFIG ]; then
	mv $LISPCONFIG $LISPCONFIG.bak
fi
cat <<ENDEND > $LISPCONFIG
;;; -*- Mode: LISP; Syntax: Common-lisp; Package: USER; Base: 10 -*-
(in-package :user)
ENDEND
echo "(defvar *YY* \"$yyclient_src_directory\")" >> $LISPCONFIG
echo "Done"

if [ -f $CCONFIG ]; then
	mv $CCONFIG $CCONFIG.bak
fi
echo -n "creating $CCONFIG ..."
( echo "#define FOURBYTESINT"
  echo "#define SHIOTACG"
  echo "#define KOSAKA921021"
  echo "#define SUPERSERVER"
  if [ "$use_wnn_kinput" = "yes" ]; then
	echo "#define KINPUT"
	echo "#define WNNKINPUT"
	echo "#define KANAFILE \"$wnn_data_directory/rk\""
	echo "#define WNNRCFILE \"$wnn_data_directory/wnnenvrc\""
	echo "#define JSERVER \"$wnn_jserver_host\""
  else
	echo "#undef KINPUT"
	echo "#undef WNNKINPUT"
  fi
  echo "#define YYBINDIR \"$yyserv_exec_directory\""
  echo "#define YYLIBDIR \"$yyserv_data_directory\""
 ) > $CCONFIG
echo "Done"

echo -n "createing directories for Lisp binaries..."
if [ ! -d client/BIN ]; then
	mkdir client/BIN
fi
( cd client; cc -c prtclif.c -o BIN/prtclif.o )
if [ ! -d CG/BIN ]; then
	mkdir CG/BIN
fi
echo "Done"

echo "create all Makefiles for this package"
xmkmf
if [ $? -ne 0 ]; then
	echo "Error ...."; exit 1
fi
make Makefiles
if [ $? -ne 0 ]; then
	echo "Error ...."; exit 1
fi

make clean
if [ $? -ne 0 ]; then
	echo "Error ...."; exit 1
fi

make includes
if [ $? -ne 0 ]; then
	echo "Error ...."; exit 1
fi

make depend
if [ $? -ne 0 ]; then
	echo "Error ...."; exit 1
fi

rm -f ./script/yyread ./script/yyyesorno
rmdir ./script
exit 0
