#!/bin/sh
#
# (c) Raphael Langerhorst, May 2005
# see LICENCE.BSD
#

# change to correct directory

gdir=`echo $0 | sed "s/\/installer//g"`
if [ ! -z $gdir ]
then
  echo "changing to directory $gdir"
  cd "$gdir"
  echo "now in directory $PWD"
fi


which kdialog > /dev/null 2>&1

if [ ! $? -eq 0 ]
then
echo "Can't find kdialog binary. Graphical setup not possible."
echo "Please look at doc/INSTALL to find out how to install"
echo "the G System manually."
echo "Short version:"
echo "a) export the QTDIR, QMAKESPEC and KDEDIR environment variables"
echo "b) ./scripts/compile"
echo "c) export G_PREFIX to desired install prefix"
echo "d) ./scripts/makeinstall"
echo "e) You might want to compile the docs with ./scripts/makedocs"
exit 2
fi


if [ ! -z $1 ]
then
if [ $1 = "G_INSTALL_OPEN_README" ]
then
  
  kdialog --title "Installing..." --yesno "The G System is now being compiled and installed into your system. Depending on the number of selected components and the general performance of your computer this can take 5 to 15 minutes. You will be notified as soon as the installation is finished.\nMeanwhile you can read the README file if you want. It has some important information if you're new to the G System. Do you want to read the README while installing?"
  
  if [ $? -eq 0 ]
  then
    kwrite_command=`which kwrite`
  
    if [ ! -z $kwrite_command ]
    then
      $kwrite_command "$PWD/README"
    else
      kdialog --title "kwrite not found" --sorry "The README file can't be opened, kwrite was not found. The installation process is still installing the G System, but you have to open the README manually, you'll find it in the same directory as the installer."
    fi
  fi
  exit 0
fi
fi

if [ -e ./instrunningflag ]
then
  kdialog --title "Installation already running!" --sorry "Sorry, it looks like the installation is already running! Note that compiling the source code takes a while so you should be patient.\nIf you're sure that the installation is not active you can remove the file instrunningflag and restart the installer."
  exit 4
fi

touch ./instrunningflag

checkreturn()
{
  retvalue=$1
  if [ ! $retvalue -eq 0 ]
  then
    kdialog --title "Setup Canceled" --sorry "The installation of the G System was canceled, you won't be able to use the G System until you have successfully run the installer."
    rm ./instrunningflag
    exit $retvalue
  fi
}

autodetect_env()
{

  echo "autodetecting variables"
  
  if [ -z $QMAKESPEC ]
  then
    echo "QTDIR variable not set, autodetecting with uname"
    case `uname -s` in
      Linux)
        QMAKESPEC="linux-g++";;
      FreeBSD)
        QMAKESPEC="freebsd-g++";;
      NetBSD)
        QMAKESPEC="netbsd-g++";;
    esac
  fi
  
  if [ -z $QTDIR ]
  then
  echo "QTDIR not set, trying to locate the directory"
  locdir=`locate qt-mt.so`
  
  # TODO THIS will NOT work on MAC, they use some dynlib or whatever suffix
  QTDIR=`echo $locdir | sed "s/\/lib\/libqt-mt.so//g"`
  
  echo $QTDIR
  
  checkreturn $?
  fi
  
  # let's see if we can set KDEDIR with kde-config --prefix
  if [ -z $KDEDIR ]
  then
    echo "KDEDIR environment variable not set, using kde-config."
    which kde-config > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
      KDEDIR=`kde-config --prefix`
    else
      echo "kde-config could not be found!"
    fi
  fi
  
  if [ -z $G_PREFIX ]
  then
    G_PREFIX=$KDEDIR
  fi
  
  echo "autodetection done, variables:"
  
  echo "QMAKESPEC: $QMAKESPEC"
  echo "QTDIR:     $QTDIR"
  echo "KDEDIR:    $KDEDIR"
  echo "G_PREFIX:  $G_PREFIX"

}

set_QMAKESPEC()
{
  
  echo "QMAKESPEC: $QMAKESPEC"
  echo "Running QMAKESPEC dialog..."
  
  QMAKESPEC=`kdialog --title "Environment variable: QMAKESPEC" --inputbox "Set the QMAKESPEC variable (see http://doc.trolltech.com/3.3/qmake-manual-2.html)" $QMAKESPEC`
  
  checkreturn $?
  
  echo "Final QMAKESPEC value: $QMAKESPEC"

}

set_QTDIR()
{

  echo "QTDIR: $QTDIR"

  QTDIR=`kdialog --title "Environment variable: QTDIR" --inputbox "Set the QTDIR variable to the path of your Qt installation" $QTDIR`
  
  checkreturn $?
  
  echo "Final QTDIR value: $QTDIR"

}

set_KDEDIR()
{

  echo "KDEDIR: $KDEDIR"

  KDEDIR=`kdialog --title "Environment variable: KDEDIR" --inputbox "Set the KDEDIR variable to the path of your KDE installation" $KDEDIR`
  
  checkreturn $?
  
  echo "Final KDEDIR value: $KDEDIR"

}

set_G_PREFIX()
{

  echo "G_PREFIX: $G_PREFIX"

  G_PREFIX=`kdialog --title "Installation Prefix" --inputbox "Where do you want the G System to be installed?" $G_PREFIX`
  
  checkreturn $?
  
  echo "Final G_PREFIX value: $G_PREFIX"

}


set_options()
{

  INST_OPTIONS=`kdialog --title "Choose Components" --checklist "Which G System components should be installed?" SERVER "G Universe Server" on CLIENT "G Universe Client" on GOD "G Options Dialog (configuration utility)" on DEMO "Demo" on DOCS "Documentation" on APIREF "API Reference" off --separate-output`
  
  checkreturn $?
  
  echo $INST_OPTIONS
  
  # TODO There's probably a better way for reading in the options...
  
  for x in $INST_OPTIONS
  do
    case $x in
      SERVER)
        INST_SERVER="yes";;
      CLIENT)
        INST_CLIENT="yes";;
      GOD)
        INST_GOD="yes";;
      DEMO)
        INST_DEMO="yes";;
      DOCS)
        INST_DOCS="yes";;
      APIREF)
        INST_APIREF="yes";;
      *)
        echo "WARNING: Unknown installation option: $x";;
    esac
  done
  
  echo "Install options:"
  
  echo "Server: $INST_SERVER"
  echo "Client: $INST_CLIENT"
  echo "GOD: $INST_GOD"
  echo "Demo: $INST_DEMO"
  echo "Docs: $INST_DOCS"
  echo "APIREF: $INST_APIREF"

}


# Do not ask this, the user obviously started the installation.
# kdialog --title "G System Installer" --yesno "Do you want to install the G System now?"

# checkreturn $?

set_options

autodetect_env

if [ -z $QMAKESPEC ]
then
  set_QMAKESPEC
fi

if [ -z $QTDIR ]
then
  set_QTDIR
fi

if [ -z $KDEDIR ]
then
  set_KDEDIR
fi

if [ -z $G_PREFIX ]
then
  set_G_PREFIX
fi


kdialog --title "Environment variables" --yesno "The following settings have been collected:\nQMAKESPEC: $QMAKESPEC\nQTDIR: $QTDIR\nKDEDIR: $KDEDIR\nInstallation prefix: $G_PREFIX\nAre you happy with these settings? If not you can set each variable individually."

USE_AUTO_VALUE=$?

if [ ! $USE_AUTO_VALUE -eq 0 ]
then
  set_QMAKESPEC
  set_QTDIR
  set_KDEDIR
  set_G_PREFIX
fi


echo "Exporting variables"

if [ ! -z $QMAKESPEC ]
then
  export QMAKESPEC
fi

if [ ! -z $QTDIR ]
then
  export QTDIR
fi

if [ ! -z $KDEDIR ]
then
  export KDEDIR
fi

if [ ! -z $G_PREFIX ]
then
  export G_PREFIX
fi

echo "Disabling all components that were not selected"

# this is checked for in the src/src.pro qmake project file

if [ -z $INST_SERVER ]
then
  G_NO_SERVER="yes"
  export G_NO_SERVER
fi

if [ -z $INST_CLIENT ]
then
  G_NO_CLIENT="yes"
  export G_NO_CLIENT
fi

if [ -z $INST_GOD ]
then
  G_NO_GOD="yes"
  export G_NO_GOD
fi

if [ -z $INST_DEMO ]
then
  G_NO_DEMO="yes"
  export G_NO_DEMO
fi

result=`kdialog --title "Ready to Install" --yesno "All Information has been collected, the installation can now be performed.\nShould the G System be installed?"`

checkreturn $?


$PWD/installer G_INSTALL_OPEN_README &

echo "Installation in progress..."

./scripts/compile > ./build/compile.log 2>&1

if [ ! $? -eq 0 ]
then
  kdialog --title "Compilation failed!" --sorry "Compiling the G System source code failed! You will find the log in $PWD/build/compile.log\nIf you can't fix the problem yourself, please send an email with the log file attached to g-users@lists.g-system.at and include any relevant information like operating system including version."
  rm ./instrunningflag
  exit 3
fi

if [ ! -z $INST_DOCS ]
then

  doc_command="./scripts/makedocs -a all"

  if [ ! -z $INST_APIREF ]
  then
    doc_command="$doc_command api"
  fi
  
  `$doc_command > ./build/makedocs.log 2>&1`

else
  if [ ! -z $INST_APIREF ]
  then
    `./scripts/makedocs api > ./build/makedocs.log 2>&1`
  fi
fi

if [ ! $? -eq 0 ]
then
  kdialog --title "Documentation generation failed!" --sorry "Could not generate the documentation! You will find the log in $PWD/build/makedocs.log\nIf you can't fix the problem yourself, please send an email with the log file attached to g-users@lists.g-system.at and include any relevant information like operating system including version.\nInstallation will continue! But you won't have the docs available in the installation.\nThe documentation is also online on the G System homepage: http://www.g-system.at"
fi

# TODO improve target directory sanity checks

if test ! -d $G_PREFIX
then
  mkdir -p $G_PREFIX
fi

if test ! -w $G_PREFIX
then
  kdialog --title "Super User priviledges for installation" --msgbox "The installation into $G_PREFIX requires super user priviledges, please enter the root password in the following dialog.\nIf you want to cancel the password input, please use Ignore, NOT Cancel in the next dialog."
  
  checkreturn $?
  
  kdesu "$PWD/scripts/makeinstall"
  
  checkreturn $?

else

  ./scripts/makeinstall > ./build/makeinstall.log 2>&1
  
  if [ ! $? -eq 0 ]
  then
    kdialog --title "Installation failed!" --sorry "Failed to install the G System, make sure the desired installation directory is writable to you. You will find the log in $PWD/build/makeinstall.log\nIf you can't fix the problem yourself, please send an email with the log file attached to g-users@lists.g-system.at and include any relevant information like operating system including version."
    rm ./instrunningflag
    exit 5
  fi

fi

#kdialog --title "Client Configuration" --yesno "Installation successfully finished, do you want to create a configuration for the G Universe client now?"


#if [ $? -eq 0 ]
#then
#  if test -w /usr/local/etc -o -w /usr/local/etc/guniverseclient.xml
#  then
#    $G_PREFIX/bin/god /usr/local/etc/guniverseclient.xml
#  else
#    kdesu "$G_PREFIX/bin/god /usr/local/etc/guniverseclient.xml"
#  fi
#fi

if [ ! -z $INST_CLIENT ]
then
  export LD_LIBRARY_PATH=$G_PREFIX/lib
  $G_PREFIX/bin/god $HOME/.guniverseclient.xml
fi


if [ -r $HOME/.guniverseclient.xml ]
then
  kdialog --title "Installation finished" --msgbox "Installation of the G System into $G_PREFIX successfully completed.\nDepending on what you have installed you can now take a look at the demo $G_PREFIX/bin/gdemo, start the $G_PREFIX/bin/guniverseclient to connect to an existing universe or tweak some configurations with $G_PREFIX/bin/god.\nIt is recommended to join the G System mailing lists, see http://www.g-system.at/page_doc_maillist.php\nEnjoy!"
else
  kdialog --title "Installation finished" --msgbox "Installation of the G System into $G_PREFIX successfully completed.\nDepending on what you have installed you can now use $G_PREFIX/bin/god to configure the G Universe or take a look at the demo $G_PREFIX/bin/gdemo.\nAfter you have created a suitable configuration you can use $G_PREFIX/bin/guniverseclient with the configuration file as parameter to connect to the the G Universe.\nIt is recommended to join the G System mailing lists, see http://www.g-system.at/page_doc_maillist.php\nEnjoy!"
fi

rm ./instrunningflag
echo "Installation completed."


syntax highlighted by Code2HTML, v. 0.9.1