#!/bin/sh

#set -e
psf_inst_par=.psf_install_parameters
x11configdir='???'

if	[ "`echo -n`" = "-n" ]
then	n=	c=\\c
else	n=-n	c=
fi

if 	[ -f "$psf_inst_par" ]
then	exec 3<"$psf_inst_par"
	read srcdir <&3
	read destdir <&3
	read x11configdir <&3
	read prog_perl <&3
	read prog_mknod <&3
	read prog_wish <&3
	exec 3<&-
else	srcdir=`pwd`
	destdir=`dirname "$srcdir"`
	x11configdir=`cd /; xmkmf 2>&1 | head -1 | sed 's/.*-I//' |
			 sed 's/ 	]//g'`
fi

if	[ ! -d "$x11configdir"  -o ! -f "$x11configdir/Imake.tmpl" \
				-o ! -f "$x11configdir/Imake.rules" ]
then
	xmkmf_says=`cd /; xmkmf 2>&1 | head -1`

	case "$xmkmf_says" in
		*"not found"*)						;;
		*"-DUseInstalled -I/"*)	x11configdir=`echo "$xmkmf_says" |
						sed 's+.*-I/+/+'`	;;
		*"-DUseInstalled /"*)	x11configdir=`echo "$xmkmf_says" |
						sed 's+.* /+/+'`	;;
	esac
fi

if	[ ! -d "$x11configdir" -o ! -f "$x11configdir/Imake.tmpl" \
			       -o ! -f "$x11configdir/Imake.rules" ]
then
	echo I cannot find the X11 configuration directory.
	echo This directory should contain the files Imake.rules and Imake.tmpl.
	echo Please type the name of that directory.  Alternatively, if you
	echo type an empty line, `basename $0` will stop.  You can then find
	echo the directory and start over.
	echo
	echo $n "Where are the X11 configuration files? [ I don't know ] " $c
	read answer
	if [ -z "$answer" ]
	then	exit
	else	x11configdir="$answer"
	fi
fi

#locate perl
prog="perl"
word="perl version 5.x"
prog_perl="no"
echo "searching for $word"
IFS="${IFS=   }"; save_ifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/$prog; then
	prog_perl="$dir/$prog"
	break
    fi
done
if test "$prog_perl" != "no"; then
    echo "    checking version of $prog_perl"
    vl=`$prog_perl -v | grep "This is perl, version 5."`
    if test -z "$vl"; then
	vl=`$prog_perl -v | grep "This is perl, v5."`
	if test -z "$vl"; then
            vl=`$prog_perl -v | grep "This is perl 5, version"`
            if test -z "$vl"; then
                prog_perl=no
            fi
	fi
    fi
fi
if test "$prog_perl" = "no"; then
    echo "    searching alternatives"
    for dir in $PATH; do
	test -z "$dir" && dir=.
	if test -f $dir/${prog}5; then
	    prog_perl="$dir/${prog}5"
	    break
	fi
    done
fi
IFS="$save_ifs"
test -z "$prog_perl" && prog_perl="no"
if [ $prog_perl = "no" ]
then
    echo I cannot find $word.
    echo Please type the location of $word. Alternatively, if you
    echo type an empty line, `basename $0` will stop. You can then find
    echo $word and start over.
    echo If you type n or N, installation will continue, but programs
    echo which need $word or not made.
    echo
    echo $n "What is the location of $word? [ I don't know ] " $c
    read answer
    if [ -z "$answer" ]
    then exit
    else
	case "$answer" in
	n|N)
	    prog_perl="no";;
	*)
	    prog_perl="$answer";;
	esac
    fi
fi
echo "$word : $prog_perl"

#locate mknod
prog="mknod"
word="mknod"
prog_mknod="no"
echo "searching for $word"
save_path="$PATH"
PATH="/etc:/usr/etc:/usr/sbin:$PATH"
IFS="${IFS=   }"; save_ifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/$prog; then
	prog_mknod="$dir/$prog"
	break
    fi
done
IFS="$save_ifs"
PATH="$save_path"
test -z "$prog_mknod" && prog_mknod="no"
if [ $prog_mknod = "no" ]
then
    echo I cannot find $word.
    echo Please type the location of $word. Alternatively, if you
    echo type an empty line, `basename $0` will stop. You can then find
    echo $word and start over.
    echo If you type n or N, installation will continue, but programs
    echo which need $word or not made.
    echo
    echo $n "What is the location of $word? [ I don't know ] " $c
    read answer
    if [ -z "$answer" ]
    then exit
    else
	case "$answer" in
	n|N)
	    prog_mknod="no";;
	*)
	    prog_mknod="$answer";;
	esac
    fi
fi
echo "$word : $prog_mknod"

#locate wish
prog="wish"
word="wish version 8.x"
echo "searching for $word"
prog_wish="no"
IFS="${IFS=   }"; save_ifs="$IFS"; IFS="${IFS}:"
for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/${prog}; then
	prog_wish="$dir/${prog}"
	break
    fi
done
if test "$prog_wish" != "no"; then
    echo "    checking version of $prog_wish"
    echo 'wm withdraw . ; puts "$tcl_version" ; exit' > conftestac-tcltk$$
    vl=`$prog_wish conftestac-tcltk$$ | grep "8."`
    rm conftestac-tcltk$$
    if test -z "$vl"; then
        prog_wish=no
    fi
fi
if test "$prog_wish" = "no"; then
    echo "    searching alternatives"
    for dir in $PATH; do
	test -z "$dir" && dir=.
	if test -f $dir/${prog}8.3; then
	    prog_wish="$dir/${prog}8.3"
	    break
	elif test -f $dir/${prog}8.2; then
	    prog_wish="$dir/${prog}8.2"
	    break
	elif test -f $dir/${prog}8.1; then
	    prog_wish="$dir/${prog}8.1"
	    break
	elif test -f $dir/${prog}8.0; then
	    prog_wish="$dir/${prog}8.0"
	    break
	fi
    done
fi
IFS="$save_ifs"
test -z "$prog_wish" && prog_wish="no"
if [ $prog_wish = "no" ]
then
    echo I cannot find $word.
    echo Please type the location of $word. Alternatively, if you
    echo type an empty line, `basename $0` will stop. You can then find
    echo $word and start over.
    echo If you type n or N, installation will continue, but programs
    echo which need $word or not made.
    echo
    echo $n "What is the location of $word? [ I don't know ] " $c
    read answer
    if [ -z "$answer" ]
    then exit
    else
	case "$answer" in
	n|N)
	    prog_wish="no";;
	*)
	    prog_wish="$answer";;
	esac
    fi
fi
echo "$word : $prog_wish"

set -e
echo
echo "The source of the PSF tools is in $srcdir"
echo "The PSF tools will be installed in $destdir, that is,"
echo "	executables will be installed in $destdir/bin"
echo "	auxilliary files will be installed in $destdir/lib"
echo "	manual pages will be installed in $destdir/man"
echo "The X11 configuration files are in $x11configdir"
echo
echo $n "Is that correct? [y] " $c

read answer
case $answer in
     [yY]*|'') ;;
     ?*)echo "In which directory can I find the source of the PSF tools?"
	echo $n "	[$srcdir] " $c
	read answer
	srcdir="${answer:-$srcdir}"

	echo "In which directory should the PSF tools be installed?"
	echo $n "	[$destdir] " $c
	read answer
	case "$answer" in
		*/bin)	destdir=`dirname "$answer"`
			echo "We'll make that $destdir" ;;
		?*)	destdir="$answer" ;;
	esac

	echo "In which directory can I find the X11 configuration files?"
	echo $n "	[$x11configdir] " $c
	read answer
	x11configdir="${answer:-$x11configdir}"
esac

if	[ ! -f "$x11configdir/Imake.tmpl" -o ! -f "$x11configdir/Imake.rules" ]
then	( echo "Hmmm.... the files $x11configdir/Imake.tmpl and"
	  echo "$x11configdir/Imake.rules do not exist.  Hope that's OK."
	) | fmt -77
	echo
fi

echo "$srcdir"		>  $psf_inst_par
echo "$destdir"		>> $psf_inst_par
echo "$x11configdir"	>> $psf_inst_par
echo "$prog_perl"	>> $psf_inst_par
echo "$prog_mknod"	>> $psf_inst_par
echo "$prog_wish"	>> $psf_inst_par

psfconfigdir="$srcdir/imake"

cd "$srcdir"

test -f README && chmod +w README
test -f Imakefile && chmod +w Imakefile

sed -e "s+@DESTINATION@+$destdir+" \
    -e "s+@MASTERDIR@+$srcdir+" README.tmpl > README
sed -e "s+@MASTERDIR@+$srcdir+" -e "s+@DESTINATION@+$destdir+" \
    -e "s+@XCONFIGDIR@+$x11configdir+" -e "s+@PROG_PERL@+$prog_perl+" \
    -e "s+@PROG_MKNOD@+$prog_mknod+" -e "s+@PROG_WISH@+$prog_wish+" \
    Imakefile.tmpl > Imakefile

chmod 444 README Imakefile

imake "-I$x11configdir" "-I$psfconfigdir" "-DUseInstalled" \
	"-DTOPDIR=$srcdir" "-DCURDIR=$srcdir" "-DDestination=$destdir" \
	"-DMasterDir=$srcdir" "-DCONFIGDIR=MasterDir/src/imake" \
	"-DXconfigDir=$x11configdir" "-DProg_Perl=$prog_perl" \
	"-DProg_Mknod=$prog_mknod" "-DProg_Wish=$prog_wish"

set -x

if	[ -d distribute ]
then	echo "#define THIS_IS_THE_DEVELOPMENT_SITE" > .develop_site
	exit
fi

echo "/* this_is_not_the_development_site */" > .develop_site
(	cd src/imake
	imake "-I$x11configdir" "-I$psfconfigdir" "-DUseInstalled" \
		"-DTOPDIR=.." "-DCURDIR=imake" "-DDestination=$destdir" \
		"-DMasterDir=$srcdir" "-DCONFIGDIR=MasterDir/src/imake" \
		"-DProg_Perl=$prog_perl" "-DProg_Mknod=$prog_mknod" \
		"-DProg_Wish=$prog_wish"
	make all
)

make Makefiles
make depend
make all
make install
make install.man
# make test		# not yet implemented
