:

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 "Netwag version ${NETWAGDEF_VERSION} (${NETWAGDEF_VERSIONMAJOR} ${NETWAGDEF_VERSIONMINOR} ${NETWAGDEF_VERSIONMICRO})"
netwv="${NETWAGDEF_VERSIONMAJOR}${NETWAGDEF_VERSIONMINOR}"


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

netwibconfigfake ()
{
  case $2 in
    INSTBIN) echo "../../netwox-src/src";;
    INSTMAN1) echo "/tmp";;
    INSTUSER) echo "root";;
    INSTGROUP) echo "root";;
  esac
}
if [ $argone = "test" ] ; then
  netwibconfig=netwibconfigfake
else
  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 ?"
    exit
  fi
fi


########################################################################
echo "Setting variables"
NETWAGDEF_INSTBIN=`$netwibconfig -d INSTBIN`
NETWAGDEF_INSTMAN1=`$netwibconfig -d INSTMAN1`
NETWAGDEF_INSTUSER=`$netwibconfig -d INSTUSER`
NETWAGDEF_INSTGROUP=`$netwibconfig -d INSTGROUP`


########################################################################
bin_netwox_found=0
if [ $argone = "test" ] ; then
  netwox="netwox"
  bin_netwox_found=1
else
  netwox="netwox$netwv"
fi
echo "Searching path to $netwox"

# first, try with NETWAGDEF_INSTBIN
bin_netwox="${NETWAGDEF_INSTBIN}/$netwox"
if [ -x $bin_netwox ] ; then
  bin_netwox_found=1
fi

# now, try with the path
if [ "Z$bin_netwox_found" != "Z1" ] ; then
  bin_netwox=`./findpath.sh $netwox`
  if [ "Z$bin_netwox" != "Z" ] ; then
    bin_netwox_found=1
  fi
fi

# eventually quit here
if [ "Z$bin_netwox_found" != "Z1" ] ; then
  echo "Error: $netwox wasn't found in the PATH($PATH). Did you install netwox ?"
  exit
fi

# display message
echo " Found at $bin_netwox"


########################################################################
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
INSTBIN=$NETWAGDEF_INSTBIN
INSTMAN1=$NETWAGDEF_INSTMAN1
INSTUSERGROUP=$NETWAGDEF_INSTUSER:$NETWAGDEF_INSTGROUP


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

netwag: netwag_vars1v_unix.tcl netwag_vars2v_unix.tcl
	./concat.sh unix $bin_wish > netwag
	chmod 555 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
	cp netwag \$(INSTBIN)/netwag$netwv
	chown \$(INSTUSERGROUP) \$(INSTBIN)/netwag$netwv
	chmod 555 \$(INSTBIN)/netwag$netwv
	rm -f \$(INSTBIN)/netwag
	ln -s netwag$netwv \$(INSTBIN)/netwag
	chown \$(INSTUSERGROUP) \$(INSTBIN)/netwag
	cp ../doc/netwag.1 \$(INSTMAN1)/netwag$netwv.1
	chown \$(INSTUSERGROUP) \$(INSTMAN1)/netwag$netwv.1
	chmod 444 \$(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 'netwag' if \$(INSTBIN) is in your PATH)"

# 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'"
