#!/bin/bash

# startup tests
if [ "$(getconf LONG_BIT)" != "64" ]; then
  echo 'OS is not 64bit'
  exit 1
fi

if [ "$(id -u)" -eq "0" ];then 
  echo 'Please run this script as a normal user (not root)'
  exit 1
fi

if [ "${DISPLAY}" == "" ]; then
  echo 'A running X-session is required'
  exit 1
fi

#set default parameters:
DIR_GTB="${HOME}/GuidosToolbox"

# already set up?
GTBVF="$DIR_GTB/idl/save_add/guidos_progs/GTBversion.txt"
if [ ! -e "$GTBVF" ];then 
  mkdir -p "$DIR_GTB"
  cp -fr /opt/GTB/* "$DIR_GTB/"
else
 # file exist, verify if uptodate
 current=3.310
 gtbv=`sed -n '1p' "$GTBVF"`
 gtbv=`echo "${gtbv//[!.0-9]/}"`
 update=`echo "$current > $gtbv" | bc -l | grep -q 1 && echo yes || echo no`
 if [ "$update" == "yes" ];then
  cp -fr /opt/GTB/* "$DIR_GTB/"
 fi
fi

# Run the application
exec "${DIR_GTB}/startGTB.sh" 

