#!/bin/sh 
#
# Copyright (c) 1999,2000,2001 WU-FTPD Development Group.  
# All rights reserved.
#  
# Portions Copyright (c) 1980, 1985, 1988, 1989, 1990, 1991, 1993, 1994  
#   The Regents of the University of California. 
# Portions Copyright (c) 1993, 1994 Washington University in Saint Louis.  
# Portions Copyright (c) 1996, 1998 Berkeley Software Design, Inc.  
# Portions Copyright (c) 1989 Massachusetts Institute of Technology.  
# Portions Copyright (c) 1998 Sendmail, Inc.  
# Portions Copyright (c) 1983, 1995, 1996, 1997 Eric P.  Allman.  
# Portions Copyright (c) 1997 by Stan Barber.  
# Portions Copyright (c) 1997 by Kent Landfield.  
# Portions Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997  
#   Free Software Foundation, Inc.    
#  
# Use and distribution of this software and its source code are governed   
# by the terms and conditions of the WU-FTPD Software License ("LICENSE").  
#  
# If you did not receive a copy of the license, it may be obtained online  
# at http://www.wu-ftpd.org/license.html.  
#
# $Id: build,v 1.25.2.1 2001/11/29 17:39:29 wuftpd Exp $
#

echo
echo 'IMPORTANT NOTICE'
echo ''
echo 'Beginning with version 2.6.0, the WU-FTPD Development Group is moving the'
echo 'build process to use GNU Autoconf.  Until this move is completed, both'
echo 'build systems will remain supported.  You are encouraged to use the new'
echo '"configure" command to select options, then "make" to compile the daemon.'
echo 'For details on this, read the README.AUTOCONF file.'
echo 'If problems occur, please report them to the development group via email to'
echo 'wuftpd-members@wu-ftpd.org  We are soliciting feedback on the new'
echo 'configuration process, and your help is greatly appreciated.'
echo
echo 'If you choose to continue to use the original build process, bear in mind'
echo 'it will be phased out over the next few releases: when we are reasonably'
echo 'sure the new configuration system works for most target platforms.'
echo
echo 'Sleeping for five seconds, sorry for the interruption.'
sleep 5

RELEASE=wu-ftpd-2.6.2
cat > .bld.hlp <<EOF
Usage: build <make-options> <target-platform>

<target-platform> may be one of the following:
  gen    : generic make (copy this when porting to a new system)
  aix    : IBM AIX
  aux    : AU/X
  bdi    : BSD/OS
  bsd    : BSD
  dec    : DEC Unix 3.X
  du4    : DEC Unix 4.X or later
  dyn    : Dynix
  fbs    : FreeBSD 2.0 or later
  him    : Hitachi SR2201 and S-3600 Unix (HI-UX/MPP and HI-OSF/1-MJ)
  hiu    : Hitachi 3050 Unix (HI-UX/WE2)
  hpx    : HP-UX
  lnx    : Linux (tested on 1.2.X and 2.0.0)
  nbs    : NetBSD 1.X
  nx2    : NeXTstep 2.x
  nx3    : NeXTstep 3.x
  osf    : OSF/1
  osx    : Mac OS X
  ptx    : ???
  sco    : SCO Unix 3.2v4.2 / SCO OpenServer 5
  sgi    : SGI Irix 5.3
  ir4    : SGI Irix 4.0.5a
  sny    : Sony NewsOS
  sol    : SunOS 5.x / Solaris 2.x 
  s41    : SunOS 4.1.x 
  ult    : Ultrix 4.x
  uxw    : UnixWare 1.1 or later
  clean  : Clean up object files and such to reduce disk space after building.
  install: Install ftpd.
EOF

maketarget="no-target"
makeopts=""
makeargs=""

args=$#
while [ $args -gt 0 ]
do
  case $1 in
 
    help) cat .bld.hlp
          exit ;;

    -*) makeargs="$makeargs $1" ;;

    install|clean|distrib|???)
         if [ $maketarget != no-target ]
         then
             echo "Can only make one target system at a time"
             echo 'Both "'"$maketarget"'" and "'"$1"'" where given'
             exit
         else
             maketarget=$1
         fi
       ;;

    *) makeopts="$makeopts $1" ;;

  esac
  
  shift
  
  args=`expr $args - 1`

done

rm -f config.h
ln -s config.h.noac config.h
rm -f src/pathnames.h
ln -s pathnames.h.noac src/pathnames.h

echo 'make args are : '$makeargs''
echo 'make opts are : '$makeopts''
case $maketarget in

   ???) 
        echo ''
        echo "Copying Makefiles."
	rm -f Makefile
	if [ -f makefiles/common ]; then
		cat makefiles/Makefile.$maketarget makefiles/common > Makefile
	else
		cp makefiles/Makefile.$maketarget Makefile
	fi
	cd src
	rm -f Makefile
	if [ -f makefiles/common ]; then
		cat makefiles/Makefile.$maketarget makefiles/common > Makefile
	else
		cp makefiles/Makefile.$maketarget Makefile
	fi
	cd ../support
	rm -f Makefile
	if [ -f makefiles/common ]; then
		cat makefiles/Makefile.$maketarget makefiles/common > Makefile
	else
		cp  makefiles/Makefile.$maketarget Makefile
	fi
        cd ..

        echo ''
	echo "Linking src/config.h"
	cd ./src
	rm -f config.h
	ln -s config/config.$maketarget config.h
        echo ''
        echo "Making support library."
        cd ../support
        make $makeargs $makeopts libsupport.a
        echo ''
        echo "Making ftpd."
        cd ../src
        make $makeargs $makeopts  ftpd
        echo ''
        echo "Making ftpcount."
        make $makeargs $makeopts  ftpcount
        echo ''
        echo "Making ftpshut".
        make $makeargs $makeopts  ftpshut
        echo ''
        echo "Making ftprestart".
        make $makeargs $makeopts  ftprestart
        echo ''
        echo "Making ckconfig."
        make $makeargs $makeopts  ckconfig
        echo ''
        echo "Making privatepw."
        make $makeargs $makeopts  privatepw
        cd ..

        if [ ! -d bin ] ;  then    mkdir bin;        fi
        cd bin
        rm -f ftpd ftpcount ftpshut ftprestart ftpwho ckconfig privatepw
        if [ -s ../src/ftpd     ] ; then cp ../src/ftpd     ftpd     ; fi
        if [ -s ../src/ftpcount ] ; then
		cp ../src/ftpcount ftpcount
		ln -s ftpcount ftpwho
	fi
        if [ -s ../src/ftpshut  ] ; then cp ../src/ftpshut  ftpshut  ; fi
        if [ -s ../src/ftprestart  ] ; then cp ../src/ftprestart  ftprestart ; fi
	if [ -s ../src/ckconfig ] ; then cp ../src/ckconfig ckconfig ; fi
	if [ -s ../src/privatepw ] ; then cp ../src/privatepw privatepw ; fi
        cd ..
        echo ''
        echo "Executables are in bin directory:"
        size bin/ftpd bin/ftpcount bin/ftpshut bin/ftprestart bin/ftpwho bin/ckconfig bin/privatepw
        echo "Done"
        ;;


    clean) # This only sort of works 
        echo "Cleaning root directory."
        if [ -s .depend ] ;         then rm .depend  ; fi
        if [ -s .bld.hlp ] ;        then rm .bld.hlp ; fi
        if [ -s wu-ftpd.tar ] ;     then rm wu-ftpd.tar ; fi
	rm -f Makefile
	rm -f config.h
	rm -f *~ makefiles/*~ util/*~
        echo "Cleaning support directory."
        cd support
        cat makefiles/Makefile.gen makefiles/common > Makefile
        make $makeargs $makeopts clean
	rm -f Makefile
        echo "Cleaning src directory."
        cd ../src
        cat makefiles/Makefile.gen makefiles/common > Makefile
        make $makeargs $makeopts clean
	rm -f Makefile
	rm -f Makefile.bak
	rm -f config.h
	rm -f edit
	rm -f pathnames.h
	rm -f y.tab.h
	rm -f vers.c
        echo "Cleaning util/privatepw directory."
        echo "Cleaning bin directory."
        cd ..
        if [ -d bin ] ;  then    rm -rf bin;        fi
        ;;

    install)
        make -f Makefile $makeargs $makeopts install
        ;;

    distrib)
        ./build clean
        echo "Building file list."
# assumes the gnu tar program
	ls RCS/* */RCS/* */*/RCS/* > /tmp/xclude$$
	echo $RELEASE >> /tmp/xclude$$
	echo .bld.hlp >> /tmp/xclude$$
	echo RCS >> /tmp/xclude$$
	echo src/RCS >> /tmp/xclude$$
	echo support/RCS >> /tmp/xclude$$
	echo support/arpa/RCS >> /tmp/xclude$$
	echo support/man/RCS >> /tmp/xclude$$
	echo makefiles/RCS >> /tmp/xclude$$
	echo util/RCS >> /tmp/xclude$$
	echo doc/RCS >> /tmp/xclude$$
	echo doc/examples/RCS >> /tmp/xclude$$
        echo "Building distribution tar file."
	mkdir $RELEASE
	tar -X/tmp/xclude$$  -cf - . |(cd $RELEASE; tar xBf - )
	tar -cf $RELEASE.tar $RELEASE
        echo "Cleaning up."
	rm -rf /tmp/xclude$$ $RELEASE
        ;;

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

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