#!/bin/bash
#
# BDSIM configuration script
# I. Agapov 21.01.05


#default settings
Makefile=Makefile
success="1"
fatal="0"

echo
echo "###########################################"
echo "#                  BDSIM                  #"
echo "###########################################"

echo
echo " This is the BDSIM configuration script...."

echo " Checking your system ..."

# Determine if libxml exist...
# we will uyse at the end of the file
#xmltest=`which xml2-config`
#if [ ! -z "$xmltest" ]; then
#printf "You have libxml installed:\n"
#printf "Do you want to enable the GDML geometry description format ?\n"
#read -p "[y/n]" value
#if test "$value" == "y"; then
#    USE_XML="USE_XML"
#	         echo "           BDSIM is able to read GDML format...."
#fi
#fi


# Determine G4 installation directory

G4_install=$G4INSTALL

printf "\n\n What is your Geant4 installation directory?\n"


read -p "[$G4_install]" value


if test "$value" == ""; then
	value=$G4_install
fi


if [[ ! -d "$value" ]]; then
    echo
    echo "Error!!! : directory $value not found, trying default"
    
    if [[ -d $G4_install ]]; then
	value=$G4_install
	echo "           Ok " 
    else
	echo "           the compilation will certainly fail"
	echo "           install Geant4 first"
	echo
	
	fatal="1"
    fi

fi

G4_install=$value



#determine G4 version --- to be fixed x.x.x
				      # x x

printf "\n \n Determiming Geant4 version ......"

#G4_version=${G4_install##*/geant}
#G4_version=${G4_version%%.p*}

G4_version=$(echo $G4_install | grep -o '[4].[0-9]\(.[0-9]\)*' | cut -d . -f 2)
G4_minor_version=$(echo $G4_install | grep -o '[4].[0-9]\(.[0-9]\)*' | cut -d . -f 3)
#G4_version=$(echo $G4_install | grep -o '[4].[0-9]\(.[0-9]\)*' | sed "s/4.//; s/.[0-9]//")

if test "$G4_version" != ""; then
   if test "$G4_minor_version" != "" & test $G4_version=9; then
     printf " 4.$G4_version.$G4_minor_version\n"
   else
     printf " 4.$G4_version\n"
   fi
else
   G4_version="0"
   printf " can't determine\n"
#   success="0"
   read -p "Choose version 4.[0-9]" value
   G4_version=$value
   if test $G4_version = 9; then
     read -p "Choose minor version 4.9.[0-9]" minor_value
     G4_minor_version=$minor_value
  fi
fi

#printf " change the G4VERSION variables \n in the Makefile if I'm  wrong \n"


# Determine for CLHEP version
CLHEP_majorversion=$(find $CLHEP_BASE_DIR/ -name \*CLHEP\* | grep -o '[12].[0-9]\(.[0-9]\)*'| sed "s/.[0-9]//; s/.[0-9]//; s/.[0-9]//" | head -n 1)

CLHEP_minorversion=$(find $CLHEP_BASE_DIR/ -name \*CLHEP\* | grep -o '[12].[0-9]\(.[0-9]\)*'| sed "s/[12].//; s/.[0-9]//; s/.[0-9]//" | head -n 1)

if test "$CLHEP_majorversion" == "2"; then
   CLHEP_version="9"
else
if test "$CLHEP_majorversion" == "1"; then
   CLHEP_version="$CLHEP_minorversion"
else
   CLHEP_version="0"
fi
fi
printf " Your CLHEP version is $CLHEP_majorversion.$CLHEP_minorversion ???\n"

read -p "[y/n]" value

if test "$value" == "n"; then
   printf "\n \n Enter the CLHEP version \n"
   read -p "[x.x.x.x]" value
   CLHEP_majorversion=$(echo $value | grep -o '[12].[0-9]\(.[0-9]\)*' | sed "s/.[0-9]//; s/.[0-9]//; s/.[0-9]//")
   CLHEP_minorversion=$(echo $value | grep -o '[12].[0-9]\(.[0-9]\)*' | sed "s/[12].//; s/.[0-9]//; s/.[0-9]//")

   if test "$CLHEP_majorversion" == "2"; then
     CLHEP_version="9"
   else
   if test "$CLHEP_majorversion" == "1"; then
     CLHEP_version="$CLHEP_minorversion"
   else
     CLHEP_version="0"
   fi
   fi
   printf " Your CLHEP version is $CLHEP_version\n"
fi
		

# Determine CERN Root installation directory

USE_ROOT="n"

printf "\n\n Do you want to use Root output (root has to be installed) ?\n"

read -p "[n/y]" value

if test "$value" == "y"; then
    USE_ROOT="-DUSE_ROOT"
    echo "           using Root output driver...."

ROOT_sys="$ROOTSYS"

printf "\n\n What is your ROOT installation directory?\n"


read -p "[$ROOT_sys]" value


if test "$value" == ""; then
	value=$ROOT_sys
fi

if [[ ! -d $value ]]; then
    echo
    echo "Error!!! : directory $value not found, trying default"
    
    if [[ -d $ROOT_sys ]]; then
	value=$ROOT_sys
	echo "           Ok " 
    else
	echo "           the compilation will certainly fail"
	echo "           try fixing the ROOTSYS environment variable"
	echo
	
	fatal="1"
    fi
   
fi

ROOT_sys=$value


printf "\n Checking for root-config ......"

if test -x $ROOT_sys/bin/root-config ; then
   printf " OK\n"
else
   printf " Failed\n"
   success="0"
fi

else
    echo "           not using Root output driver...."
    USE_ROOT=""
fi

# Ask for binary location

BIN_dir=$(pwd)

printf "\n\n Where do you want your binary file to be?\n"


read -p "[$BIN_dir]" value


if test "$value" == ""; then
	value=$BIN_dir
fi

if [[ ! -d $value ]]; then
	BIN_dir=$(pwd)
	echo
	echo "Warning  : directory $value not found, trying default"
	echo "           Using default value"
	echo "           $BIN_dir"
	echo
else
	BIN_dir=$value
fi

#test for Leopard
if test "x$G4SYSTEM" = "xDarwin-g++"; then
   if [ `sw_vers | sed -n 's/ProductVersion://p' | cut -d . -f 2` = 5 ]; then
      opengllib='$(OGLHOME)/lib/libGLU.dylib $(OGLHOME)/lib/libGL.dylib /System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib'
   fi
fi

# generate Makefile

if test "$fatal" != "1"; then
printf "\n Writing Makefile ... \n \n"

echo > $Makefile 
#echo " ###########################################  " > $Makefile
echo "#   Makefile for BDSIM                       " >> $Makefile
echo "#   Generated automatically with $0          " >> $Makefile
echo "#   by  $USER                                " >> $Makefile
echo "#   on $(date)                               " >> $Makefile
#echo "#                                            " >> $Makefile
#echo " ###########################################  " >> $Makefile
echo >> $Makefile
echo >> $Makefile

if [ ! -z "$xmltest" ]; then
cat Makefile.in  | sed "
s%@G4INSTALL@%$G4INSTALL%g;
s%@ROOTSYS@%$ROOT_sys%g;
s%@BINDIR@%$BIN_dir%g;
s%@G4VERSION@%$G4_version%g;
s%@G4MINORVERSION@%$G4_minor_version%g;
s%@CLHEP_VERSION@%$CLHEP_version%g;
s%@USE_ROOT@%$USE_ROOT%g;
s%@OGLLIBS@%$opengllib%g;
" >> $Makefile
else
cat Makefile.in  | sed "
s%@G4INSTALL@%$G4INSTALL%g;
s%@ROOTSYS@%$ROOT_sys%g;
s%@BINDIR@%$BIN_dir%g;
s%@G4VERSION@%$G4_version%g;
s%@G4MINORVERSION@%$G4_minor_version%g;
s%@CLHEP_VERSION@%$CLHEP_version%g;
s%@USE_ROOT@%$USE_ROOT%g;
s%@OGLLIBS@%$opengllib%g;
" >> $Makefile
fi

fi


if test "$fatal" == "1" ; then
    echo "I found some errors, fix them first. Makefile not written" 
    exit 1
fi

if test "$success" == "1" ; then
    echo " Configuration successful. Now type "
else
    printf " There were problems in your configuration,\n consider fixing them if the compilation fails\n"
    echo " To compile "
fi

echo
echo "   make"
#echo "   make install"
echo
