#!/bin/bash

usage() {
  echo "Run as: ./ciao-configure [optional: install_top]"
  echo "Where: "
  echo "    [optional: install_top] can be used to fix the install location"
  echo "       references to elsewhere."
  echo "    For example: You can install into /System/Volumes and fix the scripts to"
  echo "       think they are in /proj (for mounting purposes)."
}

LIST="ciao.csh  ciao_setup.sh  ciao.sh  setup_obsvis.csh  setup_obsvis.sh  setup_sherpa.csh  setup_sherpa.sh  setup_tools.csh  setup_tools.sh"

if [ ! -z ${1} ] ; then
   case "${1}"
      in
      "-h"|"--help" )
         usage
         exit 0
         ;;
      * )
         install_top="${1}"
         ;;
   esac
fi 

BIN_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
ciao_path="$( dirname ${BIN_DIR} )"
scripts_path="${ciao_path}/init"
install_top="${install_top:-${ciao_path}}"
echo "Fixing top to: ${install_top}"

#Setings from build config
OSX_ONLY="#"
SET_LD_LIBRARY_PATH="LD_LIBRARY_PATH"
pyver="3.11"
imagerdir=$install_top/imager

mkdir -p ${ciao_path}/binexe

#Move any previous versions and copy in the new scripts
for instfile in $(ls ${scripts_path}) ; do
   #Move any matching files in the install out of the way
   if [ -f ${BIN_DIR}/${instfile} ] ; then
      if [ "${instfile:0:6}" == "python" ] && [ "$(file ${BIN_DIR}/${instfile} | grep "ASCII")x" == "x" ] ; then
         mv ${BIN_DIR}/${instfile} ${ciao_path}/binexe/.
      elif [ "${instfile:0:7}" == "ipython" ] && [ "$(grep "import" ${BIN_DIR}/${instfile} 2>/dev/null)x" != "x" ] ; then
         mv ${BIN_DIR}/${instfile} ${ciao_path}/binexe/${instfile}.orig
         cat ${ciao_path}/binexe/${instfile}.orig | sed "s|${BIN_DIR}/python${pyver}|/usr/bin/env python|g" 1>${ciao_path}/binexe/${instfile}
         chmod 775 ${BIN_DIR}/../binexe/ipython
      else
         mv ${BIN_DIR}/${instfile} ${BIN_DIR}/${instfile}.orig
      fi
   fi
   cp -p ${scripts_path}/${instfile} ${BIN_DIR}/.
done

#Move CIAO binaries and wrap
binlists="$(ls ${scripts_path}/*_bin.txt)"
if [ "${?}" == 0 ] ; then
   for binlist in ${binlists} ; do
      cat ${binlist} |
          while read FILE dummy ; do
             if [ -f ${BIN_DIR}/${FILE} ] ; then
                #Skip if already wrapped (always remove incase this is an updated install)
                if [ "$(grep "ciao_setup" ${BIN_DIR}/${FILE} 2>/dev/null)x" == "x" ] ; then
                   rm -f ${ciao_path}/binexe/${FILE}
                   mv ${BIN_DIR}/${FILE} ${ciao_path}/binexe/${FILE}
                   #Install wrapper
                   cp ${scripts_path}/runciao ${BIN_DIR}/${FILE}
                   chmod 755 ${BIN_DIR}/${FILE}
                fi
             else
                echo "ERROR: Could not find - ${BIN_DIR}/${FILE}"
                exit 1
             fi
          done
   done
fi

#Fix magic cookies
cd ${BIN_DIR}

for fixfile in ${LIST} ; do
   chmod u+w $fixfile
   sed -i.orig "s|\@install_top\@|$install_top|g" ${fixfile}
   sed -i.orig "s|\@OSX_ONLY\@|$OSX_ONLY|g" ${fixfile}
   sed -i.orig "s|\@SET_LD_LIBRARY_PATH\@|$SET_LD_LIBRARY_PATH|g" ${fixfile}
   sed -i.orig "s|\@imagerdir\@|$imagerdir|g" ${fixfile}
   sed -i.orig "s|\@pyver\@|$pyver|g" ${fixfile}
done

#Check sed
# Then, fix all #! references to python in bin
echo "checking sed" 1>tmp-test-sed.txt
sed -i'' -e "s:checking:checked:g" tmp-test-sed.txt
for pypath_dirs in bin test/smoke/bin ; do
   cd ${ciao_path}/${pypath_dirs}
   if [ -f tmp-test-sed.txt-e ] ; then
      sed_space="true"
      find . -type f -exec file {} + | \
         awk -F: '/ASCII text/ || /shell script/ || /script text/ {print $1}' | \
         tr '\n' '\0' | xargs -0 sed -i '' -e "s:#\!${BIN_DIR}/python:#\!/usr/bin/env python:g"
   else
      sed_space="false"
      find . -type f -exec file {} + | \
         awk -F: '/ASCII text/ || /shell script/ || /script text/ {print $1}' | \
         tr '\n' '\0' | xargs -0 sed -i'' -e "s:#\!${BIN_DIR}/python:#\!/usr/bin/env python:g"
   fi
done
cd ${BIN_DIR}
rm tmp-test-sed.txt*

if [ "${install_top}" != "${ciao_path}" ] ; then
    #Fix other Conda files
    cd ${ciao_path}
    if [ "${?}" != "0" ] ; then
       exit 1
    fi
    if [ "${sed_space}" == "true" ] ; then
       find . \( -type d -name CALDB -prune \) -o \( -type d -name spectral -prune \) -o -type f -exec file {} + | \
          awk -F: '/ASCII text/ || /shell script/ || /script text/ || /libtool library file/ {print $1}' | \
          tr '\n' '\0' | xargs -0 sed -i '' -e "s:${ciao_path}:${install_top}:g"
    else
       find . \( -type d -name CALDB -prune \) -o \( -type d -name spectral -prune \) -o -type f -exec file {} + | \
          awk -F: '/ASCII text/ || /shell script/ || /script text/ || /libtool library file/ {print $1}' | \
          tr '\n' '\0' | xargs -0 sed -i'' -e "s:${ciao_path}:${install_top}:g"
    fi
fi

echo "DONE!"
