#!/bin/sh
#
#  Configure Version 1.4 of May 9, 1992
#  SKK Configuration Script for skk 5.x and skkserv 3.x
#
#  Written by Yukiyoshi Kameyama (kam@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
#
########################################################################
#
# "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
#
set COPYING Configure Configure.euc Makefile ReadMe ReadMe.English \
	SKK-JISYO.L SKK-JISYO.M SKK-JISYO.S SKK-JISYO.JIS2 \
	SKK.tut \
	skk.el skk-isearch.el s-isearch.el skk-tut.el skk-tools.el \
	skk.elc s-isearch.elc skk-tut.elc skk-tools.elc \
	skk-romkan.tex skk.tex skk.ps latexinfo.sty tabular.sty \
	skk \
	skkserv/Makefile skkserv/skkserv.h skkserv/skkserv.c \
	skkserv/add-services skkserv/add-rc.local skkserv/README
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 space in your answers."
echo ""
echo "If you want to quit this script, you can kill me."
echo "Before I'll actually do file modification and installation,"
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
#
USER=`whoami`
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 more than a 14char-name is allowed or not.
#
if test ! -f skk-isearch.elc
then
 echo "It seems that your system does not allow long filenames."
 echo "Use \"s-isearch.el\" instead of \"skk-isearch.el\"."
 SHORTNAME=1
else
 SHORTNAME=0
fi
#
#  emacs path
#
EMACS=`which nemacs`
case $EMACS in
n*) EMACS=`which emacs`
 case $EMACS in
 n*)	EMACS=/usr/local/nemacs
	;;
 esac
 ;;
esac
echo $n "Where is your Nemacs [$EMACS] ?$c"
read answer
case xx$answer in
xx)	;;
*)	EMACS=$answer
	;;
esac
if test ! -f $EMACS
then
 echo "Nemacs does not exist.  Check your path."
 exit 1
fi
echo "I found your emacs is \"$EMACS\"."
#
# 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 | sed -e "s/etc\/\$/etc/"`
echo $n "Where is the emacs-exec-directory [$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 made the directory \"$JISYODIR\"."
           break
	else
	   echo "I couldn't create \"$JISYODIR\"."
        fi
        ;;
 *)     ;;
 esac
 echo $n "Input emacs-exec-directory [$ETCDIR] ?$c"
 read answer
 case xx$answer in
 xx)  ;;
 */)  ETCDIR=`echo $answer | sed -e "s/\/$//"`
      ;;
 *)   ETCDIR=$answer
      ;;
 esac
done
#
#  jisyo directory
#
echo $n "Where should I install JISYO files [$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 made 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 made 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-manuals [$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 made the directory \"$MANDIR\"."
         break;
       else
	 echo "I couldn't create $MANDIR"
       fi
       ;;
 *)    ;;
 esac
 echo $n "Input 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 made 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 ?
#	INSTALL=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."
	  INSTALL=2
          ;;
     *)   echo "I'll install SKK with skkserv."
	  INSTALL=1
          ;;
     esac
     ;;
*)   echo "I'll install SKK with skkserv."
     INSTALL=1
     ;;
esac
#
#  server directory
#
if test $INSTALL = "1"
then
 echo $n "To which directory should I install skkserv [$ETCDIR] ?$c"
 read answer
 case xx$answer in
 xx) SERVERDIR=$ETCDIR
     ;;
 */) 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 made 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 	1: not use NIS or already defined 
 #		2: NIS master
 #		3: NIS slave
 #
 if test $USER = "root"
 then
  DOMAIN=`domainname`
  case `which ypwhich` in
  n*) NIS=1
      ;;
  *)  if ypwhich > /dev/null
      then
       if ypcat services | grep "^skkserv" > /dev/null
       then
	NIS=1
       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=2
              ;;
        *)    NIS=3
              ;;
        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*) 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 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 "^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/s-isearch/" Makefile.new > .temporal
 mv .temporal Makefile.new
fi
# confirmation
echo ""
echo ""
echo "Thank you for your answers.  Q&A is over."
echo "I made the file \"Makefile.new\" as the result of Q&A."
echo "I'll do the installation as follows:"
echo ""
echo "	copy *.el and *.elc files into $LISPDIR"
if test $INSTALL = "1"
then
  echo "	compile and install the server into $SERVERDIR"
fi
echo "	install $JISYO into $JISYODIR"
echo "	install SKK.* into $ETCDIR"
echo "	install skk into $INFODIR"
echo "	install manuals into $MANDIR"
#
if test $INSTALL = "1"
then
 if test $USER = "root"
 then
  echo ""
  echo "In addition to them, I can set up the following:"
  echo "	add the following entry into /etc/services or NIS map"
  echo ""
  echo "		skkserv	1178/tcp"
  echo ""
  echo "	add several lines which automatically invoke skkserv at system-start"
  echo ""
  echo ""
  echo "I'll ask you whether I can do them at the installation time."
  echo "Don't worry!"
 else
  echo "I'll install skk for your private use, so you should add the "
  echo "following line into your \".skk\" file by yourself:"
  echo ""
  echo "	(setq skk-portnum 1178)"
  echo ""
  echo "And you must 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, and do "
      echo ""
      echo "	make -f Makefile.new everything"
      echo ""
      echo "Please DO NOT modify Makefile itself if you use Configure later."
      echo ""
      echo ""
      echo ""
      exit 1
      ;;
esac
# 
echo "Making and installing skk..."
RESULT=0
if test $INSTALL = "1"
then
   if test $USER = "root"
   then
	if make -f Makefile.new everything
	then
	   RESULT=1
	fi
   elif make -f Makefile.new private
   then
	RESULT=1
   fi
elif make -f Makefile.new skk
then
    if make -f Makefile.new install-skk
    then
	if make -f Makefile.new skkman
	then
	    RESULT=1
        fi
    fi
fi
#
if test $RESULT = 1
then
 echo "I have finished my work. Installation is done."
 echo "Each user should do his/her own setting."
 echo "Read \"skk.tex\" for details."
 echo "Do not forget to quit superuser shell."
 #
 if test $USER = "root"
 then
  echo ""
 fi
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 "	make -f Makefile.new"
 echo "to re-install the staffs."
 echo ""
fi
#
echo "Bye bye."

exit 0
