#!/bin/sh 

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)
  hpx    : HP-UX
  nx3    : NeXTstep 3.0
  osf    : OSF/1
  sgi    : SGI Irix 4.0.5a
  sol    : SunOS 5.x / Solaris 2.x
  sny    : Sony NewsOS
  s41    : SunOS 4.1.x (requires acc, gcc, or suitable ansi compiler)
  ult    : Ultrix 4.x
  lin	 : Linux
  clean  : Clean up object files and such to reduce disk space after building.
  install: Install ftpd.
EOF

maketarget="no-target"
makeargs=""

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

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

    install|clean|???)
         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
       ;;

      
    *)  echo 'Invalid argument, "'$1'", given to build.'
        echo 'Give command "build help" for help.'
        exit
      ;;

  esac
  
  shift
  
  args=`expr $args - 1`

done

echo 'make args are "'$makeargs'"'

case $maketarget in

   ???) 
        echo ''
        echo "Linking Makefiles."
		cd src
		ln -s makefiles/Makefile.$maketarget Makefile
		ln -s config/config.$maketarget config.h
		cd ../support
		ln -s makefiles/Makefile.$maketarget Makefile
#       echo ''
#       echo "Make Depend in src directory."
#       cd ../src
#       make $makeargs depend
#       echo ''
        echo "Making support library."
        cd ../support
        make $makeargs
        echo ''
        echo "Making ftpd."
        cd ../src
        make $makeargs  ftpd
        echo ''
        echo "Making ftpcount."
        make $makeargs  ftpcount
        echo ''
        echo "Making ftpshut".
        make $makeargs  ftpshut
        cd ..
        if [ ! -d bin ] ;  then    mkdir bin;        fi
        cd bin
        rm -f ftpd ftpcount ftpshut
        if [ -s ../src/ftpd ] ;      then ln ../src/ftpd  ftpd      ; fi
        if [ -s ../src/ftpcount ] ;  then ln ../src/ftpcount ftpcount ; fi
        if [ -s ../src/ftpshut ] ;   then ln ../src/ftpshut ftpshut    ; fi
        cd ..
        echo ''
        echo "Links to executables are in bin directory:"
        size bin/ftpd bin/ftpcount bin/ftpshut
        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
        echo "Cleaning support directory."
        cd support
        make -f makefiles/Makefile.gen $makeargs clean
		rm -f Makefile
        echo "Cleaning src directory."
        cd ../src
        make -f makefiles/Makefile.gen $makeargs clean
		rm -f Makefile
		rm -f Makefile.bak
		rm -f config.h
        echo "Cleaning bin directory."
        cd ..
        if [ -d bin ] ;  then    rm -rf bin;        fi
        ;;

    install)
        make -f Makefile install
        ;;

    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
