:

set -u

########################################################################
# Those parameters are used in test mode
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
netwibconfigfake ()
{
  case $2 in
    INSTBIN) echo "../../netwox-src/src";;
    INSTMAN1) echo "/tmp";;
    PROGGREP) echo "grep";;
    PROGWC) echo "wc";;
  esac
}


########################################################################
. ./compil/unix/version.sh
echo "Netwag version ${NETWAGDEF_VERSION} (${NETWAGDEF_VERSIONMAJOR} ${NETWAGDEF_VERSIONMINOR} ${NETWAGDEF_VERSIONMICRO})"
netwv="${NETWAGDEF_VERSIONMAJOR}${NETWAGDEF_VERSIONMINOR}"


########################################################################
echo "Loading config.dat"
. ./config.dat


########################################################################
echo "Checking programs"

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 $netwibconfig "netwib configuration"
progcheck $netwox "netwox toolbox"
progcheck $NETWAGDEF_PROGGREP "grep"
progcheck $NETWAGDEF_PROGWC "wc"


########################################################################
echo "Setting variables"
if [ $argone = "test" ] ; then
  bin_netwox=../../../test/netwox/netwox
else 
  bin_netwox=$netwoxfinal
fi

########################################################################
echo "Checking directories"

if [ ! -d $NETWAGDEF_INSTBIN ] ; then
  echo "Warning: the binary directory '$NETWAGDEF_INSTBIN' doesn't exists"
else
  echo " $NETWAGDEF_INSTBIN yes"
  foundit=`echo $PATH | $NETWAGDEF_PROGGREP $NETWAGDEF_INSTBIN | $NETWAGDEF_PROGWC -l`
  if [ $foundit -ne 1 ] ; then
    echo "Warning: the directory '$NETWAGDEF_INSTBIN' is not in the PATH"
    echo "To execute netwag, you'll have to run '$NETWAGDEF_INSTBIN/netwag' instead of 'netwag'."
  fi
fi

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


########################################################################
echo "Checking Tcl/Tk"

bin_wish=""
./findwish.sh wish
if [ "Z$?" = "Z0" ] ; then
  bin_wish=`./findpath.sh wish`
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8`
  fi
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8.4
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8.4`
  fi
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8.3
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8.3`
  fi
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8.2
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8.2`
  fi
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8.1
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8.1`
  fi
fi
if [ "Z$bin_wish" = "Z" ] ; then
  ./findwish.sh wish8.0
  if [ "Z$?" = "Z0" ] ; then
    bin_wish=`./findpath.sh wish8.0`
  fi
fi

if [ "Z$bin_wish" = "Z" ] ; then
  echo "Error: wish wasn't found in the PATH($PATH). Did you install Tcl/Tk ?"
  exit
fi


########################################################################
echo "Checking available shells"

bin_shell_prefer=""
if [ -x /bin/ksh ] ; then
  bin_shell_ksh="/bin/ksh"
  echo " /bin/ksh"
  bin_shell_prefer="ksh"
else
  echo " no /bin/ksh"
  bin_shell_ksh=""
fi
if [ -x /bin/csh ] ; then
  bin_shell_csh="/bin/csh"
  echo " /bin/csh"
  bin_shell_prefer="csh"
else
  echo " no /bin/csh"
  bin_shell_csh=""
fi
if [ -x /bin/tcsh ] ; then
  bin_shell_tcsh="/bin/tcsh"
  echo " /bin/tcsh"
  bin_shell_prefer="tcsh"
else
  echo " no /bin/tcsh"
  bin_shell_tcsh=""
fi
if [ -x /bin/sh ] ; then
  bin_shell_sh="/bin/sh"
  echo " /bin/sh"
  bin_shell_prefer="sh"
else
  echo " no /bin/sh"
  bin_shell_sh=""
fi

if [ "Z$bin_shell_prefer" = "Z" ] ; then
  echo "Error : I found no shells"
  exit
else
  echo " preferred shell : $bin_shell_prefer"
fi


########################################################################
echo "Searching terminal emulators"

bin_term_prefer=""
bin_term_cmdtool=`./findpath.sh cmdtool`
if [ "Z$bin_term_cmdtool" != "Z" ] ; then
  echo " $bin_term_cmdtool"
  bin_term_prefer="cmdtool"
else
  echo " no cmdtool"
fi
bin_term_dtterm=`./findpath.sh dtterm`
if [ "Z$bin_term_dtterm" != "Z" ] ; then
  echo " $bin_term_dtterm"
  bin_term_prefer="dtterm"
else
  echo " no dtterm"
fi
bin_term_konsole=`./findpath.sh konsole`
if [ "Z$bin_term_konsole" != "Z" ] ; then
  echo " $bin_term_konsole"
  bin_term_prefer="konsole"
else
  echo " no konsole"
fi
bin_term_gnometerminal=`./findpath.sh gnome-terminal`
if [ "Z$bin_term_gnometerminal" != "Z" ] ; then
  echo " $bin_term_gnometerminal"
  bin_term_prefer="gnometerminal"
else
  echo " no gnome-terminal"
fi
bin_term_rxvt=`./findpath.sh rxvt`
if [ "Z$bin_term_rxvt" != "Z" ] ; then
  echo " $bin_term_rxvt"
  bin_term_prefer="rxvt"
else
  echo " no rxvt"
fi
bin_term_eterm=`./findpath.sh eterm`
if [ "Z$bin_term_eterm" != "Z" ] ; then
  echo " $bin_term_eterm"
  bin_term_prefer="eterm"
else
  echo " no eterm"
fi
bin_term_nxterm=`./findpath.sh nxterm`
if [ "Z$bin_term_nxterm" != "Z" ] ; then
  echo " $bin_term_nxterm"
  bin_term_prefer="nxterm"
else
  echo " no nxterm"
fi
bin_term_xterm=`./findpath.sh xterm`
if [ "Z$bin_term_xterm" != "Z" ] ; then
  echo " $bin_term_xterm"
  bin_term_prefer="xterm"
else
  echo " no xterm"
fi

if [ "Z$bin_term_prefer" = "Z" ] ; then
  echo "Error : No terminal emulator found"
  exit
else
  echo " preferred terminal emulator : $bin_term_prefer"
fi


########################################################################
echo "Creation of netwag_vars1v_unix.tcl"
echo "set netwag_glo_dir_tmp \"/tmp\"" > netwag_vars1v_unix.tcl
echo "set netwag_glo_file_session \"\$env(HOME)/.netwag.ses\"" >> netwag_vars1v_unix.tcl
echo "set netwag_glo_bin_netwox \"$bin_netwox\"" >> netwag_vars1v_unix.tcl


########################################################################
echo "Creation of netwag_vars2v_unix.tcl"
echo "set netwag_glo_bin_shell(prefer) \"$bin_shell_prefer\"" > netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_shell(sh) \"$bin_shell_sh\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_shell(csh) \"$bin_shell_csh\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_shell(tcsh) \"$bin_shell_tcsh\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_shell(ksh) \"$bin_shell_ksh\"" >> netwag_vars2v_unix.tcl
echo "" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(prefer) \"$bin_term_prefer\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(xterm) \"$bin_term_xterm\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(nxterm) \"$bin_term_nxterm\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(eterm) \"$bin_term_eterm\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(rxvt) \"$bin_term_rxvt\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(gnometerminal) \"$bin_term_gnometerminal\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(konsole) \"$bin_term_konsole\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(dtterm) \"$bin_term_dtterm\"" >> netwag_vars2v_unix.tcl
echo "set netwag_glo_bin_term(cmdtool) \"$bin_term_cmdtool\"" >> netwag_vars2v_unix.tcl


########################################################################
echo "Creation of makefile"

cat > ./Makefile <<EOF
#----------------------------------------------------------------------
# This file was generated with ./genemake

#----------------------------------------------------------------------
# variables
DESTDIR=
INSTBIN=\$(DESTDIR)$NETWAGDEF_INSTBIN
INSTMAN1=\$(DESTDIR)$NETWAGDEF_INSTMAN1


#----------------------------------------------------------------------
# we want only netwag
all: netwag

netwag: netwag_vars1v_unix.tcl netwag_vars2v_unix.tcl
	./concat.sh unix $bin_wish > netwag
	chmod 755 netwag
	@echo ""
	@echo "OK - You may want to edit netwag to change variables declared"
	@echo "     at the top of the file."
	@echo "     Then install netwag with 'make install'"

# installation : when everything is ok
install: netwag
	[ -d \$(INSTBIN) ] || mkdir -p \$(INSTBIN)
	cp netwag \$(INSTBIN)/netwag$netwv
	chmod 755 \$(INSTBIN)/netwag$netwv
	rm -f \$(INSTBIN)/netwag
	ln -s netwag$netwv \$(INSTBIN)/netwag
	[ -d \$(INSTMAN1) ] || mkdir -p \$(INSTMAN1)
	cp ../doc/netwag.1 \$(INSTMAN1)/netwag$netwv.1
	chmod 644 \$(INSTMAN1)/netwag$netwv.1
	rm -f \$(INSTMAN1)/netwag.1
	ln -s netwag$netwv.1 \$(INSTMAN1)/netwag.1
	@echo ""
	@echo "OK - You can now run netwag with 'netwag' or '\$(INSTBIN)/netwag'"

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

# cleaning
clean:
	rm -f netwag
cleanall: clean
	rm -f Makefile
	rm -f netwag_vars1v_unix.tcl netwag_vars2v_unix.tcl

EOF


########################################################################
########################################################################
echo "End of genemake"

###########
echo ""
echo "OK - You can now compile with 'make'"
