:

set -u

argone=${1-notset}
argtwo=${2-notset}

if [ $argone = "notset" ] ; then
  envone=${NETWENVONE-notset}
  if [ $envone != "notset" ] ; then
    argone=$envone
  fi
fi
if [ $argtwo = "notset" ] ; then
  envtwo=${NETWENVTWO-notset}
  if [ $envtwo != "notset" ] ; then
    argtwo=$envtwo
  fi
fi

# Define the variables. Edit config.dat if you want to make changes.
. ./config.dat
. ./compil/unix/version.sh


########################################################################
echo "Netwox version ${NETWOXDEF_VERSION} (${NETWOXDEF_VERSIONMAJOR} ${NETWOXDEF_VERSIONMINOR} ${NETWOXDEF_VERSIONMICRO})"
netwv="${NETWOXDEF_VERSIONMAJOR}${NETWOXDEF_VERSIONMINOR}"


########################################################################
echo "Searching netwib$netwv-config"

if [ ${NETWIBDEF_INSTBIN-notset} = "notset" ] ; then
  netwibconfig="netwib$netwv-config"
  type $netwibconfig 2> /dev/null 1> /dev/null
  if [ "$?Z" != "0Z" ] ; then
    echo "Error: $netwibconfig wasn't found in the PATH($PATH). Did you install netwib ?"
    echo "You can edit config.dat to specify an alternate location for $netwibconfig."
    exit
  fi
else
  netwibconfig="${NETWIBDEF_INSTBIN}/netwib$netwv-config"
  type $netwibconfig 2> /dev/null 1> /dev/null
  if [ "$?Z" != "0Z" ] ; then
    echo "Error: netwib$netwv-config wasn't found in ${NETWIBDEF_INSTBIN}"
    exit
  fi
fi


########################################################################
echo "Setting variables"
NETWOXDEF_PROGAR=`$netwibconfig -d PROGAR`
NETWOXDEF_PROGCC=`$netwibconfig -d PROGCC`
NETWOXDEF_PROGGREP=`$netwibconfig -d PROGGREP`
NETWOXDEF_PROGRANLIB=`$netwibconfig -d PROGRANLIB`
NETWOXDEF_PROGWC=`$netwibconfig -d PROGWC`
NETWOXDEF_PROGFIND=find
NETWOXDEF_PROGSED=sed
NETWOXDEF_PROGTR=tr
NETWOXDEF_PROGSORT=sort

NETWOXDEF_INSTBIN=`$netwibconfig -d INSTBIN`
NETWOXDEF_INSTMAN1=`$netwibconfig -d INSTMAN1`

NETWOXDEF_INSTUSER=`$netwibconfig -d INSTUSER`
NETWOXDEF_INSTGROUP=`$netwibconfig -d INSTGROUP`

gccOpt="-Wall -ansi"
gccInc=`$netwibconfig -c`
gccLib=`$netwibconfig -l`


########################################################################
echo "Program selection"

progcheck ()
{ progname=$1
  progdesc="$2"

  type $progname 2> /dev/null 1> /dev/null
  if [ "$?Z" != "0Z" ] ; then
    echo "Error: the $progdesc '$progname' wasn't found"
    exit
  else
    echo " $progname yes"
  fi
}

progcheck $NETWOXDEF_PROGAR "archiver"
progcheck $NETWOXDEF_PROGCC "compiler"
progcheck $NETWOXDEF_PROGGREP "grep"
progcheck $NETWOXDEF_PROGRANLIB "ranlib"
progcheck $NETWOXDEF_PROGWC "wc"
progcheck $NETWOXDEF_PROGFIND "find"
progcheck $NETWOXDEF_PROGSED "sed"
progcheck $NETWOXDEF_PROGTR "tr"
progcheck $NETWOXDEF_PROGSORT "sort"


########################################################################
echo "Directory selection"

if [ ! -d $NETWOXDEF_INSTBIN ] ; then
  echo "Error: the binary directory '$NETWOXDEF_INSTBIN' doesn't exists"
  exit
else
  echo " $NETWOXDEF_INSTBIN yes"
  foundit=`echo $PATH | $NETWOXDEF_PROGGREP $NETWOXDEF_INSTBIN | $NETWOXDEF_PROGWC -l`
  if [ $foundit -ne 1 ] ; then
    echo "Warning: the directory '$NETWOXDEF_INSTBIN' is not in the PATH"
    echo "To execute netwox, you'll have to run '$NETWOXDEF_INSTBIN/netwox' instead of 'netwox'."
    echo "Press enter to continue"
    read foobar
  fi
fi

if [ ! -d $NETWOXDEF_INSTMAN1 ] ; then
  echo "Error: the help directory '$NETWOXDEF_INSTMAN1' doesn't exists"
  exit
else
  echo " $NETWOXDEF_INSTMAN1 yes"
fi


########################################################################
echo "Generate the different files"

#########
deffile="def.h"
echo " Creation of $deffile"
if [ $argone = "test" ] ; then
  echo "#include <netwib.h>" > $deffile
else
  echo "#include <netwib$netwv.h>" > $deffile
fi
echo "" >> $deffile
echo "#define NETWOXDEF_VERSION \"$NETWOXDEF_VERSION\"" >> $deffile
echo "#define NETWOXDEF_VERSIONMAJOR $NETWOXDEF_VERSIONMAJOR" >> $deffile
echo "#define NETWOXDEF_VERSIONMINOR $NETWOXDEF_VERSIONMINOR" >> $deffile
echo "#define NETWOXDEF_VERSIONMICRO $NETWOXDEF_VERSIONMICRO" >> $deffile
echo "#define NETWOXDEF_PROGCC \"$NETWOXDEF_PROGCC\"" >> $deffile
echo "#define NETWOXDEF_PROGAR \"$NETWOXDEF_PROGAR\"" >> $deffile
echo "#define NETWOXDEF_PROGGREP \"$NETWOXDEF_PROGGREP\"" >> $deffile
echo "#define NETWOXDEF_PROGRANLIB \"$NETWOXDEF_PROGRANLIB\"" >> $deffile
echo "#define NETWOXDEF_PROGWC \"$NETWOXDEF_PROGWC\"" >> $deffile
echo "#define NETWOXDEF_PROGFIND \"$NETWOXDEF_PROGFIND\"" >> $deffile
echo "#define NETWOXDEF_PROGSED \"$NETWOXDEF_PROGSED\"" >> $deffile
echo "#define NETWOXDEF_PROGTR \"$NETWOXDEF_PROGTR\"" >> $deffile
echo "#define NETWOXDEF_INSTBIN \"$NETWOXDEF_INSTBIN\"" >> $deffile
echo "#define NETWOXDEF_INSTMAN1 \"$NETWOXDEF_INSTMAN1\"" >> $deffile
echo "#define NETWOXDEF_INSTUSER \"$NETWOXDEF_INSTUSER\"" >> $deffile
echo "#define NETWOXDEF_INSTGROUP \"$NETWOXDEF_INSTGROUP\"" >> $deffile


#########
echo " Creation of Makefiles"

netwox_file_list ()
{
  thedir=$1
  $NETWOXDEF_PROGFIND $thedir -type f -name "*.c" |\
    $NETWOXDEF_PROGSED 's|^.*/||' | $NETWOXDEF_PROGSED 's|\.c$||' |\
    $NETWOXDEF_PROGSORT
}

netwox_head_make ()
{
  cat <<EOF
#----------------------------------------------------------------------
# This file was generated with ./genemake

#----------------------------------------------------------------------
# variables which can be modified
CC=$NETWOXDEF_PROGCC
AR=$NETWOXDEF_PROGAR
RANLIB=$NETWOXDEF_PROGRANLIB
INSTBIN=$NETWOXDEF_INSTBIN
INSTMAN1=$NETWOXDEF_INSTMAN1
INSTUSERGROUP=$NETWOXDEF_INSTUSER:$NETWOXDEF_INSTGROUP

GCCOPTa=-g -Wtraditional -pedantic -W
GCCOPTb=-Wshadow -Wpointer-arith -Wundef -Wsign-compare
GCCOPTc=-Wwrite-strings -Wcast-align -Wstrict-prototypes -Wcast-qual
GCCOPTd=-Wnested-externs
GCCOPTe=-Wmissing-prototypes
GCCOPTf=-Wbad-function-cast
GCCOPTg=-Werror
GCCOPTh=-Wmissing-declarations
GCCOPTy=\$(GCCOPTa) \$(GCCOPTb) \$(GCCOPTc) \$(GCCOPTd)
GCCOPTz=\$(GCCOPTe) \$(GCCOPTf) \$(GCCOPTg) \$(GCCOPTh)
EOF

if [ $argone = "test" ] ; then
  echo "#GCCOPT=$gccOpt"
  echo "GCCOPT=$gccOpt \$(GCCOPTy) \$(GCCOPTz)"
else
  echo "#GCCOPT=$gccOpt \$(GCCOPTy) \$(GCCOPTz)"
  echo "GCCOPT=-O2 $gccOpt"
fi

cat <<EOF
GCCINC=$gccInc

GCCLIB=$gccLib


#----------------------------------------------------------------------

# we want ...
EOF
}

# notes :
# Modules and tools are both stored in modtool.a, because
# when stored separately, they had cross dependencies bringing
# problems on some systems.
# Instead of using "ar -r" which is slow on first call, we use
# "ar -d" and "ar -q".
netwox_dir_make ()
{
  dependh=$1
  thedir=$2

  echo "  $thedir"

  netwox_head_make > $thedir/Makefile

  cat >> $thedir/Makefile <<EOF
all: ../modtool.a

../modtool.a: `netwox_file_list $thedir | $NETWOXDEF_PROGSED 's|$|.o|' | ${NETWOXDEF_PROGTR} '\012' ' '`

EOF
  dm=$thedir/Makefile
  netwox_file_list $thedir | while read f
  do
    if [ $dependh -eq 1 ] ; then
      if [ $f = "tool" ] ; then
        echo "$f.o: $f.c $f.h ../tools.list ../tools.h" >> $thedir/Makefile
      else
        echo "$f.o: $f.c $f.h" >> $thedir/Makefile
      fi
    else
      echo "$f.o: $f.c" >> $thedir/Makefile
    fi
    echo "	@if [ -f $f.o ] ; then \$(AR) -d ../modtool.a $f.o ; fi" >> $dm
    echo "	\$(CC) \$(GCCOPT) \$(GCCINC) -c -o $f.o $f.c" >> $dm
    echo "	\$(AR) -q ../modtool.a $f.o" >> $dm
  done

}

netwox_dir_make 0 tools
netwox_dir_make 1 modules

##
netwox_main_make ()
{
  netwox_head_make > Makefile

  echo "  ."

  cat >> Makefile <<EOF
all: netwox

netwoxtools:
	cd tools ; make
netwoxmodules:
	cd modules ; make

netwox: netwoxmodules netwoxtools
	\$(RANLIB) modtool.a
	\$(CC) \$(GCCOPT) \$(GCCINC) netwox.c -o netwox modtool.a \$(GCCLIB)
EOF
if [ $argone != "test" ] ; then
  echo "	@echo \"OK - You can now install netwox with 'make install'\"" >> Makefile
fi
cat >> Makefile <<EOF

installnetwox: netwox
	cp netwox \$(INSTBIN)/netwox$netwv
	chown \$(INSTUSERGROUP) \$(INSTBIN)/netwox$netwv
	chmod 555 \$(INSTBIN)/netwox$netwv
	rm -f \$(INSTBIN)/netwox
	ln -s netwox$netwv \$(INSTBIN)/netwox
	chown \$(INSTUSERGROUP) \$(INSTBIN)/netwox

installman:
	cp ../doc/netwox.1 \$(INSTMAN1)/netwox$netwv.1
	chown \$(INSTUSERGROUP) \$(INSTMAN1)/netwox$netwv.1
	chmod 444 \$(INSTMAN1)/netwox$netwv.1
	rm -f \$(INSTMAN1)/netwox.1
	ln -s netwox$netwv.1 \$(INSTMAN1)/netwox.1
	chown \$(INSTUSERGROUP) \$(INSTMAN1)/netwox.1

install: installnetwox installman
	@echo ""
	@echo "OK - You can now run netwox with 'netwox' or '\$(INSTBIN)/netwox'"
	@echo "I suggest you to start with 'netwox 1', as explained ../INSTALLUNIX_EN.TXT"

uninstall: 
	@echo "Several versions of netwox can be installed on the same system."
	@echo "I left you remove exactly the version you want to remove :"
	@echo "  \$(INSTBIN)/netwox*"
	@echo "  \$(INSTMAN1)/netwox*.1"
	@echo "If you are SURE about what you do, you can for example enter :"
	@echo "  rm -rf \$(INSTBIN)/netwox*"
	@echo "  rm -rf \$(INSTMAN1)/netwox*.1"

# cleaning
clean:
	rm -f modules/*.o
	rm -f tools/*.o
	rm -f netwox modtool.a core netwox.core
cleanall: clean
	rm -f def.h Makefile modules/Makefile tools/Makefile

EOF
}

netwox_main_make


########################################################################
########################################################################
if [ $argone != "test" ] ; then
echo "End of genemake"
echo ""
echo "OK - You can now compile with 'make'"
fi
