#!/bin/sh
#
# WindowMaker configuration and compilation script.
#
# Copyright (c) 1997, 1998 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
}


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 INSTALL and FAQ files before e-mailing "
echo "questions. (we are lazy to reply questions already answered "
echo "there as much as you are lazy to read these ;) "
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"
	echo "Do you want to disable features that become duplicated with"
	echo "KDE support (like root menus)?"
	echo -n "<y/n> [n] "
	read lite
	if [ "$lite" = y -o "lite" = Y ]; then
		OPTIONS="$OPTIONS --enable-lite"
	fi
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

######################## 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 -f 2 -d: |cut -f 2 -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

		if [ "$MB" = "" -a "$i" = "ja" -o "$i" = "kr" ]; then
			echo
			echo "A language you selected needs multi-byte character support"
			echo "Do you want to enable it?"
			echo -n "<y/n> [n] "
			read MB
			if [ "$MB" = "y" -o "$MB" = "Y" ]; then
				OPTIONS="$OPTIONS --enable-kanji"
			fi
		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 of all users who use Window Maker"
    perai
done

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



#####################  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 clean)
perform make

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

perform make install

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

