#!/bin/sh
#
#  Configure: SKK Configuration Script for skk 4.30 and skkserv 3.5
#
#  Written by Yukiyoshi Kameyama (kam@tohoku.ac.jp)
#  Inspired by the SKK configuration program "skkconf" 
#	created by Jun Takemura (jun@hakuba.cat.co.jp)
#
#  check existence of source files
#
set COPYING Configure Makefile ReadMe SKK.tut \
	SKK-JISYO.L SKK-JISYO.M SKK-JISYO.S SKK-JISYO.JIS2 \
	skk-romkan.tex skk.tex \
	skk.el skk-isearch.el skk-tut.el skk-tools.el \
	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."
  exit 1
 fi
done
#
#  hello
#
echo "Hi, I am SKK Configuration Script."
echo ""
echo "I'll ask several questions to you, and make a new file \"Makefile.new\""
echo "which is suitable for your site."
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 ""
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."
echo ""
echo "Your terminal should have 20 lines to see messages"
echo ""
echo -n "Are you ready [y] ?"
read answer
case xx$answer in
xxy*)  echo "Hahaha, you don't have to type \"y\" if you select the default !"
       ;;
xxn*)  echo "I ignore your answer, if you want to stop, kill me."
       sleep 10
       ;;
esac
#
#  rename skk-isearch.el if more than 14char files name is not allowed.
#
echo "" > this-is-fifteen
if test ! -f this-is-fifteen
then
 echo "It seems that your system does not allow long filenames."
 echo "I'll rename skk-isearch.el into skk-isrch.el"
 if test -f skk-isrch.el
 then
  echo "The file skk-isrch.el already exists."
  echo -n "Do you want me to overwrite it (yes/no) [n] ?"
  read answer
  case $answer in
  y*) 	rm skk-isrch.el
	;;
  *)	echo "O.K.  You should move or remove skk-isrch.el by hand."
	exit 1
	;;
  esac
 fi
 mv skk-isearch.el skk-isrch.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] ?"
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
#
# emacs exec-directory
#
echo "Just for a moment."
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/"`
if test ! -d $ETCDIR
then 
 echo "The emacs-exec-directory $ETCDIR does not exist."
 echo "Something funny happens."
 exit 1
fi
echo "Wow, I know emacs-exec-directory is $ETCDIR"
#
# jisho
#
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] ?"
read answer
case xx$answer in
xx)  JISYO=SKK-JISYO.L
     ;;
*)   JISYO=$answer
     ;;
esac
if test ! -f $JISYO
then
 echo "Such a JISYO file does not exist."
 exit 1
fi
echo -n "Where do you want to install JISYO files [$ETCDIR] ?"
read answer
case xx$answer in
xx)  JISYODIR=$ETCDIR
     ;;
*)   JISYODIR=$answer
     if test ! -d $JISYODIR
     then
	echo "Such a directory does not exist."
	exit 1
     fi
     ;;
esac
#
#  lisp and man directory
#
LISPDIR=`echo $ETCDIR | sed -e "s/\/etc\$/\/lisp/"`
MANDIR=`echo $ETCDIR | sed -e "s/\/etc\$/\/man/"`
echo -n "Where is the directory for Emacs-lisp libraries [$LISPDIR] ?"
read answer
case xx$answer in
xx) ;;
*/) LISPDIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  LISPDIR=$answer
    ;;
esac
if test ! -d $LISPDIR
then
 echo "Such a directory does not exist."
 exit 1
fi
echo -n "Where is the directory for Emacs related manuals [$MANDIR] ?"
read answer
case xx$answer in
xx) ;;
*/) MANDIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  MANDIR=$answer
    ;;
esac
if test ! -d $MANDIR
then
 echo "Such a directory does not exist."
 echo -n "Do you want me to create it now [y] ?"
 read answer
 case $answer in
 n*)  echo "O.K.  You should start from the beginning. Good bye."
      exit 1
      ;;
 *)   if mkdir $MANDIR;
      then
        echo "I made the directory \"$MANDIR\"."
      else
	echo "I cannot create $MANDIR"
	exit 1
      fi
      ;;
 esac
fi
#
#  server directory
#
echo -n "To which directory do you want to install skkserv [$ETCDIR] ?"
read answer
case xx$answer in
xx) SERVERDIR=$ETCDIR
    ;;
*/) SERVERDIR=`echo $answer | sed -e "s/\/$//"`
    ;;
*)  SERVERDIR=$answer
    ;;
esac
if test ! -d $SERVERDIR
then
 echo "Such a directory does not exist."
 exit 1
fi
#
#  how to define services
#  NIS types 	1: not use NIS
#		2: NIS master
#		3: NIS slave
#
USER=`whoami`
if test $USER = "root"
then
 DOMAIN=`domainname`
 case `which ypwhich` in
 n*) NIS=1
     ;;
 *)  echo -n "Are you using NIS(YP) [y] ?"
     read answer
     case $answer in
     n*)  NIS=1
 	 ;;
     *)  echo "You can modify the \"services\" file on NIS(YP) master server only."
         echo -n "Is your host the NIS(YP) master server [y] ?"
 	 read answer
 	 case $answer in
 	 n*) echo "O.K. I'll ask you to change NIS(YP) map at installtion"
	     echo NIS=2
             ;;
         *)  NIS=3
             ;;
         esac
	 ;;
     esac
     ;;
 esac
else
 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 [n] ?"
 read answer
 case $answer in
 y*)  ;;
 *)   echo "Then, you should become the super user before running Configure"
      exit 1
      ;;
 esac
 NIS=0
fi
#
#  C compiler
#
echo -n "Which C compiler do you use [cc] ?"
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"
    exit 1
    ;;
esac
#
#  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/skk-isrch/" Makefile.new > .temporal
 mv .temporal Makefil.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 "	copy *.el and *.elc files into $LISPDIR"
echo "	compile and install the server into $SERVERDIR"
echo "	install SKK.* into $JISYODIR"
echo "	install manuals into $MANDIR"
#
if test $USER = "root"
then
 echo "In addition to that, I can set up the following:"
 echo "	add the following entry into /etc/services or NIS map"
 echo "		skkserv	1178/tcp"
 echo "	add several lines which automatically invoke skkserv at system-start"
 echo "I'll ask you whether I should do them at the installation time."
 echo "Don't worry!"
 echo ""
else
 echo "You will install skk privately, so you should add the following line"
 echo "into your \".skk\" file"
 echo "	(setq skk-portnum 1178)"
 echo "And you must invoke \"skkserv\" yourself, if the automatic invocation"
 echo "by SKK fails.  (This is usually not necessary.)"
fi
echo "Please confirm the procedure above carefully."
echo "Now, I'm ready to make and install SKK."
echo -n "Can I proceed [y] ?"
read answer
case xx$answer in
xxn*) echo "You can edit \"Makefile.new\" by hand, and do "
      echo "	make -f Makefile.new everything"
      echo "Please DO NOT modify Makefile itself if you use Configure later."
      exit 1
      ;;
esac
# 
echo -n "Do you want to use skk jisyo server [y] ?"
read answer
case $answer in
n*)  echo "Really ?  I strongly recommend you to use it"
     echo -n "Do you want to use \"skkserv\" [y] ?"
     read answer
     case $answer in
     n*) echo "O.K.  It's on your decision."
         make -f Makefile.new skk
         make -f Makefile.new install-skk
         make -f Makefile.new skkman
	 exit 0
         ;;
     esac
     ;;
esac
if test $USER = "root"
then
 make -f Makefile.new everything
else
 make -f Makefile.new private
fi
# 
echo "I have finished my work. Installation is done."
echo "Each user should do his/her own setting."
echo "You can refer the file \"for-individual\" for individual settings."
if test $USER = "root"
then
 echo "Do not forget to quit superuser shell."
fi

if test -f 'for-indivual'
then
	echo "will not over-write existing file \"for-individual\""
else
cat > for-individual << \SHAR_EOF
touch $(HOME)/.skk-record
touch $(HOME)/.skk-jisyo
touch add-emacs
touch add-skk
echo "; This file is to be added to ~/.emacs" >> add-emacs
echo "(global-set-key "\C-x\C-j" 'skk-mode) " >> add-emacs
echo "(global-set-key "\C-xj" 'skk-auto-fill-mode) " >> add-emacs
echo "(global-set-key "\C-xt" 'skk-tutorial) " >> add-emacs
echo "(global-set-key "\C-s" 'skk-isearch-forward) " >> add-emacs
echo "(global-set-key "\C-r" 'skk-isearch-backward) " >> add-emacs
echo "(autoload 'skk-mode "skk" nil t) " >> add-emacs
echo "(autoload 'skk-tutorial "skk-tut" nil t) " >> add-emacs
echo "(autoload 'skk-isearch-forward "skk-isearch" nil t) " >> add-emacs
echo "(autoload 'skk-isearch-backward "skk-isearch" nil t) " >> add-emacs
echo "(autoload 'skk-check-jisyo "skk-tools" nil t) " >> add-emacs
echo "(autoload 'skk-merge "skk-tools" nil t) " >> add-emacs
echo "(autoload 'skk-diff "skk-tools" nil t) " >> add-emacs
echo "; This file is to be added to ~/.skk" >> add-skk
echo "(setq skk-server-host "localhost")" >> add-skk
echo "(setq skk-serv "$SERVERDIR/skkserv")" >> add-skk
SHAR_EOF
chmod 644 for-individual
fi

exit 0
