#! /bin/bash
echo " "
echo "This is the script to install the latest IDL patch from SPDF onto your Mac."
echo "You may need to enter the administrator account's password."

VERSION="3.8.0"
ftp="https://spdf.gsfc.nasa.gov/pub/software/cdf/dist/cdf38_0/idl"
IDL_SO64="$ftp/macosx/x86_64/idl_cdf.so"
IDL_SO32="$ftp/macosx/i386/idl_cdf.so"
IDL_DLM="$ftp/idl_cdf.dlm"
LEAPS="https://cdf.gsfc.nasa.gov/html/CDFLeapSeconds.txt"
SHOWVERS="$ftp/showcdfversion.pro"
SHOWVERS2="$ftp/showcdfversion2.pro"
if [ -x "$(command -v curl)" ]; then
  GET="curl -O "
elif [ -x "$(command -v wget)" ]; then
  GET="wget "
else
  echo 'Error: require curl or wget' >&2
  exit 1;
fi

echo " "
echo "This is the list of currently available IDL version(s) installed on your Mac:"
echo " "
I386=""
options=( $(mdfind 'kMDItemContentType == public.folder && kMDItemFSName == "bin.darwin*"[cd]'|egrep "idl|envi" | egrep -v "idlde" | egrep -v "license" | egrep -v "ppc") )
PS3='Select the number, shown on the left of the above list, for the IDL/ENVI to be replaced by the CDF patch: '
select opt in "${options[@]}" ; do
  if (( REPLY <= 0 || REPLY >= (1 + ${#options[@]}) )) ; then
    echo "$REPLY: an invalid number"
    exit 1;
  fi
  echo  "You picked ${opt} which is number $REPLY"
  IDL_DM=$IDL_DLM
  if [[ ${opt} =~ x86_64 ]]; then
      IDL_SO=$IDL_SO64
  elif [[ ${opt} =~ i386 ]]; then
      IDL_SO=$IDL_SO32
      I386="-32"
  else
     echo 'Error: require IDL/ENVI bin path for x86_64 or i386' >&2;
     exit 1
  fi
  today=`date '+%Y%m%d'`
  arr=(${opt//// })
  for fn in ${arr[@]}; do
      if [[ ${fn} =~ idl.*[0-9]+ ]]; then
        selected=$fn
        break
      fi
  done
  TEMPDIR=$(mktemp -d ~/idl_save_${selected}_${today}.XXXXXXXX)
  echo "Saving old and new files to ${TEMPDIR}"
  cd ${TEMPDIR}
  echo "$GET ${IDL_SO}"
  $GET ${IDL_SO}
  if [ $? -ne 0 ]; then
       echo "Error: failed $GET ${IDL_SO}" >&2
       exit 1
  fi
  echo " "
  echo "$GET ${IDL_DM}"
  $GET ${IDL_DM}
  if [ $? -ne 0 ]; then
       echo "Error: failed $GET ${IDL_DM}" >&2
       exit 1
  fi
  echo " "
  echo "$GET ${LEAPS}"
  $GET ${LEAPS}
  if [ $? -ne 0 ]; then
       echo "Error: failed $GET ${LEAPS}" >&2
       exit 1
  fi
  echo " "
  echo "$GET ${SHOWVERS}"
  $GET ${SHOWVERS}
  if [ $? -ne 0 ]; then 
       echo "Error: failed $GET ${SHOWVERS}" >&2
       exit 1
  fi
  echo " "
  echo "$GET ${SHOWVERS2}"
  $GET ${SHOWVERS2}
  if [ $? -ne 0 ]; then 
       echo "Error: failed $GET ${SHOWVERS2}" >&2
       exit 1
  fi
  echo " "
  date_time=`date +%Y-%m-%d`
  cd ${opt}
  if [[ ${opt} =~ "/Applications/" ]]; then
    echo " "
    echo "Enter administrator account password: "
    sudo xattr -d com.apple.quarantine *
    if [ ! -f idl_cdf.so-orig-$date_time ]; then
      sudo mv -v idl_cdf.so idl_cdf.so-orig-$date_time
      if [ $? -ne 0 ]; then
           exit 1
      fi
      sudo mv -v idl_cdf.dlm idl_cdf.dlm-orig-$date_time
      if [ $? -ne 0 ]; then
           exit 1
      fi
    fi
    sudo mv -v ${TEMPDIR}/idl_cdf.so idl_cdf.so
    if [ $? -ne 0 ]; then
         exit 1
    fi
    sudo mv -v ${TEMPDIR}/idl_cdf.dlm idl_cdf.dlm
    if [ $? -ne 0 ]; then
         exit 1
    fi
  else
    if [ ! -f idl_cdf.so-orig-$date_time ]; then
      mv -v idl_cdf.so idl_cdf.so-orig-$date_time
      mv -v idl_cdf.dlm idl_cdf.dlm-orig-$date_time
    fi
    mv -v ${TEMPDIR}/idl_cdf.so idl_cdf.so
    mv -v ${TEMPDIR}/idl_cdf.dlm idl_cdf.dlm
  fi
  cd ${TEMPDIR}
  echo " "
  echo "Testing the installation... version: $VERSION"
  echo " "
  if [ -d ${opt}/../../resource/xprinter ]; then
    export IDL_DIR=${opt}/../.. && export DYLD_LIBRARY_PATH=.:${opt} && export XPPATH=${opt}/../../resource/xprinter && echo ".run showcdfversion2" | ${opt}/idl ${I386}
  else
    export IDL_DIR=${opt}/../.. && export DYLD_LIBRARY_PATH=.:${opt} && echo ".run showcdfversion2" | ${opt}/idl ${I386}
  fi
  echo " "
  echo "The patch (for .so and .dlm) is installed in ${opt}"
  echo " "
  echo "NOTE: The original version (.dlm and .so) there is saved and renamed"
  echo "      with \"-orig-$date_time\" appended."
  echo " "
  rm showcdfversion.pro
  rm showcdfversion2.pro
  break # end of selected bin dir
done

