#!/bin/sh
#
# WindowMaker configuration and compilation script.
#
# Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
#

export LINGUAS;LINGUAS=""
if test "$NLSDIR" = ""; then
export NLSDIR;NLSDIR="/usr/lib/locale"
fi


OPTIONS=""
PREFIX="/usr/local"

perai() {
	echo "Type <Return> to continue"
	read nada
}


perform() {
	if ! $* ; then
		echo
		echo "Window Maker installation failed or aborted by user."
		exit 1
	fi
}

help() {
	echo "Usage: $(basename $0) <switch>"
	echo "Where switches are:"
	echo "    -s   --quiet  make silent build"
	echo "    -h   --help   show this message"
	echo ""
	exit $1
}


OPTIONS=""
MAKE_OPTIONS=""
while [ -n "$1" ]
do
	case "$1" in
		"-s"|"--quiet")	OPTIONS="--quiet"
				MAKE_OPTIONS="-s";;
		"-h"|"--help")	help 0;;
		*)		echo "bad command-line parameter $1"
				help 1;;
	esac
	shift
done

trap "echo Window Maker installation aborted. ; exit 1" 2 3

echo
echo "========================="
echo "Window Maker Installation"
echo "========================="
echo
echo "NOTE: If this script fails, read the INSTALL file and"
echo "install by hand."
echo
echo "Please read the README, INSTALL and FAQ files before e-mailing "
echo "questions. We will IGNORE any questions that are already"
echo "answered in the documentation."
echo
perai
echo
echo "Did you install libPropList? If not, please install it now."
echo
perai
echo "Did you read the INSTALL file?"
echo -n "<y/n> "
read rtfm
if [ "$rtfm" != y ]; then
	echo "Please read the INSTALL file before installing"
	exit
fi
echo "Are you sure?"
echo -n "<y/n> "
read rtfm
if [ "$rtfm" != y ]; then
	echo "Please read the INSTALL file before installing"
	exit
fi
echo "Are you lying?"
echo -n "<y/n> "
read rtfm
if [ "$rtfm" != n ]; then
	echo "What a shame!"
	echo "Please read the INSTALL file before installing"
	exit
fi
echo "Do you *swear* that you really read the INSTALL file?"
echo -n "<y/n> "
read rtfm
if [ "$rtfm" != y ]; then
	echo "Please read the INSTALL file before installing"
	exit
fi
echo "Last chance. You will not get help if you have some problem"
echo "because you didn't read that file. Even if your computer explodes."
echo "Do you *really* read it?"
echo -n "<y/n> "
read rtfm
if [ "$rtfm" != y ]; then
	echo "Please read the INSTALL file before installing"
	exit
fi
echo
echo "Ok, you have been warned."
echo
perai


if test "$USER" != "root"; then
	echo
	echo "Warning: you must run this script as root to install"
	echo "Window Maker. Hit <Control>-<C> to quit this script."
	perai
	echo
fi


echo
echo "Option Selection"
echo "================"

######################## KDE
echo
echo "Do you want KDE support?"
echo -n "<y/n> [n] "
read kde
if [ "$kde" = y -o "$kde" = Y ]; then
	OPTIONS="$OPTIONS --enable-kde"
fi

######################## GNOME
echo
echo "Do you want GNOME support?"
echo -n "<y/n> [n] "
read gnome
if [ "$gnome" = y -o "$gnome" = Y ]; then
	OPTIONS="$OPTIONS --enable-gnome"
fi

######################## OLWM
echo
echo "Do you want OPEN LOOK(tm)/olwm support?"
echo -n "<y/n> [n] "
read olwm
if [ "$olwm" = y -o "$olwm" = Y ]; then
	OPTIONS="$OPTIONS --enable-openlook"
fi

######################## NLS
echo
echo "Do you want National Language Support?"
echo -n "<y/n> [n] "
read NLS

if [ "$NLS" = "y" -o "$NLS" = "Y" ]; then
	NLS="Y"
	echo "The supported locales are:"
	ling=` (cd po; /bin/ls *.po) `
	ALL_LINGUAS=""
	for l in $ling; do
		lname=`(cd po; grep Language-Team $l|cut -f2- -d:|cut -f1 -d\\\\|cut -f1 -d\<)`
		lname=`echo $lname`
		lcode=`basename $l .po`
		ALL_LINGUAS="$ALL_LINGUAS $lcode"
		echo "$lcode $lname"
	done
	echo "Type in the locales you want (all will install everything) [none]"
	read foo
	if test "$foo" = "all"; then
		LINGUAS="$ALL_LINGUAS"
	else
		LINGUAS="$foo"
	fi
	echo "Selected locales are: $LINGUAS"

	MB=""
	for i in $LINGUAS; do
		ok=0
		for b in $ALL_LINGUAS; do
			if test "$b" = "$i"; then
				ok=1
				break
			fi
		done
		if test "$ok" = "0"; then
			echo
			echo "$i is not a supported locale"
			perai
			continue
		fi

	done
	echo
	echo "Where do you want to put the message files? [$NLSDIR]"
	echo -n "? "
	read foo
	if test "x$foo" != "x"; then
		NLSDIR=$foo
	fi
fi
##################### Installation path

done=0
while [ $done = 0 ]; do
    echo
    echo "Where do you want to install Window Maker? [$PREFIX]"
    echo "Don't change it if you don't know what you're doing."
    if test "$USER" != "root"; then
	echo "Make sure to specify a path where you have write permission."
    fi
    echo "(The default will put Window Maker in $PREFIX/bin, $PREFIX/lib etc.)"
    echo -n "? "
    read foo
    if test "x$foo" != "x"; then
	if [ "$foo" = "y" -o "$foo" = "n" ]; then
	    echo
	    echo "Hmm... I don't think you really want to install Window Maker into \"$foo\""
	    echo
	else
	    done=1	
	    PREFIX=$foo
	fi
    else
    	done=1
    fi
    echo
    echo "   $PREFIX/bin must be in the PATH environment variable"
    echo "of all users who will use Window Maker"
    perai
done

OPTIONS="$OPTIONS --prefix=$PREFIX"


##################### Install stripped binaries?

INSTALL="install"
echo "Do you want stripped binaries to be installed?"
echo "Installed binaries will be smaller (with debug info removed)."
echo -n "<y/n> [n] "
read foo
if [ "$foo" = y -o "$foo" = Y ]; then
	INSTALL="install-strip"
fi


#####################  Configure
echo "--------------------------"
echo "Configuring Window Maker..."
echo "--------------------------"
if [ `uname -s` = "SCO_SV" ]; then
	echo "CFLAGS=\"$CFLAGS -belf -DANSICPP\" ./configure $OPTIONS"

	CFLAGS="$CFLAGS -belf -DANSICPP" 

	perform ./configure $OPTIONS
else
	echo "CFLAGS=\"$CFLAGS $GCCFLAGS\" ./configure $OPTIONS"

	CFLAGS="$CFLAGS $GCCFLAGS" 

	perform ./configure $OPTIONS
fi


#################### Compile
echo "-------------------------"
echo "Compiling Window Maker..."
echo "-------------------------"
(cd src; perform make $MAKE_OPTIONS clean)
perform make $MAKE_OPTIONS

echo "--------------------------"
echo "Installing Window Maker..."
echo "--------------------------"

perform make $MAKE_OPTIONS $INSTALL

if [ `uname -s` = "Linux" ]; then
    echo
    echo "   Make sure $PREFIX/lib is in the /etc/ld.so.conf file"
	/sbin/ldconfig
fi

echo 
echo "Installation Finished!"
echo
echo "Now, each user that wishes to use WindowMaker must run the wmaker.inst"
echo "script that was just installed."


if test "$NLS" = "Y"; then
	echo "Don't forget to set the LANG environment variable to your locale"
fi

