#!/bin/sh

download_gpg()
{
  echo
  echo Choose a download site:
  echo
  echo "1. ftp.gnupg.ca (Canada)"
  echo "2. ftp.planetmirror.com (Australia)"
  echo "3. ftp.ayamura.org (Japan)"
  echo "4. gd.tuwien.ac.at (Austria)"
  echo "5. ftp.mirror.ac.uk (United Kingdom)"
  echo "6. ftp.gnupg.org"
  echo
  echo -n "Enter your selection [1]: "
  read site

  case $site in
    1)
      ftpsite="ftp.gnupg.ca"
      ftpdir="/gnupg"
      ;;
    2)
      ftpsite="ftp.planetmirror.com"
      ftpdir="/pub/gnupg/"
      ;;
    3)
      ftpsite="ftp.ayamura.org"
      ftpdir="/pub/gnupg/gnupg"
      ;;
    4)
      ftpsite="gd.tuwien.ac.at"
      ftpdir="/privacy/gnupg/gnupg"
      ;;
    5)
      ftpsite="ftp.mirror.ac.uk"
      ftpdir="/sites/ftp.gnupg.org/gcrypt/gnupg"
      ;;
    6)
      ftpsite="ftp.gnupg.org"
      ftpdir="/gcrypt/gnupg/"
      ;;
    *)
      ftpsite="ftp.gnupg.ca"
      ftpdir="/gnupg"
      ;;
  esac

  echo
  echo Downloading gnupg
  echo

  ftp -v -n $ftpsite <<!
     user anonymous anon@anon.net
     binary
     prompt
     cd $ftpdir
     get $GPG_TARBALL
     quit
!

  /bin/mv -f $GPG_TARBALL GPG
}

get_full_filename()
{
  read fn
  if [ "$fn" = "" ]; then
    full_fn=$fn
  else
    if [ -f $fn ]; then
      firstchar=`echo $fn | cut -c 1`
      if [ "$firstchar" = "/" ]; then
        full_fn=$fn
      else
        full_fn=$PWD/$fn
      fi
    else
      full_fn=""
    fi
  fi
}

get_rpclib()
{
  input="bad"
  while [ "$input" != "ok" ]
  do
    echo ""
    echo -n "Enter the full location of the 'librpclib.a' file: "
    get_full_filename
    rpclib="$full_fn"
    if [ "$rpclib" = "" ]; then
      input="bad"
      rpclib="$fn"
      echo "I cannot find $rpclib"
    else
      if [ -f "$rpclib" ]; then
        input="ok"
      else
        input="bad"
        rpclib="$fn"
        echo "I cannot find $rpclib"
      fi
    fi
  done
}

get_user_choice()
{
  input="bad"
  while [ "$input" != "ok" ]
  do
    input="ok"
    echo Choose one of the following:
    echo
    echo 1. Specify an alternate location for the gpg program.
    echo 2. Let NetSolve download gpg tarball.
    echo 3. Download gpg yourself.
    echo 4. Use already downloaded gpg tarball.
    echo 5. Skip gpg specification and do not enable in NetSolve.
    echo
    echo -n "Enter your choice: "
    read num
    case $num in
      1)
         echo
         echo -n "Enter the path of the gpg program: "
         get_full_filename
         if [ "$full_fn" = "" ]; then
           echo
           echo Cannot open \'$fn\'.
           echo
           input="bad"
         else
           configure_args="--with-gpg=$full_fn"
         fi
         ;;
      2)
         download_gpg
         if [ -f GPG/$GPG_TARBALL ]; then
           echo Download complete.
           configure_args="--with-buildgpg=$PWD/GPG/$GPG_TARBALL"
         else
           echo
           echo Download failed.
           echo
           input="bad"
         fi
         ;;
      3)
         echo
         echo Download gpg and place in the GPG subdirectory.
         echo -n "When you have finished enter the tarball name: "
         get_full_filename
         if [ "$full_fn" = "" ]; then
           echo
           echo Cannot open \'$fn\'.
           echo
           input="bad"
         else
           configure_args="--with-buildgpg=$full_fn"
         fi
         ;;
      4)
         echo
         echo -n "Enter the tarball name: "
         get_full_filename
         if [ "$full_fn" = "" ]; then
           echo
           echo Cannot open \'$fn\'.
           echo
           input="bad"
         else
           configure_args="--with-buildgpg=$full_fn"
         fi
         ;;
      5)
         echo
         echo GnuPG will not be enabled in NetSolve.
         echo
         configure_args="skipgpg"
         ;;
      *)
         echo
         echo Warning: invalid choice \'$num\'.  Specify again.
         echo
         input="bad"
         ;;
    esac
  done
}

GPG_TARBALL=gnupg-1.2.3.tar.gz

clear

if [ -f ./server_config ]; then
  echo ""
  echo "NetSolve 2.0 Installation"
  echo "-------------------------"
  echo ""
  echo "This will install NetSolve in the current directory."
else
  echo "This script should be run from the root of the NetSolve"
  echo "code distribution.  Change to that directory and run the"
  echo "installation script again."
  exit 1
fi

input="bad"
while [ "$input" != "ok" ]
do
  input="ok"

  install_agent="no"
  install_client="no"
  install_server="no"
  install_testers="no"
  install_tools="no"
  install_gridrpc="no"
  install_matlab="no"
  install_octave="no"

  echo ""
  echo "Choose the components to be installed (multiple selections allowed)."
  echo ""
  echo "1. Standard (Client, Server, Agent, Testers, Tools)"
  echo "2. Client"
  echo "3. Server"
  echo "4. Agent"
  echo "5. Tools"
  echo "6. Testers"
  echo "7. GridRPC API"
  echo "8. Matlab interface"
  echo "9. Octave interface"
  echo ""
  echo -n "Enter the numbers of your choices separated by spaces (0 to exit): "
  read first second third fourth fifth sixth seventh eighth ninth tenth

  if [ "$first" = "0" ]; then
    echo Exiting.
    exit 0
  fi

  if [ "$first" = "" ]; then
    input="bad"
    continue
  fi

  if [ "$tenth" != "" ]; then
    echo Error: extra input.
    input="bad"
    continue
  fi

  for num in $first $second $third $fourth $fifth $sixth $seventh $eighth $ninth
  do
    case $num in 
      1)
         install_client="yes"
         install_server="yes"
         install_agent="yes"
         install_tools="yes"
         install_testers="yes"
         ;;
      2)
         install_client="yes"
         ;;
      3)
         install_server="yes"
         ;;
      4)
         install_agent="yes"
         ;;
      5)
         install_tools="yes"
         ;;
      6)
         install_testers="yes"
         ;;
      7)
         install_gridrpc="yes"
         ;;
      8)
         install_matlab="yes"
         ;;
      9)
         install_octave="yes"
         ;;
      *)
         echo
         echo Warning: invalid choice \'$num\'.  Specify again.
         echo
         input="bad"
         ;;
    esac
  done
done

if [ "$install_server" = "yes" ]; then

  echo
  echo NetSolve has a feature called \'Hardware/Software Server\'
  echo which allows software to be transferred between different 
  echo NetSolve servers.  For enhanced security, NetSolve can be 
  echo configured to use GnuPG to sign and verify the software.
  echo
  echo GnuPG stands for GNU Privacy Guard and is GNU\'s tool for secure 
  echo communication and data storage. It can be used to encrypt data 
  echo and to create digital signatures. It includes an advanced key 
  echo management facility and is compliant with the proposed OpenPGP 
  echo Internet standard as described in RFC 2440.
  echo
  echo -n "Do you want to enable GPG in NetSolve (y|n)? "
  read answer

  if [ "$answer" = "y" ]; then
    gnupg_path=`which gpg`
    gnupg_ver=`gpg --version | egrep GnuPG`

    if [ "$gnupg_ver" != "" ]; then
      echo
      echo You appear to have gpg version \'$gnupg_ver\' on your system.
      echo We recommend gpg version 1.2.3.
      echo
      echo -n "Use '$gnupg_path' (y|n)? "
      read answer
      if [ "$answer" != "y" ]; then
        echo
        echo You have chosen not to use the existing gpg on your system.
        echo
        get_user_choice
      else
        configure_args=--with-gpg=${gnupg_path}
      fi
    else
      echo
      echo I can\'t find GnuPG on your system.  
      echo
      get_user_choice
    fi
  fi
fi

if [ "$configure_args" = "skipgpg" ]; then
  configure_args=""
fi

nsarch=`conf/config.guess | sed 's/[-|.]/_/g'`

case $nsarch in
    *cygwin*)
      rpclibpath=/lib
      rpclib="$rpclibpath/librpclib.a"

      if [ -f "$rpclib" ]; then
        echo
        echo "You appear to be running Cygwin.  Note that you will need to"
        echo "have the RPC library.  I see a version of this library at:"
        echo "  $rpclib"
        echo
        echo "Would you like to use this version?  (y/n)  [if you are not sure,"
        echo -n "just enter 'y']: "
        read answer

        if [ "$answer" != "y" ]; then
          get_rpclib
        fi
      else
        echo
        echo "You appear to be running Cygwin.  Note that you will need to"
        echo "download the RPC library, which contains the XDR routines used"
        echo "by NetSolve.  This library can be obtained in the following"
        echo "manner:"
        echo ""
        echo "     -Run setup.exe from http://www.cygwin.com, i.e."
        echo "        http://www.cygwin.com/setup.exe"
        echo ""
        echo "     -From the Libs category select sunrpc for download"
        echo ""
        echo "     -This installs librpclib.a in /lib,"
        echo "      the header files are installed into /usr/include"
        echo
        echo "Please perform the steps above before continuing with the installation."
        echo -n "When finished, hit [return]: "
        read answer
        echo
        rpclib="$rpclibpath/librpclib.a"
        if [ -f "$rpclib" ]; then
          echo "Ok, your installation appears to have succeeded."
        else
          echo "I cannot find $rpclib"
          get_rpclib
        fi
      fi
      rpctmp=`dirname $rpclib`
      rpcinc=`dirname $rpctmp`/include
      configure_args="$configure_args --with-rpclib=$rpclib --with-rpcinc=$rpcinc"
    ;;
esac

echo
echo "Now you may specify any additional arguments to configure (this"
echo "is optional).  Note that some systems have a limit on the number of"
echo "characters you can type, so if you have a long line of configure"
echo "options, you may want to split them up and enter them in smaller"
echo "chunks.  Begin entering your options:"
echo 
echo -n "Option [enter to quit]: "
read more_config_args
while [ "$more_config_args" != "" ]
do
  configure_args="$configure_args $more_config_args"
  echo -n "Next option [enter to quit]: "
  read more_config_args
done

echo Going to run \'./configure $configure_args\'
echo

hack=./ns_install_hack
/bin/rm -f "$hack"
cat >"$hack" <<EOSS
#!/bin/sh
./configure ${configure_args}
EOSS
chmod +x "$hack"

if "$hack"; then
  if [ "$install_server" = "yes" ]; then
    if make wrappers; then
      echo "Sparse wrappers built successfully."
    else
      echo "Failed to build Sparse wrappers."
      exit 1
    fi

    if make server; then
      echo "Server built successfully."
    else
      echo "Failed to build server."
      exit 1
    fi
  fi
  if [ "$install_agent" = "yes" ]; then
    if make agent; then
      echo "Agent built successfully."
    else
      echo "Failed to build agent."
      exit 1
    fi
  fi
  if [ "$install_client" = "yes" ]; then
    if make C Fortran; then
      echo "Client built successfully."
    else
      echo "Failed to build client."
      exit 1
    fi
  fi
  if [ "$install_tools" = "yes" ]; then
    if make tools; then
      echo "Tools built successfully."
    else
      echo "Failed to build tools."
      exit 1
    fi
  fi
  if [ "$install_testers" = "yes" ]; then
    if make tester; then
      echo "Testers built successfully."
    else
      echo "Failed to build testers."
      exit 1
    fi
  fi
  if [ "$install_gridrpc" = "yes" ]; then
    if make gridrpc; then
      echo "GridRPC built successfully."
    else
      echo "Failed to build GridRPC."
      exit 1
    fi
  fi
  if [ "$install_matlab" = "yes" ]; then
    if make matlab; then
      echo "Matlab interface built successfully."
    else
      echo "Failed to build Matlab interface."
      exit 1
    fi
  fi
  if [ "$install_octave" = "yes" ]; then
    if make octave; then
      echo "Octave interface built successfully."
    else
      echo "Failed to build Octave interface."
      exit 1
    fi
  fi
else
  echo "Failed to configure NetSolve."
  exit 1
fi

/bin/rm -f "$hack"

echo
echo
echo
echo NetSolve installation complete.  Some tips for getting started
echo with NetSolve:
echo
echo "   You may want to edit the @AGENT setting in the server_config file."
echo 
echo "   Remember to set the NETSOLVE_AGENT environment variable to"
echo "      the host name of your agent."
echo 
echo "   To start the agent:"
echo "     bin/$nsarch/NS_agent"
echo
echo "   To start the server:"
echo "     bin/$nsarch/NS_server"
echo

exit 0
