#!/bin/sh

if [[ $# > 0 ]]; then
  mask=$1
fi

cid=1

project_name=EC-GN-JA-PCF
mkdir ${project_name}

current_dir=$(pwd)

################################################################################################################
#Section 1
#Update SVN

ok=$(echo ${mask} | cut -c ${cid})

if [[ ${ok} != "0" ]]; then
  if [[ ! -d ${project_name}/.svn ]]; then
    rm -rf ${project_name}
    svn co https://svnpub.iter.org/codac/iter/codac/icdev/units/m-${project_name}/trunk/
    mv trunk ${project_name}
  fi

  cd ${project_name}
  svn up
  status=$?
  while [[ ${status} != 0 ]]; do
    svn cleanup
    svn up
    status=$?
  done  
fi
cid=$((cid+1))

cd ${current_dir}
################################################################################################################
#Section 2
#Get PLC data from SVN

ok=$(echo ${mask} | cut -c ${cid})

#if [[ ${ok} != "0" ]]; then
#  plc_proj_folder=src/main/resources/52HV05-PLC-7103
#  export_folder=${plc_proj_folder}/export
#  if [[ -d ${project_name}/${export_folder} ]]; then
#    rm -rf ${project_name}-IN/${export_folder}
#    cp -rf ${project_name}/${export_folder} ${project_name}-IN/${export_folder}
#  fi
#fi

cid=$((cid+1))

cd ${current_dir}

################################################################################################################
#Section 3
#SDD(-cool) Generation

ok=$(echo ${mask} | cut -c ${cid})

if [[ ${ok} != "0" ]]; then
#  cp -rf ${project_name}/src/main/resources tmp_resources
  rm -rf ${project_name}/*
  cp -rf ${project_name}-IN/* ${project_name}/
  cp -rf ${project_name}-IN/.[^.]* ${project_name}/
#  cp -rf tmp_resources/* ${project_name}/src/main/resources
#  rm -rf tmp_resources

  cd ${project_name,,}-sdd-in/SDD_IN
  ./sdd-generate.sh
fi
cid=$((cid+1))

cd ${current_dir}

################################################################################################################
#Section 4
#Post Patches

ok=$(echo ${mask} | cut -c ${cid})

#if [[ ${ok} != "0" ]]; then
#fi

cid=$((cid+1))

cd ${current_dir}

################################################################################################################
#Section 5
#ZIP generation

ok=$(echo ${mask} | cut -c ${cid})

if [[ ${ok} != "0" ]]; then
  cd ${current_dir}
  dateId=$(date +'%Y-%m-%d_%H-%M-%S_%N')
  zip -r ${project_name}-${dateId}.zip ${project_name} -x ${project_name}/.svn\* -x ${project_name}/src/main/boy/unified-control-lib\*
  nKeep=3
  nBackups=$(ls -t ${project_name}-*.zip | wc -l)
  if [[ ${nBackups} -gt ${nKeep} ]]; then
    nRemovals=$(( ${nBackups} - ${nKeep} ))
    toRemove=$(ls -t ${project_name}-*.zip | tail -n ${nRemovals})
    rm -f ${toRemove}
  fi

fi
cid=$((cid+1))
cd ${current_dir}

################################################################################################################
#Section 6
#SVN Sync
ok=$(echo ${mask} | cut -c ${cid})

if [[ ${ok} != "0" ]]; then
  
  cd ${project_name}
  mvn clean

  svn st | grep ^? | awk '{print " --force "$2}' | xargs svn add
  svn st | grep ^! | awk '{print " --force "$2}' | xargs svn rm

  svn ci -m "Generated on ${dateId}"
  cd ${current_dir}

fi
cid=$((cid+1))

cd ${current_dir}


################################################################################################################
#Section 7
#Project Compilation

ok=$(echo ${mask} | cut -c ${cid})

if [[ ${ok} != "0" ]]; then
  cd ${project_name}
  mvn clean package -O -Drelease=final
  cd ${current_dir}
  cp -rf ${project_name}/target/* target/
fi
