#!/bin/sh
#
#  Configure Version 2.0 of May 22, 1995
#  SKK Configuration Script 
#	for skk 8.6 or later, and skkserv 3.x or later
#
#  Written by Yukiyoshi Kameyama (kam@sato.riec.tohoku.ac.jp)
#  Inspired by the SKK configuration program "skkconf" 
#	created by Jun Takemura (jun@hakuba.cat.co.jp)
#
########################################################################
#
#  1.01 patched by Masahiko Sato (masahiko@sato.riec.tohoku.ac.jp)
#  1.1  retry in case of non-existent directory
#	messages in Japanese
#  1.2	several tune-up's
#  1.3	bug fix by Nobu Takahashi (nt@hpycla.kobe.hp.com)
#		   Hironobu Takahashi (takahasi@tiny.or.jp)
#  1.4	bug fixes by kam
#  1.5	bug fix by Yuji IMOTO (imoto@minnie.ai.kyutech.ac.jp)
#  1.6	little improvement by kam
#  1.6.3 minor fixes by kam
#  1.6.5 bug fix for Makefile by Kenji Yabuuchi (yab@kubota.co.jp)
#  1.7  remove misconception in "private" installation
#	  owing to suggestions by SASABE Tetsuro and Masahiko Sato
#  1.8  generalize file-existence-check
#  1.8.1  install -> cp & chown, check if there's "whoami" command
#  1.8.2  fix errors in Japanese sentences 
#	(pointed out by funa@rdhw.tec.toshiba.co.jp)
#  1.9  fix errors if applied to MULE version 2.0
#		the function call (message exec-directory) produces
#		some output to STDERR, rather than STDOUT (as MULE 
#		1.x did). 
#	(pointed out by miyu@shiratori.riec.tohoku.ac.jp)
#  2.0  determine emacs-version
#
########################################################################
#
# "echo -n string" or "echo string\c"
#
if echo -n 'abc' | grep n > /dev/null
then
 n=''
 c='\c'
else
 n='-n'
 c=''
fi
#
#  check existence of source files
#
f=List-of-Files
if test ! -f $f
then
 echo "The file \"$f\" does not exist, please check the package."
 echo ""
 exit 1
fi
set `cat $f`
for f do
 if test ! -f $f
 then
  echo "The file \"$f\" does not exist, please check the package."
  echo ""
  exit 1
 fi
done
#
#  hello
#
echo "Hi, I am SKK Configuration Script."
echo "If your /bin/sh can handle 8bit code, and you can read it, "
echo "kill me and run Configure.euc."
sleep 5
echo "I'll ask several questions to you, and make a new file \"Makefile.new\""
echo "which is suitable for your site.  Moreover, I'll make and install SKK,"
echo "if you want."
echo ""
echo "Most questions have default answers which are shown in enclosing "
echo "brackets like [/usr/local/nemacs/etc]. If your answer is exactly "
echo "the same as the default, you may just hit return."
echo "If not, please type the whole answer."
echo "Please do not use white spaces in your answers."
echo ""
echo "If you want to quit this script, you can kill me at any time."
echo "Before modifying files or installing the package,"
echo "I'll ask you to confirm the result.  So, please don't worry."
echo ""
echo $n "Are you ready (yes/no) [y] ?$c"
read answer
case xx$answer in
xxy*)  echo "Hahaha, you don't have to type \"y\" if you choose the default !"
       ;;
xxn*)  echo "Mmm, I ignore your answer, if you want to stop, kill me."
       sleep 10
       ;;
esac
#
#  superuser/private
#
case `which whoami` in
 no*) echo "Are you superuser (root) ? (Reply by yes or no)"
     read answer
     case xx$answer in
	xxy*)	USER=root
		;;
	*)	USER=notroot
		;;
     esac
     ;;
 *)  USER=`whoami`
     ;;
esac

if test $USER != "root"
then
 echo ""
 echo "You are not a superuser."
 echo "I recommend you to install SKK for everyone using your machine."
 echo "But if you want, I'll tune-up everything for your private use."
 echo $n "Do you want to install SKK privately (yes/no) [n] ?$c"
 read answer
 case xx$answer in
 xxy*)  ;;
 *)   echo "Then, you should become the super user before running Configure."
      echo ""
      exit 1
      ;;
 esac
fi
#
#  check if length of filenames can be more than 14
#
echo "" >    abcdefghijklmno
if test ! -f abcdefghijklmno
then
 echo "It seems that your system does not allow long filenames."
 echo "Use \"skk-is.el\" instead of \"skk-isearch.el\"."
 SHORTNAME=1
else
 SHORTNAME=0
 rm abcdefghijklmno
fi
#
#  emacs path
#
EORMU="Nemacs"
MULE=`which mule`
case $MULE in
*alias*)	MULE=mule
		;;
esac
EMACS=`which nemacs`
case $EMACS in
no*) 	 EMACS=`which emacs`
	 case $EMACS in
	 	no*) 	 EMACS=/usr/local/nemacs
			 ;;
	 	*alias*) EMACS=emacs
			 ;;
	 esac
	 ;;
*alias*) EMACS=nemacs
         ;;
esac
#
case $MULE in
no*)	;;
*)	case $EMACS in
 		no*)	EMACS=$MULE
			EORMU="Mule"
			;;
		*)	echo $n "Do you use Mule or Nemacs [mule] ?$c"
			read answer
			case xx$answer in
				*emacs)	;;
				*)	EMACS=$MULE
					EORMU="Mule"
					;;
			esac
			;;
	esac
	;;
esac

echo $n "Where is your $EORMU [$EMACS] ?$c"
read answer
case xx$answer in
xx)	;;
*)	EMACS=$answer
	;;
esac
if test ! -f $EMACS
then
 echo "$EORMU does not exist.  Check your path."
 exit 1
fi
echo "I found your emacs is \"$EMACS\"."
#
# emacs-version
#
echo "Checking emacs-version ..."
if test -f get-vers.el
then
 rm -f get-vers.el
fi
echo "(message (emacs-version))" > get-vers.el
EVERSION=`$EMACS -batch -l get-vers.el 2>&1 |sed -e "s/GNU Emacs //" -e "s/\..*//"`
case xx$EVERSION in
xx19) if test $SHORTNAME = 1
      then
	cp skk-is-m.el skk-is.el
      else
	cp skk-is-m.el skk-isearch.el
      fi
      echo "Your emacs or mule is based on GNU Emacs 19.xx."
      ;;
*)    if test $SHORTNAME = 1
      then
      	cp skk-is-n.el skk-is.el
      else
	cp skk-is-n.el skk-isearch.el
      fi
      echo "Your emacs or mule is based on GNU Emacs 18.xx."
      ;;
esac
#
# emacs exec-directory
#
echo "Searching for the emacs-exec-directory..."
if test -f getexecdir.el
then
 rm -f getexecdir.el
fi
echo "(message exec-directory)" > getexecdir.el
ETCDIR=`$EMACS -batch -l getexecdir.el 2>&1 | sed -e "s/etc\/\$/etc/"`
 echo $n "Where is the emacs-etc-directory (where SKK.tut etc. will be placed) [$ETCDIR] ?$c"
 read answer
 case xx$answer in
 xx) ;;
 */) ETCDIR=`echo $answer | sed -e "s/\/$//"`
     ;;
 *)  ETCDIR=$answer
     ;;
 esac
 while test ! -d $ETCDIR
 do
  echo "Such a directory does not exist."
  echo $n "Should I create it now (yes/no) [n] ?$c"
  read answer
  case xx$answer in
  xxy*)  if mkdir $ETCDIR
         then
            echo "I created the directory \"$ETCDIR\"."
            break
 	else
 	   echo "I couldn't create \"$ETCDIR\"."
         fi
         ;;
  *)     ;;
  esac
  echo $n E0507
  read answer
  case xx$answer in
  xx)  ;;
  */)  ETCDIR=`echo $answer | sed -e "s/\/$//"`
       ;;
  *)   ETCDIR=$answer
       ;;
  esac
 done
#
EXECDIR=$ETCDIR
 echo $n "Where is the directory for executables (such as emacs-sort) [$EXECDIR] ?$c"
 read answer
 case xx$answer in
 xx) ;;
 */) EXECDIR=`echo $answer | sed -e "s/\/$//"`
     ;;
 *)  EXECDIR=$answer
     ;;
 esac
 while test ! -d $EXECDIR
 do
  echo "Such a directory does not exist."
  echo $n "Should I create it now (yes/no) [n] ?$c"
  read answer
  case xx$answer in
  xxy*)  if mkdir $EXECDIR
         then
            echo "I created the directory \"$EXECDIR\"."
            break
 	else
 	   echo "I couldn't create \"EXECDIR\"."
         fi
         ;;
  *)     ;;
  esac
  echo $n "Input the directory for executables [$EXECDIR] ?$c"
  read answer
  case xx$answer in
  xx)  ;;
  */)  EXECDIR=`echo $answer | sed -e "s/\/$//"`
       ;;
  *)   EXECDIR=$answer
       ;;
  esac
 done
#
#  jisyo directory
#
echo $n "Where should I install the JISYO file skk-server refers [$ETCDIR] ?$c"
read answer
case xx$answer in
xx)  JISYODIR=$ETCDIR
     ;;
*)   JISYODIR=$answer
     ;;
esac
while test ! -d $JISYODIR
do
 echo "Such a directory does not exist."
 echo $n "Should I create it now (yes/no) [n] ?$c"
 read answer
 case xx$answer in
 xxy*)  if mkdir $JISYODIR
        then
           echo "I created the directory \"$JISYODIR\"."
           break
	else
	   echo "I couldn't create \"$JISYODIR\"."
        fi
        ;;
 *)     ;;
 esac
 echo $n "Input JISYO-directory [$ETCDIR] ?$c"
 read answer
 case xx$answer in
 xx)  JISYODIR=$ETCDIR
      ;;
 *)   JISYODIR=$answer
      ;;
 esac
done
#
#  lisp, man, and info directory
#
LISPDIR=`echo $ETCDIR | sed -e "s/\/etc\$/\/lisp/"`
MANDIR=`echo $ETCDIR | sed -e "s/\/etc\$/\/man/"`
INFODIR=`echo $ETCDIR | sed -e "s/\/etc\$/\/info/"`
echo $n "Where is the directory for Emacs-lisp libraries [$LISPDIR] ?$c"
read answer
case xx$answer in
xx) ;;
*/) LISPDIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  LISPDIR=$answer
    ;;
esac
while test ! -d $LISPDIR 
do
 echo "Such a directory does not exist."
 echo $n "Should I create it now (yes/no) [n] ?$C"
 read answer
 case xx$answer in
 xxy*)  if mkdir $LISPDIR
        then
           echo "I created the directory \"$LISPDIR\"."
           break
        else
	   echo "I couldn't create $LISPDIR"
        fi
        ;;
 *)     ;;
 esac
 echo $n "Input LISP directory [$LISPDIR] ?$c"
 read answer
 case xx$answer in
 xx)  ;;
 */)  LISPDIR=`echo $answer | sed -e "s/\/$//"`
      ;;
 *)   LISPDIR=$answer
      ;;
 esac
done
#
echo $n "Where is the directory for Emacs-man [$MANDIR] ?$c"
read answer
case xx$answer in
xx) ;;
*/) MANDIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  MANDIR=$answer
    ;;
esac
while test ! -d $MANDIR
do
 echo "Such a directory does not exist."
 echo $n "Can I create it now (yes/no) [n] ?$c"
 read answer
 case xx$answer in
 xxy*) if mkdir $MANDIR
       then
         echo "I created the directory \"$MANDIR\"."
         break;
       else
	 echo "I couldn't create $MANDIR"
       fi
       ;;
 *)    ;;
 esac
 echo $n "Input Emacs-man directory [$MANDIR] ?$c"
 read answer
 case xx$answer in
 xx)  ;;
 */)  MANDIR=`echo $answer | sed -e "s/\/$//"`
      ;;
 *)   MANDIR=$answer
      ;;
 esac
done
#
echo $n "Where is the directory for Emacs-info [$INFODIR] ?$c"
read answer
case xx$answer in
xx) ;;
*/) INFODIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  INFODIR=$answer
    ;;
esac
while test ! -d $INFODIR
do
 echo "Such a directory does not exist."
 echo $n "Can I create it now (yes/no) [n] ?$c"
 read answer
 case xx$answer in
 xxy*) if mkdir $INFODIR
       then
         echo "I create the directory \"$INFODIR\"."
         break;
       else
	 echo "I couldn't create $INFODIR"
       fi
       ;;
 *)    ;;
 esac
 echo $n "Input Emacs-info directory [$INFODIR] ?$c"
 read answer
 case xx$answer in
 xx)  ;;
 */)  INFODIR=`echo $answer | sed -e "s/\/$//"`
      ;;
 *)   INFODIR=$answer
      ;;
 esac
done
#
#  install skkserv ?
#	INSTALLSERV=1 with skkserv, 2 without skkserv
echo $n "Do you want to use skk jisyo server (yes/no) [y] ?$c"
read answer
case xx$answer in
xxn*)  echo "Really ?  I STRONGLY recommend you to use it."
     echo ""
     echo $n "Do you want to use skk jisyo server (yes/no) [y] ?$c"
     read answer
     case xx$answer in
     xxn*)  echo "O.K.  It's on your decision, though I'm sad."
	  echo "You will regret this decision later."
	  INSTALLSERV=2
          ;;
     *)   echo "I'll install SKK with skkserv."
	  INSTALLSERV=1
          ;;
     esac
     ;;
*)   echo "I'll install SKK with skkserv."
     INSTALLSERV=1
     ;;
esac
#
#  server directory
#
if test $INSTALLSERV = "1"
then
 echo $n "To which directory should I install skkserv [$ETCDIR] ?$c"
 read answer
 case xx$answer in
 xx) SERVERDIR=$EXECDIR
     ;;
 */) SERVERDIR=`echo $answer | sed -e "s/\/$//"`
     ;;
 *)  SERVERDIR=$answer
     ;;
 esac
 while test ! -d $SERVERDIR
 do
  echo "Such a directory does not exist."
  echo $n "Can I create it now (yes/no) [n] ?$c"
  read answer
  case xx$answer in
  xxy*) if mkdir $SERVERDIR
        then
          echo "I create the directory \"$SERVERDIR\"."
          break;
        else
 	 echo "I couldn't create $SERVERDIR"
        fi
        ;;
  *)    ;;
  esac
  echo $n "Input SERVER directory [$SERVERDIR] ?$c"
  read answer
  case xx$answer in
  xx)  ;;
  */)  SERVERDIR=`echo $answer | sed -e "s/\/$//"`
       ;;
  *)   SERVERDIR=$answer
       ;;
  esac
 done
 #
 # jisho
 #
 echo ""
 echo "There are several kinds of JISYO files provided in this release."
 echo "	SKK-JISYO.L	Large Size (preferable)"
 echo "	SKK-JISYO.M	Medium Size"
 echo "	SKK-JISYO.S	Small Size"
 echo "	SKK-JISYO.JIS2	JIS 2nd Level (not suitable to be used solely)"
 echo $n "Which one do you want to use as SKK Server's JISYO [SKK-JISYO.L] ?$c"
 read answer
 case xx$answer in
 xx)  JISYO=SKK-JISYO.L
      ;;
 *)   JISYO=$answer
      ;;
 esac
 while test ! -f $JISYO
 do
  echo "Such a file does not exist."
  echo $n "Input JISYO file [SKK-JISYO.L] ?$c"
  read answer
  case xx$answer in
  xx)  JISYO=SKK-JISYO.L
       ;;
  *)   JISYO=$answer
       ;;
  esac
 done
 #
 #  how to define services
 #  NIS types 	0: already defined (in /etc/services for non-NIS system
 # 				 and in NIS map for NIS system)
 #		1: not use NIS 
 #		2: NIS master server
 #		3: NIS not master server
 #
 if test $USER = "root"
 then
  DOMAIN=`domainname`
  case `which ypwhich` in
  n*|*alias*) if grep "^skkserv" /etc/services > /dev/null
      then
       NIS=0
      else
       NIS=1
      fi
      ;;
  *)  if ypwhich > /dev/null
      then
       if ypcat services | grep "^skkserv" > /dev/null
       then
	NIS=0
       else
	echo "It seems that your host is running NIS (Yellow Pages)."
        echo "You should modify the file \"/etc/services\" on the master "
	echo "server of NIS/YP.  I don't know which machine is the master,"
	echo "so, please give me the information."
        echo $n "Is your host the master server of NIS/YP (yes/no) [y] ?$c"
  	read answer
  	case xx$answer in
  	xxn*) echo "O.K. I'll ask you to change NIS(YP) map on the master server"
	      echo ""
 	      NIS=3
              ;;
        *)    NIS=2
              ;;
        esac
       fi
      else
       NIS=1
      fi
      ;;
  esac
 else
    NIS=0
 fi
 #
 #  C compiler
 #
 LOOP=1
 while test $LOOP = "1"
 do 
  echo $n "Which C compiler do you use [cc] ?$c"
  read answer
  case xx$answer in
  xx) 	CC=cc
  	CCFLAG=-O
      	;;
  xxgcc)	CC=gcc
  	CCFLAG=-traditional
         	;;
  *)  	CC=$answer
      	;;
  esac
  case `which $CC` in
  n*|*alias*) 	echo "Scuh a file does not exist in your path."
      		;;
  *)  		LOOP=0
      		;;
  esac
 done
else
 SERVERDIR=$ETCDIR
 JISYO=SKK-JISYO.L
 NIS=0
 CC=cc
fi
#
#  remake Makefile
#
if test -f Makefile.new
then
 rm -f Makefile.new
fi
(echo "# Makefile.new"
 echo "# Customized Makefile created by Configure" 
 echo EMACS=$EMACS 		
 echo LISPDIR=$LISPDIR 		
 echo JISYO=$JISYO 		
 echo JISYODIR=$JISYODIR
 echo ETCDIR=$ETCDIR
 echo EXECDIR=$EXECDIR
 echo INFODIR=$INFODIR
 echo SERVERDIR=$SERVERDIR		
 echo MANDIR=$MANDIR 		
 echo CC=$CC	 		
 echo CCFLAG=$CCFLAG 		
 echo NIS=$NIS 			
 grep 	-v "^EMACS="		Makefile | \
 grep	-v "^LISPDIR="			 | \
 grep	-v "^JISYO="			 | \
 grep	-v "^JISYODIR="			 | \
 grep	-v "^ETCDIR="			 | \
 grep	-v "^EXECDIR="			 | \
 grep	-v "^INFODIR="			 | \
 grep	-v "^SERVERDIR="		 | \
 grep	-v "^MANDIR="			 | \
 grep	-v "^CC="			 | \
 grep	-v "^CCFLAG="			 | \
 grep	-v "^NIS="			) > Makefile.new
# short name
if test $SHORTNAME = 1
then
 rm -f .temporal
 sed -e "s/skk-isearch/skk-is/" Makefile.new > .temporal
 mv .temporal Makefile.new
fi
# confirmation
echo ""
echo ""
echo "Thank you for your answers.  Q&A is over."
echo "I created the file \"Makefile.new\" as the result of Q&A."
echo "I'll do the installation as follows:"
echo ""
echo "	install SKK.* into $ETCDIR"
echo "	install executables into $EXECDIR"
echo "	byte-compile *.el files to make *.elc files"
echo "	install *.el and *.elc files into $LISPDIR"
echo "	install manuals into $MANDIR"
echo "	install skk into $INFODIR"
if test $INSTALLSERV = "1"
then
 echo "	compile and install the server into $SERVERDIR"
 echo "	install $JISYO (which is referred by skkserv) into $JISYODIR"
fi
#
if test $USER = "root"
then
 echo "In addition to them, I can set up the following:"
 if test $INSTALLSERV = "1"
 then
  echo "	add the following entry into /etc/services or NIS map"
  echo "		skkserv	1178/tcp"
  echo "	add several lines to /etc/rc.local which automatically "
  echo "	invoke skkserv at system-start"
 fi
 echo "I'll ask you to confirm before I modify /etc/services and /etc/rc.local."
 echo "Don't worry!"
else
 echo "This is private installation for you."
 if test $ETCDIR != $EXECDIR
 then
  echo E1511
  echo "	(setq skk-emacs-sort \"$EXECDIR/emacs-sort\")"
 fi
 if test $INSTALLSERV = "1"
 then
  echo "Add the following line to your \".skk\" file by yourself:"
  echo "	(setq skk-portnum 1178)"
  echo ""
  echo "And you will have to invoke \"skkserv\" by yourself, if the automatic invocation"
  echo "by SKK fails.  (This is not necessary in usual cases.)"
 fi
fi
echo ""
echo "Please confirm the procedure above carefully."
echo "Now, I'm ready to make and install SKK."
echo $n "Can I proceed (yes/no) [y] ?$c"
read answer
case xx$answer in
xxn*) echo "You can edit \"Makefile.new\" by hand, "
      echo "and do"
      echo ""
      if test $USER = "root"
      then
        echo "	make -f Makefile.new public"
      else
        echo "	make -f Makefile.new private"
      fi
      echo ""
      echo "Then, SKK system will be installed."
      echo "Please do NOT modify Makefile itself if you use Configure later."
      echo ""
      if test $USER = "root"
      then
 echo "By the way, you had better add the following line to $INFODIR/info"
 echo "file, for I cannot do it intelligently."
 echo "	* SKK: (skk).	Japanese Input System for Nemacs/Mule."
 echo ""
      fi
      exit 1
      ;;
esac
# 
echo "Making and installing skk..."
RESULT=0
if test $INSTALLSERV = "1"
then
   if test $USER = "root"
   then
	if make -f Makefile.new public
	then
	   RESULT=1
	fi
   elif make -f Makefile.new private
   then
	RESULT=1
   fi
elif make -f Makefile.new install-skk
then
   RESULT=1
fi
#
if test $RESULT = 1
then
 echo "I have finished my work. Installation is done."
 echo "Each user should do his/her own setting."
 #
 if test $USER = "root"
 then
 echo "By the way, you had better add the following line to $INFODIR/info"
 echo "file, for I cannot do it intelligently."
 echo "	* SKK: (skk).	Japanese Input System for Nemacs/Mule."
 echo ""
  echo ""
 fi
 echo "Read \"skk.tex\" for details."
 echo "Do not forget to quit superuser shell."
else
 echo ""
 echo "SORRY! I failed to make & install SKK."
 echo "Please check the steps above."
 echo "You may refer the file \"Makefile.new\"".
 echo "And if you find problems, you may edit it and do"
 echo ""
 if test $USER = "root"
 then
  echo "	make -f Makefile.new public"
 echo "By the way, you had better add the following line to $INFODIR/info"
 echo "file, for I cannot do it intelligently."
 echo "	* SKK: (skk).	Japanese Input System for Nemacs/Mule."
 echo ""
 else
  echo "	make -f Makefile.new private"
 fi
 echo "to re-install the staffs."
fi
#
echo "Bye bye."

exit 0
