#! /bin/sh
# Configuration for lpsms.
# Neil Moore <neil@cs.uky.edu>
# 18 May 2000
#
# Part of lpsms.  See COPYRIGHT for copyright information.

# XXX: search for libpvm, see if we need libnsl and libsocket
# XXX: port the whole thing to autoconf

#LPSMSVER=0.6.0-beta3
LPSMSVER=0.5.3
LPARSEVER=0.99.54

if [ -f MCONF ]; then
    mv MCONF MCONF.bak
fi

cat <<EOF

Welcome to lpsms configuration.  Below you will be asked a series of
questions about where you want to install lpsms.  Each question has
a default response (indicated by square brackets); if you wish to use
the default, you need only hit <ENTER>.

All directories specified will be created during installation if they
do not already exist.


EOF

cat <<EOF
You must now choose an installation location for lpsms.  No files will
be installed directly under this directory; rather, the default
locations for files will be subdirectories of this directory.

If you are planning on installing lpsms for system-wide use (and you
have the appropriate permissions), you should probably answer
/usr/local, /usr/local/lpsms, or /opt/lpsms.

EOF

printf "Where do you wish to install? [~/lpsms]  "
read INSTALL_PATH
if [ -z "$INSTALL_PATH" ]; then
    INSTALL_PATH=$HOME/lpsms
fi
# Expand globs and ~
INSTALL_PATH=`csh -c "echo $INSTALL_PATH" 2>/dev/null`



cat <<EOF

Now choose an installation location for the lpsms binaries.  These
include lparse, stable, sderes, lderes, printsm, smdtconv, stratify,
and simplify, as well as the scripts make-stable and sm2dt.

After installing, you probably want to add this directory to your
PATH, if it is not already there.

EOF

printf "Where do you wish to install binaries? [$INSTALL_PATH/bin]  "
read BIN_PATH
if [ -z "$BIN_PATH" ]; then
    BIN_PATH=$INSTALL_PATH/bin
fi
BIN_PATH=`csh -c "echo $BIN_PATH" 2>/dev/null`

cat <<EOF

Now choose an installation location for the lpsms libraries.  Currently,
the only such library is liblp.a.

EOF


printf "Where do you wish to install libraries? [$INSTALL_PATH/lib]  "
read LIB_PATH
if [ -z "$LIB_PATH" ]; then
    LIB_PATH=$INSTALL_PATH/lib
fi
LIB_PATH=`csh -c "echo $LIB_PATH" 2>/dev/null`

cat <<EOF

Now choose an installation location for the lpsms header files.
Currently, these consist of the various liblp headers.

EOF

printf "Where do you wish to install headers? [$INSTALL_PATH/include]  "
read HDR_PATH
if [ -z "$HDR_PATH" ]; then
    HDR_PATH=$INSTALL_PATH/include
fi
HDR_PATH=`csh -c "echo $HDR_PATH" 2>/dev/null`

cat <<EOF

Now choose the manual section in which to install man pages.  Normally,
this will be section 1 (\`Commands'), but some sites may have special
sections for non-vendor-installed software.

EOF

printf "In which manual section do you wish to install man pages? [1]  "
read MAN_SECTION
if [ -z "$MAN_SECTION" ]; then
    MAN_SECTION=1
fi

cat <<EOF

Now choose an installation location for the lpsms manual pages (including
pages for lderes, sderes, sm2dt, smdtconv, simplify, stratify, make-stable,
and stable.

After installing, you probably want to add this directory to your
MANPATH, if it is not already there.

EOF

printf "Where do you wish to install man pages? [$INSTALL_PATH/man/man$MAN_SECTION]  "
read MAN_PATH
if [ -z "$MAN_PATH" ]; then
    MAN_PATH=$INSTALL_PATH/man/man$MAN_SECTION
fi
MAN_PATH=`csh -c "echo $MAN_PATH" 2>/dev/null`

cat <<EOF

Now choose an installation location for the lparse info page.  This
documentation is intended for use with GNU info or GNU Emacs, but is
for the most part human-readable.

EOF

printf "Where do you wish to install info pages? [$INSTALL_PATH/info]  "
read INFO_PATH
if [ -z "$INFO_PATH" ]; then
    INFO_PATH=$INSTALL_PATH/info
fi
INFO_PATH=`csh -c "echo $INFO_PATH" 2>/dev/null`

cat <<EOF


Now choose permissions for the various lpsms components.  The defaults
should suffice in nearly all cases.

EOF


printf "Install binaries with what permissions? [0755]  "
read BIN_MODE
if [ -z "$BIN_MODE" ]; then
    BIN_MODE=0755
fi

printf "Install static libraries with what permissions? [0644]  "
read LIB_MODE
if [ -z "$LIB_MODE" ]; then
    LIB_MODE=0644
fi

printf "Install header files with what permissions? [0644]  "
read HDR_MODE
if [ -z "$HDR_MODE" ]; then
    HDR_MODE=0644
fi

printf "Install manual pages with what permissions? [0644]  "
read MAN_MODE
if [ -z "$MAN_MODE" ]; then
    MAN_MODE=0644
fi

cat <<EOF

If you would like for the lpsms components, once installed, to be owned by
some user and group other than the one that runs \`make install', answer \`Y'
here.  Note that you normally must be root to do this.

EOF

printf "Would you like to change the ownership of files? [N]  "
read DO_CHOWN
if [ -n "$DO_CHOWN" ]; then
    if [ "`echo $DO_CHOWN | tr a-z A-Z | sed -e 's/^\(.\).*/\1/'`" = "N" ]; then
	DO_CHOWN=""
    else
	DO_CHOWN=Y
    fi
fi

if [ -n "$DO_CHOWN" ]; then
    cat <<EOF
    
Specify here the users and groups that should own the various components
of lpsms.  Both symbolic user/group names and numerical user/group IDs
are allowed.  Note that these users and groups must already exist.

EOF

    printf "What user should own binaries? [root]  "
    read BIN_OWNER
    if [ -z "$BIN_OWNER" ]; then
	BIN_OWNER=root
    fi
    printf "What group should own binaries? [bin]  "
    read BIN_GROUP
    if [ -z "$BIN_GROUP" ]; then
	BIN_GROUP=bin
    fi
    printf "What user should own libraries? [root]  "
    read LIB_OWNER
    if [ -z "$LIB_OWNER" ]; then
	LIB_OWNER=root
    fi
    printf "What group should own libraries? [root]  "
    read LIB_GROUP
    if [ -z "$LIB_GROUP" ]; then
	LIB_GROUP=root
    fi
    printf "What user should own headers? [root]  "
    read HDR_OWNER
    if [ -z "$HDR_OWNER" ]; then
	HDR_OWNER=root
    fi
    printf "What group should own headers? [root]  "
    read HDR_GROUP
    if [ -z "$LIB_GROUP" ]; then
	HDR_GROUP=root
    fi
    printf "What user should own man pages? [root]  "
    read MAN_OWNER
    if [ -z "$MAN_OWNER" ]; then
	MAN_OWNER=root
    fi
    printf "What group should own man pages? [man]  "
    read MAN_GROUP
    if [ -z "$MAN_GROUP" ]; then
	MAN_GROUP=man
    fi
else
    BIN_OWNER=root
    BIN_GROUP=bin
    LIB_OWNER=root
    LIB_GROUP=root
    HDR_OWNER=root
    HDR_GROUP=root
    MAN_OWNER=root
    MAN_GROUP=man
fi


sed -e 's/^X //' <<EOF >conftest.c
X #include <stdio.h>
X 
X int main(void) { printf("test\n"); return 0; }
X 
EOF

printf "Searching for a working compiler... "
if gcc -o conftest conftest.c >/dev/null 2>&1; then
	echo "found gcc"
	CC=gcc
	CFLAGS='-O3 -Wall -ansi -pedantic -DLPSMSVER=\"$(LPSMSVER)\" $(INCLUDES)'
elif cc -o conftest conftest.c >/dev/null 2>&1; then
	echo "found cc"
	CC=cc
	CFLAGS='-O -Wall -DLPSMSVER=\"$(LPSMSVER)\" $(INCLUDES)'
else
	echo "not found"
	exit 1;
fi

rm conftest conftest.c 2>/dev/null

sed -e 's/^X //' <<EOF >conftest.man
X .TH stable 1 "15 Nov 1999" "lpsms 0.2"
X .SH NAME
X stable - compute stable models of logic programs
X 
EOF

printf "Searching for troff... "
if groff -man -Tps conftest.man > conftest.ps 2>/dev/null; then
	echo "found groff"
	TROFF='groff -man -Tps'
elif troff -man -Tps conftest.man > conftest.ps 2>/dev/null; then
	echo "found troff"
	TROFF='troff -man -Tps'
else
	echo "not found - make doc will fail"
	TROFF='false'
fi
rm conftest.man conftest.ps

printf "Checking for __inline__... "

sed -e 's/^X //' <<EOF >conftest.c
X #include <stdio.h>
X __inline__ void test(void);
X 
X int main(void) { test(); printf("st\n"); }
X 
X __inline__ void test(void) { printf("te"); return 0; }
X 
EOF

if $CC -o conftest conftest.c >/dev/null 2>&1; then
	echo "yes"
	INLINE=__inline__
else
	echo "no"
	INLINE=
fi

CFLAGS="-Dinline=\$(INLINE) $CFLAGS"

rm conftest conftest.c 2>/dev/null

printf "Searching for perl 5... "
PERLOUT=`perl -e 'use 5; print "OK\n";'` 2>/dev/null
if [ "$PERLOUT" = OK ]; then
	echo "ok"
else
	echo "no"
	exit 1
fi

IFS="${IFS=         }"; save_IFS="$IFS"; IFS="${IFS}:"
PERL=/usr/bin/perl
for dir in $PATH; do
	if [ -x $dir/perl ]; then
		PERL=$dir/perl
		break
	fi
done
IFS=$save_IFS

sed -e 's/^X //' <<EOF >MCONF
X # Generated automatically -- do not edit
X #
X 
X .SUFFIXES: .ps .man
X 
X LPSMSVER = $LPSMSVER
X LPARSEVER = $LPARSEVER
X 
X INCLUDES = -I. -I`pwd`/liblp
X LIBS = -L`pwd`/liblp -llp
X 
X INLINE = $INLINE
X 
X CFLAGS = $CFLAGS
X CC = $CC
X TROFF = $TROFF
X 
X .man.ps:
X 	\$(TROFF) \$< > \$@
X 
X LDFLAGS = \$(LIBS)
X 
X PERL = $PERL -w
X 
X INSTALL_PATH = $INSTALL_PATH
X DO_CHOWN = $DO_CHOWN
X 
X BIN_PATH = $BIN_PATH
X BIN_OWNER = $BIN_OWNER
X BIN_GROUP = $BIN_GROUP
X BIN_MODE = $BIN_MODE
X 
X LIB_PATH = $LIB_PATH
X LIB_OWNER = $LIB_OWNER
X LIB_GROUP = $LIB_GROUP
X LIB_MODE = $LIB_MODE
X 
X HDR_PATH = $HDR_PATH
X HDR_OWNER = $HDR_OWNER
X HDR_GROUP = $HDR_GROUP
X HDR_MODE = $HDR_MODE
X 
X MAN_PATH = $MAN_PATH
X MAN_OWNER = $MAN_OWNER
X MAN_GROUP = $MAN_GROUP
X MAN_MODE = $MAN_MODE
X MAN_SECTION = $MAN_SECTION
X 
EOF

echo "Configuring lparse"
(cd lparse-$LPARSEVER/ && ./configure --prefix=$INSTALL_PATH --bindir=$BIN_PATH --libdir=$LIB_PATH \
--includedir=$HDR_PATH --mandir=$MAN_PATH --infodir=$INFO_PATH;)
