#!/bin/bash

# Part of the cosima cluster
# Check how many sims can be run on the machine

# Return:
# Positive: number of available simulation slots


commandhelp() {
  echo ""
  echo "dcosima-assemblefiles - determine all the files needed to simulate the given source file";
  echo "Copyright by Andreas Zoglauer"
  echo ""
  echo "Usage: dcosima-assemblefiles [options]";
  echo ""
  echo "Options:"
  echo "  --source=[filename]: The source file"
  echo "  --destination=[dirname]: The destination directory"
  echo "  --help: Show this help."
  echo ""
  echo "";
  echo "Example: "
  echo "  dcosima-assemblefiles --source=CrabOnly.source";
  echo "";
}


# Store command line as array
CMD=( "$@" )

# Check for help
for C in "${CMD[@]}"; do
  if [[ ${C} == *-h* ]]; then
    echo ""
    commandhelp
    exit 0
  fi
done


# Default options:
DESTINATION=""
SOURCE=""

# Overwrite default options with user options:
for C in "${CMD[@]}"; do
  if [[ ${C} == *-s*=* ]]; then
    SOURCE=`echo ${C} | awk -F"=" '{ print $2 }'`
    SOURCE=`readlink -f ${SOURCE}`
  elif [[ ${C} == *-d*=* ]]; then
    DESTINATION=`echo ${C} | awk -F"=" '{ print $2 }'`
    eval DESTINATION=${DESTINATION}
    #DESTINATION=$(readlink -f ${DESTINATION})
  elif [[ ${C} == *-h* ]]; then
    echo ""
    commandhelp
    exit 0
  else
    echo ""
    echo "ERROR: Unknown command line option: ${C}"
    echo "       See \"dcosima-assemblefiles --help\" for a list of options"
    exit 1
  fi
done


# Provide feed back and perform error checks:
echo ""
echo "Chosen options:"

if [ "${SOURCE}" == "" ]; then
  echo " "
  echo "ERROR: You need to give source file for simulations"
  exit 1
fi
if [[ ! -f ${SOURCE} ]]; then 
  echo " "
  echo "ERROR: The source file (\"${SOURCE}\") must exist"
  exit 1
fi
echo " * Source file: ${SOURCE}"

if [ "${DESTINATION}" == "" ]; then
  echo " "
  echo "ERROR: You need to give a destination directory for your files"
  exit 1
fi
if [[ ! -d ${DESTINATION} ]]; then 
  echo " "
  echo "ERROR: The destination directory (\"${DESTINATION}\") must exist"
  exit 1
fi
if [ "$(ls -A ${DESTINATION})" != "" ]; then
  echo " "
  echo "ERROR: The destination directory (\"${DESTINATION}\") must be empty"
  exit 1
fi
echo " * Destination directory: ${DESTINATION}"


FILELIST=""
TOCHECK=( ${SOURCE} )

ORIGINALS=( )
REPLACEMENTS=( )

while true; do

  # Cleanup the existing data
  for (( m=0; m<=$(( ${#TOCHECK[*]} -1 )); m++ )); do
    if [ "${TOCHECK[$m]}" != "" ]; then
      eval F="${TOCHECK[$m]}"
      #echo "First ${F}"
      F=$(echo "${F}" | dos2unix)
      #echo "Second ${F}"
      F=$(echo "${F}" | sed 's/\t/ /g')

      TOCHECK[$m]="${F}"
    fi
  done 
  
  # Check if there are still file to be checked
  FOUND="false"
  
  for (( m=0; m<=$(( ${#TOCHECK[*]} -1 )); m++ )); do
    if [ "${TOCHECK[$m]}" != "" ]; then
      FOUND="true";
      break;
    fi
  done
  if [ "${FOUND}" == "false" ]; then break; fi

  NEWONES=""
  for (( m=0; m<=$(( ${#TOCHECK[*]} -1 )); m++ )); do
    if [ "${TOCHECK[$m]}" != "" ]; then
      F="${TOCHECK[$m]}"

      if [[ ! -f ${F} ]]; then
        echo "ERROR: Unable to find file: \"${F}\""
        exit 1
      fi

      # Check for all the files
      #echo "Analyzing: ${F}"
      VERYNEWONES=()
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep "^Include" | awk '{print $2}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep "^Geometry" | awk  '{print $2}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep FarFieldFileZenithDependent | awk '{print $3}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep FarFieldNormalizedEnergyBeamFluxFunction | awk '{print $3}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep RadialProfileBeam | awk '{print $9}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep MapProfileBeam | awk '{print $9}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep ActivationSources | awk '{print $2}'`)
      VERYNEWONES+=(`cat ${F} | grep -v "^#" | grep FarFieldTransmissionProbability | awk '{print $2}'`)
      if [[ `cat ${F} | grep -v "^#" | grep "\ File\ "` != "" ]]; then
        OLDIFS=${IFS}
        IFS=$'\n'
        LINES=(`cat ${F} | grep -v "^#" | grep "\ File\ "`)
        IFS=${OLDIFS}
        #echo "LINES:"
        #printf '%s\n' "${LINES[@]}"
        for (( i=0; i<=$(( ${#LINES[@]} - 1 )); i++ )); do
          L=${LINES[$i]}
          #echo "Line ${L}"
          if [[ `echo ${L} | grep "\ File\ " | awk '{print $2}'` == File ]]; then 
            #echo "Files 2 : $(echo ${L} | grep "\ File\ " | awk '{print $2}')"
            VERYNEWONES+=(`echo ${L} | grep "\ File\ " | awk '{print $3}'`)
          elif [[ `echo ${L} | grep "\ File\ " | grep ".Orientation"` != "" ]]; then
            VERYNEWONES+=(`echo ${L} | grep "\ File\ " | awk '{print $5}'`)
          fi
        done
      fi
      
      #echo "Very new: ${VERYNEWONES[@]}"
      
      DIR=`dirname ${F}`
      for N in ${VERYNEWONES[@]}; do
        ORIG=${N}
        N="${N//$\(MEGALIB\)/$\{MEGALIB\}}"
        N="${N//$\(NUCLEARIZER\)/$\{NUCLEARIZER\}}"
        eval N=${N}
        if [ "$?" != "0" ]; then exit 1; fi  # Above line can fail, if there are uncommon paths to expand 
        eval N=${N}
        if [[ ${N} != /* ]]; then
          N="${DIR}/${N}"
        fi
        
        if [[ ${FILELIST} == *${N}* ]]; then
          continue;
        fi
        
        ORIGINALS=( ${ORIGINALS[@]} ${ORIG} ) 
        REPLACEMENTS=( ${REPLACEMENTS[@]} `basename ${N}` )
        
        NEWONES="${NEWONES} ${N}"        
      done
    fi
  done
    
  #echo "New: ${NEWONES}"
  #echo "To check: ${TOCHECK[@]}"
  
  FILELIST="${FILELIST} ${TOCHECK[@]} "
  eval TOCHECK=( ${NEWONES} )
  
done

# Copy files to destination
cd ${DESTINATION}
for F in ${FILELIST}; do
  cp ${F} .
done

# Sanitize the files
for F in `ls`; do
  for (( m=0; m<=$(( ${#ORIGINALS[*]} -1 )); m++ )); do
    mv ${F} ${F}~
    sed "s|${ORIGINALS[$m]}|${REPLACEMENTS[$m]}|g" ${F}~ > ${F}
    rm ${F}~
  done
done


exit 0;
