#!/bin/sh
#
# $Id: build,v 4.52 1999/11/15 22:16:13 hubert Exp $
#
#            T H E    P I N E    M A I L   S Y S T E M
#
#   Laurence Lundblade and Mike Seibel
#   Networks and Distributed Computing
#   Computing and Communications
#   University of Washington
#   Administration Building, AG-44
#   Seattle, Washington, 98195, USA
#   Internet: lgl@CAC.Washington.EDU
#             mikes@CAC.Washington.EDU
#
#   Please address all bugs and comments to "pine-bugs@cac.washington.edu"
#
#
#   Pine and Pico are registered trademarks of the University of Washington.
#   No commercial use of these trademarks may be made without prior written
#   permission of the University of Washington.
#
#   Pine, Pico, and Pilot software and its included text are Copyright
#   1989-1999 by the University of Washington.
#
#   The full text of our legal notices is contained in the file called
#   CPYRIGHT, included with this distribution.
#
#
#   Pine is in part based on The Elm Mail System:
#    ***********************************************************************
#    *  The Elm Mail System  -  Revision: 2.13                             *
#    *                                                                     *
#    * 			Copyright (c) 1986, 1987 Dave Taylor               *
#    * 			Copyright (c) 1988, 1989 USENET Community Trust    *
#    ***********************************************************************
# 
#


#
#  General build script for Pine
#

cat > .bld.hlp <<EOF
Usage: build <make-options> <target-platform>

<target-platform> may be one of the ports listed in doc/pine-ports or
  clean   Clean up object files and such.
          Also, a good way to rebuild Pine/Pico from scratch.

<make-options> are generally not needed. They are passed to make.
"-n" is probably the most useful, as it tells make to just print out
what it is going to do and not actually do it.

"Build" by itself with no arguments is the same as typing in the previous
build command with the arguments used then.

To build Pine and Pico the command "build xxx" should work where xxx
is one of the targets. For example "build ult" to build Pine for Ultrix.

If you are attempting to compile in the LDAP functionality in pine, you
should get the Univ. of Michigan ldap-3.3 source and put it in a directory
called "ldap" in this top-level pine directory. Or you could put a symlink
pointing from here to the actual location. The include files should show
up in ldap/include and the libraries in ldap/libraries.
Alternatively, you can try using the Netscape Directory SDK 1.0 (or probably
3.0 when it is ready) by putting the include files in ldap/include and
libldap.a in ldap/libraries. This build script will attempt to notice that
you are not using ldap-3.3 and leave out liblber.a, which is required when
using ldap-3.3.

If you are attempting to compile in Kerberos5 functionality, put a symlink
called krb5 pointing from here to the location of the includes and libraries.
The expected files should be in krb5/include and krb5/lib.


The executables built by this are:

 pine   The Pine mailer. Once compiled this should work just fine on
        your system with no other files than this binary, and no
        modifications to your system. Optionally you may create two
        configuration files, /usr/local/lib/pine.conf and 
        /usr/local/lib/pine.info. See the documentation for details.
 
 pico   The standalone editor similar to the Pine message composer.
        This is a very simple straight forward text editor.
 
 pilot  The standalone file system navigator.
 
 imapd  The IMAP daemon. If you want to run Pine in client/server mode,
        this is the daemon to run on the server. Installing this
        requires system privileges and modifications to /etc/services.
        See doc/tech-notes for more details.
 
 mtest  The test IMAP client, an absolutely minimal mail client, useful
        for debugging.

In general you should be able to just copy the Pine and Pico binaries
to the place you keep your other local binaries. /usr/local/bin is a
likely place.
  
EOF
                 

# If no args, build what was built last time.
if [ $# -eq 0 ]
then
    if [ -f .bld.args ]
    then
	set `cat .bld.args`
    fi
else
    rm -f .bld.args
fi

maketarg="no-target"
PHOME=`pwd`

for var in "$@"; do
  case "$var" in
 
    help) cat .bld.hlp
          exit ;;

    clean|???)
         if [ $maketarg != no-target ]
         then
             echo "Can only make one target system at a time"
             echo Both \""$maketarg"\" and \""$var"\" were given
             exit
         else
             maketarg="$var"
         fi
       ;;

    *) makeargs="$makeargs '$var'"
       case "$var" in
	 CC=*)                   NOCC=1    ;;
	 LDAPLIBS=*)             LLIBS=1   ;;
	 LDAPCFLAGS=*)           LFLAGS=1  ;;
	 GSSDIR=*)               GDIR=1    ;;
	 EXTRAAUTHENTICATORS=*)  AAFLAGS=1 ;;
       esac
       ;;

  esac
done

if [ $# -ne 0 ]
then
    case $maketarg in

       ???) 
	    echo "$makeargs" $maketarg > .bld.args ;;
    esac
fi

# Set up correct compiler
if [ "$NOCC" != "1" ]
then
  case $maketarg in
     sol)
 echo "\"Sol\" port no longer exists. Use \"so4\" for Solaris version 2.4 or"
 echo "lower (SunOS 5.4 or lower) and \"so5\" for Solaris version 2.5 or"
 echo "higher (SunOS 5.5 or higher)."
	  exit;;
     gso)
 echo "\"Gso\" port no longer exists. Use \"gs4\" for Solaris version 2.4 or"
 echo "lower (SunOS 5.4 or lower) and \"gs5\" for Solaris version 2.5 or"
 echo "higher (SunOS 5.5 or higher)."
	  exit;;
     gas) makeargs="CC=gcc $makeargs" ;;
     gh9) makeargs="CC=gcc $makeargs" ;;
     ghp) makeargs="CC=gcc $makeargs" ;;
     gs4) makeargs="CC=gcc $makeargs" ;;
     gs5) makeargs="CC=gcc $makeargs" ;;
     gsu) makeargs="CC=gcc $makeargs" ;;
     gul) makeargs="CC=gcc $makeargs" ;;
     mnt) makeargs="CC=gcc $makeargs" ;;
     bs3) makeargs="CC=shlicc $makeargs" ;;
     lyn) [ -f /CYGNUS.bash ] && . /CYGNUS.bash; makeargs="CC=gcc $makeargs" ;;
       *) makeargs="CC=cc $makeargs" ;;
  esac
fi

# Some ports are common for pico (for example) but different for pine.
# For example, port hxd uses hpp port for pico, hpx port for pine, and a
# separate hxd port for c-client. Not all of these have been verified to work.
case $maketarg in
   aos) ccltarg=$maketarg; picotarg=bsd;       pinetarg=$picotarg ;;
   bs2) ccltarg=bsi;       picotarg=$maketarg; pinetarg=$maketarg ;;
   bs3) ccltarg=$maketarg; picotarg=bs2;       pinetarg=bs2 ;;
   d41) ccltarg=$maketarg; picotarg=d-g;       pinetarg=$picotarg ;;
   d54) ccltarg=$maketarg; picotarg=d-g;       pinetarg=$picotarg ;;
   ghp) ccltarg=$maketarg; picotarg=gh9;       pinetarg=$maketarg ;;
   gs4) ccltarg=gso;       picotarg=gso;       pinetarg=$maketarg ;;
   gs5) ccltarg=gso;       picotarg=gso;       pinetarg=$maketarg ;;
   hpx) ccltarg=$maketarg; picotarg=hpp;       pinetarg=$maketarg ;;
   hxd) ccltarg=$maketarg; picotarg=hpp;       pinetarg=hpx ;;
   lnp) ccltarg=$maketarg; picotarg=lnx;       pinetarg=$picotarg ;;
   nx3) ccltarg=$maketarg; picotarg=nxt;       pinetarg=$picotarg ;;
   os4) ccltarg=$maketarg; picotarg=osf;       pinetarg=$picotarg ;;
   pt1) ccltarg=psx;       picotarg=$maketarg; pinetarg=$maketarg ;;
   shp) ccltarg=$maketarg; picotarg=hpp;       pinetarg=$picotarg ;;
   sg6) ccltarg=$maketarg; picotarg=sgi;       pinetarg=$picotarg ;;
   slx) ccltarg=$maketarg; picotarg=lnx;       pinetarg=$picotarg ;;
   sl4) ccltarg=$maketarg; picotarg=lnx;       pinetarg=$picotarg ;;
   sl5) ccltarg=$maketarg; picotarg=lnx;       pinetarg=$picotarg ;;
   so4) ccltarg=sol;       picotarg=sol;       pinetarg=$maketarg ;;
   so5) ccltarg=sol;       picotarg=sol;       pinetarg=$maketarg ;;
   sos) ccltarg=$maketarg; picotarg=osf;       pinetarg=$picotarg ;;
   ssn) ccltarg=$maketarg; picotarg=sun;       pinetarg=$picotarg ;;
     *) ccltarg=$maketarg; picotarg=$maketarg; pinetarg=$maketarg ;;
esac

echo "make args are $makeargs"

case $maketarg in

   ???) 
        echo ''
        cd $PHOME

# Try to automatically include LDAP definitions.
        if [ -d ldap -a -f contrib/ldap-setup -a \
	     \( "$LLIBS" != "1" -o "$LFLAGS" != "1" \) ]
	then
	  contrib/ldap-setup $pinetarg $maketarg
# Allow for override of either of these separately.
	  case "$?" in
	    1) if [ "$LLIBS" != "1" ]
	       then
		 L1="'LDAPLIBS=../ldap/libraries/libldap.a ../ldap/libraries/liblber.a'"
	       fi
	       if [ "$LFLAGS" != "1" ]
	       then
		 L2="'LDAPCFLAGS=-DENABLE_LDAP -I../ldap/include'"
	       fi
	       echo "Including LDAP functionality"
	       ;;

	    2) if [ "$LLIBS" != "1" ]
	       then
		 L1="'LDAPLIBS=../ldap/libraries/libldap.a'"
	       fi
	       if [ "$LFLAGS" != "1" ]
	       then
		 L2="'LDAPCFLAGS=-DENABLE_LDAP -I../ldap/include'"
	       fi
	       echo "Including LDAP functionality"
	       ;;

	    *) exit 30 ;;
	  esac
	fi

# Try to automatically include Kerberos5 definitions.
        if [ -d krb5 -a -f contrib/krb5-setup -a \
             \( "$GDIR" != "1" -o "$AAFLAGS" != "1" \) ]
	then
	  contrib/krb5-setup $pinetarg $maketarg
# Allow for override of either of these separately.
	  case "$?" in
	    0) if [ "$GDIR" != "1" ]
	       then
	         K1="'GSSDIR=$PHOME/krb5'"
	       fi
	       if [ "$AAFLAGS" != "1" ]
	       then
	         K2="'EXTRAAUTHENTICATORS=gss'"
	       fi
	       echo "Including Kerberos5 functionality"
	       ;;

	    *) exit 30 ;;
	  esac
	fi

        if [ -s c-client ] ; then rm -f c-client ; fi
        ln -s imap/c-client c-client
        if [ -s mtest    ] ; then rm -f mtest    ; fi
        ln -s imap/mtest mtest
        if [ -s imapd    ] ; then rm -f imapd    ; fi
        ln -s imap/imapd imapd
        echo "Making c-client library, mtest and imapd"
        eval echo make "$makeargs" "$K1" "$K2" $ccltarg
        cd $PHOME/imap
        eval make "$makeargs" "$K1" "$K2" $ccltarg
        echo ''

        echo "Making Pico and Pilot"
        cd $PHOME/pico
        eval echo make "$makeargs" -f makefile.$picotarg
        eval make "$makeargs" -f makefile.$picotarg
        echo ''

        echo "Making Pine".
        cd $PHOME/pine
        eval echo make "$makeargs" "$L1" "$L2" -f makefile.$pinetarg
        eval make "$makeargs" "$L1" "$L2" -f makefile.$pinetarg
        cd $PHOME
        if [ ! -d bin ] ;  then    mkdir bin;        fi
        cd $PHOME/bin
        rm -f pine mtest imapd pico pilot
        if [ -s ../pine/pine ] ;      then ln ../pine/pine  pine      ; fi
        if [ -s ../mtest/mtest ] ;    then ln ../mtest/mtest mtest ; fi
        if [ -s ../imapd/imapd ] ;    then ln ../imapd/imapd imapd    ; fi
        if [ -s ../pico/pico ] ;      then ln ../pico/pico pico ; fi
        if [ -s ../pico/pilot ] ;     then ln ../pico/pilot pilot ; fi
        cd $PHOME
        echo ''
        echo "Links to executables are in bin directory:"
        size bin/pine bin/mtest bin/imapd bin/pico bin/pilot
	case $maketarg in
	    mnt)    echo "Fixing stacksizes ..."
		    fixstk 64K bin/pine bin/pico bin/imapd bin/mtest bin/pilot
	esac
        echo "Done"
        ;;


    clean) # This only sort of works 
        echo "Cleaning c-client and imapd"
	cd $PHOME/imap
	make clean
        echo "Cleaning Pine"
        cd $PHOME/pine
        make -f makefile.ult clean
        echo "Cleaning pico"
        cd $PHOME/pico
        make $makeargs -f makefile.ult clean
        echo "Done"
        cd $PHOME
        ;;

    no-target)
        echo "No target plaform for which to build Pine given."
        echo 'Give command "build help" for help.'
        ;;

    *)  echo 'Do not know how to make Pine for target "'$maketarg'".'
        ;;
esac
