#!/bin/bash

############################################################################
#                                                                          #
#  Copyright (c) 2001, 2019 NoMachine, http://www.nomachine.com/.          #
#                                                                          #
#  Simple configure script used to build as part of the NX system.         #
#                                                                          #
#  Redistribution and use of the present software is allowed according     #
#  to terms specified in the file LICENSE which comes in the source        #
#  distribution.                                                           #
#                                                                          #
#  Check http://www.nomachine.com/licensing.html for other options.        #
#                                                                          #
#  NX and NoMachine are trademarks of Medialogic S.p.A.                    #
#                                                                          #
#  All rights reserved.                                                    #
#                                                                          #
############################################################################

#
# Include our configure functions.
#

if test -z "$ROOT"; then
  if test -x "../../../Configure"; then
    ROOT="../../.."
  else
    echo -ne "configure: error: can't find the main configure script\n\n"
    exit 1
  fi
fi

. $ROOT/Configure --functs || include_failed

#
# Setup the configuration environment.
#

setup_configure || setup_failed

#
# Check if the target is based on autoconf.
#
if test "$TARGET" = "Cygwin"  || test "$TARGET" = "MinGW" || \
   test "$TARGET" = "MinGW64"; then

  create_makefile || makefile_failed

  #
  # Now post-process the makefile.
  #

  replace_targets Makefile

  if ( test $# -eq 1 && test "$1" = "--build" ); then

    #add_command Makefile clean "rm -rf Win/Drivers/evuh/objchk_*"
    #add_command Makefile distclean "rm -f $BUILD/bin/nxusb32.exe"

    COMPILATION=`cat Win/prebuilt/compilation`

    sed 's/#COMPILATION/'$COMPILATION'/g' Win/prebuilt/resource.rc.in > Win/Drivers/resource.rc 
    
    if test -z "$WINDDK_BUILD_PATH"; then

      echo "Variable:"
      echo "WINDDK_BUILD_PATH"
      echo "is not set. Seting default variables path."
      export WINDDK_BUILD_PATH="D:\\WinDDK\\7600.16385.1"

    fi

	  # WINDDK_VERSION available options are fre/chk
    VERSION_DDK=fre

    cat >> Makefile <<EOF

objects:
	./Win/buildDDK.bat "$WINDDK_BUILD_PATH" "$VERSION_DDK"
EOF

      #FIXME: Following paths can be different on various Windows versions.
      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT5/i386"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wxp_x86/i386/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT5/i386"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT5/i386/nxtun.inf"

      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT6/i386"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wlh_x86/i386/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT6/i386"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT6/i386/nxtun.inf"

      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT5/amd64"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wnet_amd64/amd64/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT5/amd64"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT5/amd64/nxtun.inf"

      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT6/amd64"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wlh_amd64/amd64/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT6/amd64"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT6/amd64/nxtun.inf"

      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT5/ia64"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wnet_ia64/ia64/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT5/ia64"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT5/ia64/nxtun.inf"

    add_dependency Makefile all "objects"

      add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork/NT6/ia64"
      add_command Makefile install "cp -a Win/Drivers/obj${VERSION_DDK}_wlh_ia64/ia64/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT6/ia64"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/NT6/ia64/nxtun.inf"
      add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' Win/prebuilt/nxtun.inf.build.in > $BUILD/bin/drivers/nxnetwork/nxtun.inf"

      COMPILATION=$(($COMPILATION + 1))
      echo $COMPILATION > Win/prebuilt/compilation

    add_dependency Makefile all "objects"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wlh_amd64"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_amd64.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_amd64.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_amd64.err"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wlh_ia64"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_ia64.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_ia64.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_ia64.err"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wlh_x86"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_x86.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_x86.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wlh_x86.err"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wnet_amd64"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_amd64.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_amd64.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_amd64.err"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wnet_ia64"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_ia64.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_ia64.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wnet_ia64.err"

    add_command Makefile distclean "rm -rf Win/Drivers/objfre_wxp_x86"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wxp_x86.log"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wxp_x86.wrn"
    add_command Makefile distclean "rm -f Win/Drivers/buildfre_wxp_x86.err"

    add_command Makefile distclean "rm -f Win/Drivers/tapdrvr.cod"
  elif ( test $# -eq 1 && test "$1" = "--build-new" ); then
    # Please read Win/NewDrivers/README.rst before you use this option
    add_command Makefile install "python Win/NewDrivers/build.py --sdk=wdk -b"

    add_command Makefile install "cp -a Win/NewDrivers/dist/amd64/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT6/amd64/nxtun.sys"
    add_command Makefile install "cp -a Win/NewDrivers/dist/amd64/OemVista.inf $BUILD/bin/drivers/nxnetwork/NT6/amd64/nxtun.inf"
    add_command Makefile install "cp -a Win/NewDrivers/dist/i386/nxtun.sys $BUILD/bin/drivers/nxnetwork/NT6/i386/nxtun.sys"
    add_command Makefile install "cp -a Win/NewDrivers/dist/i386/OemVista.inf $BUILD/bin/drivers/nxnetwork/NT6/i386/nxtun.inf"
  else

    add_command Makefile install "rm -rf $BUILD/bin/drivers/nxnetwork"
    add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxnetwork"

    add_command Makefile install "cp -rp Win/prebuilt/kernel/nxtun/* $BUILD/bin/drivers/nxnetwork/"
    add_command Makefile install "cp -pa Win/prebuilt/nxtun.inf.in $BUILD/bin/drivers/nxnetwork/nxtun.inf"

    echo -n

  fi

  add_command Makefile distclean "rm -rf $BUILD/bin/drivers/nxnetwork"

  flush_targets Makefile

  echo -ne "configure: creating ./config.status\n"

  made ./config.status

elif test "$TARGET" = "MacOSX"; then

  create_makefile || makefile_failed

  #
  # Now post-process the makefile.
  #

  replace_targets Makefile

  if ( test $# -eq 1 && test "$1" = "--build" ); then

    add_command Makefile all "cd MacOSX/Drivers && make"

    COMPILATION=`cat MacOSX/prebuilt/compilation`

    add_command Makefile install "mv MacOSX/Drivers/nxtun.kext/Contents/Info.plist MacOSX/Drivers/nxtun.kext/Contents/Info.plist.tmp"
    add_command Makefile install "sed 's/#COMPILATION/$COMPILATION/g' MacOSX/Drivers/nxtun.kext/Contents/Info.plist.tmp > MacOSX/Drivers/nxtun.kext/Contents/Info.plist"
    add_command Makefile install "rm -f MacOSX/Drivers/nxtun.kext/Contents/Info.plist.tmp"

    COMPILATION=$(($COMPILATION + 1))

    echo $COMPILATION > MacOSX/prebuilt/compilation

    add_command Makefile install "cp -rp MacOSX/Drivers/nxtun.kext MacOSX/prebuilt/"

    add_command Makefile distclean "cd MacOSX/Drivers && make clean"

  else
    
    add_command Makefile install "rm -rf $BUILD/bin/drivers/nxtun"
    add_command Makefile install "mkdir -p $BUILD/bin/drivers/nxtun"

    add_command Makefile install "cp -rp MacOSX/prebuilt/nxtun.kext $BUILD/bin/drivers/nxtun"
    add_command Makefile install "chmod -R 755 $BUILD/bin/drivers/nxtun/nxtun.kext"
    add_command Makefile install "cp -rp MacOSX/prebuilt/nxtun.kext-legacy $BUILD/bin/drivers/nxtun"
    add_command Makefile install "chmod -R 755 $BUILD/bin/drivers/nxtun/nxtun.kext-legacy"

  fi

  add_command Makefile install "rm -rf $BUILD/scripts/init/nxtun"
  add_command Makefile install "mkdir -p $BUILD/scripts/init"

  add_command Makefile install "cp -rp MacOSX/prebuilt/startup_items/* $BUILD/scripts/init/."

  add_command Makefile distclean "rm -rf $BUILD/bin/drivers/nxtun"

  flush_targets Makefile

  echo -ne "configure: creating ./config.status\n"

  made ./config.status

elif test "$TARGET" = "Linux32"   || test "$TARGET" = "Linux64"   || \
     test "$TARGET" = "FreeBSD32" || test "$TARGET" = "FreeBSD64" || \
     test "$TARGET" = "Android"   || test "$TARGET" = "iOS"       || \
     test "$TARGET" = "Solaris"; then

  empty_makefile $ROOT || makefile_failed

  echo -ne "configure: creating ./config.status\n"

  made ./config.status

else

  echo -ne "configure: error: don't know what to do for target $TARGET\n\n"

  exit 1

fi
