#!/bin/sh
# ==============================================================================
#
# Name:       setup.sh
#
# Purpose:    Setup utility for the Bernese GNSS Software (UNIX version).
#
# Authors:    L.Mervart
#
# Created:    13-Aug-2003
#
# Changes:    16-Oct-2003 RD: Use "make_dir" instead of "mkdir" in all cases
#                             Prompt after typing the installation directory
#                             Use "--path" for configure.pm
#             11-Jan-2012 SL: Adapted for V5.2, do not create empty dirs
#             12-Jan-2012 SL: Error handling
#             01-Feb-2012 SL: EXAMPLE replaces old example campaign files
#             11-Jun-2012 SL: Use gzip -d instead of uncompress
#             23-Aug-2012 SL: File extension taz changed to tgz
#             13-Sep-2012 SL: DATAPOOL and SAVEDISK added, EXAMPLE
#             26-Sep-2012 SL: Handle OS specific files
#             14-Jul-2022 AV: Adapt file structure
#             02-Aug-2022 AV: Adapt capaign directory (including version)
#
# Copyright:  Astronomical Institute
#             University of Bern
#             Switzerland
#
# ==============================================================================

# Default values
# --------------
VERSION="54";
instDirDefault="${HOME}/BERN$VERSION";
thisPath=`dirname $0`;
thisPath=`(cd $thisPath; /bin/pwd)`;

# Useful routines
# ---------------
check_program () {
  PROGRAM=`which $1`;
  if [ ! -x $PROGRAM ]; then
    echo -e "\n *** `basename $0`: Cannot execute program $1\n";
    exit;
  fi
}

make_dir () {
  if [ ! -d $1 ]; then
    mkdir -p $1;
  fi
  if [ ! -d $1 ]; then
    echo -e "\n *** `basename $0`: Cannot create directory $1\n";
    exit;
  fi
}

# Check the required programs
# ---------------------------
check_program tar;
check_program gzip;
check_program perl;

# Start script
# ------------
echo;
echo " ***************************";
echo " *  Bernese GNSS Software  *";
echo " *       Installation      *";
echo " *  (UNIX/Linux platform)  *";
echo " ***************************";
echo;

# Ask about the Software directory
# --------------------------------
echo -e "\nFull path to the software directory [ $instDirDefault ]:";

read instDir;

if [ "$instDir" = "" ]; then
  instDir=$instDirDefault;
fi

echo -e "\nThe software will be installed in $instDir";

if [ -d $instDir ]; then
  echo -e "\n ### `basename $0`: Directory already exists. Move it away!";
fi

echo -e "\nPress the return key to continue or Ctrl-C to abort";
read anyString;

# Create the software directory and copy archives
# -----------------------------------------------
make_dir $instDir;

file="BERN$VERSION";
if [ ! -f "$thisPath/$file.tgz" ]; then
  echo -e "\n *** `basename $0`: $file.tgz is not available in $thisPath\n";
  exit;
fi
CMD="cp $thisPath/$file.tgz $instDir/$file.tar.gz";
echo $CMD; $CMD;

for file in CAMPAIGN54 DATAPOOL SAVEDISK ICONS; do
  if [ ! -f "$thisPath/$file.tgz" ]; then
    echo -e " ### `basename $0`: $file.tgz is not available in $thisPath";
  else
    CMD="cp $thisPath/$file.tgz $instDir/$file.tgz";
    echo $CMD; $CMD;
  fi
done
echo;

# Extract BERN archive
# --------------------
cd $instDir;
CMD="gzip -d BERN$VERSION.tar.gz";
echo $CMD; $CMD;
CMD="tar -xvf BERN$VERSION.tar";
echo $CMD; $CMD;
rm BERN$VERSION.tar;
echo;

for file in *.tgz; do
  CMD="mv $file EXAMPLE";
  echo $CMD; $CMD;
done
echo;

# Handle OS specific files
# ------------------------
for file in INSTALL.TXT UPDATE.TXT; do
  cp SUPGUI/DOC/README_${file}_UNIX SUPGUI/DOC/README_$file;
  rm SUPGUI/DOC/README_${file}_*;
done
cp SCRIPT/EXE/Makefile.template_UNIX SCRIPT/EXE/Makefile.template;
rm SCRIPT/EXE/Makefile.template_*;
rm SUPGUI/PAN/WIN32.CPU;

# Start the Bernese configuration utility
# ---------------------------------------
if [ -f "$instDir/SCRIPT/EXE/configure.pm" ]; then
  echo " The Bernese GNSS Software has been successfully installed";
  echo " in $instDir";
else
  echo " There is a problem with the installation!";
  exit;
fi

defaultPERL=`which perl`;
echo -e "\nPerl program to be used [ $defaultPERL ]:";

read PERL;

if [ "$PERL" == "" ]; then
  PERL=$defaultPERL;
fi
echo;

echo -e "\nQT library to be used [ $QTBERN ]:";

read QTDEF;

if [ "$QTDEF" ] == "" ]; then
   QTDEF=$QTBERN
fi			  

export OS_NAME="LINUX"
$PERL $instDir/SCRIPT/EXE/configure.pm --init --qtBern=$QTDEF --perl=$PERL --path=$instDir;

# ==============================================================================
