#!/bin/bash

#================================================================================
# deployScripts.bash - updates github repos for individual labos
# indend to push scripts from [DiD-scripts](https://gitlab.hevs.ch/course/did/did-scripts.git)
# Example usage 1:  ./Scripts/deployScripts.bash -v -p synd-did-labs -r https://github.com/hei-synd-did/did-labs.git
# Example usage 2:  ./Scripts/deployScripts.bash -v -p ete-did-labs -r https://github.com/hei-ete-did/did-labs.git
# Example usage 3:  ./Scripts/deployScripts.bash -v -p isc-did-labs -r https://github.com/hei-isc-did/did-labs.git
# Example usage 4:  ./Scripts/deployScripts.bash -v -p did-chrono -r https://github.com/hei-synd-did/did-chrono.git
# Example usage 5:  ./Scripts/deployScripts.bash -v -p did-cursor -r https://github.com/hei-synd-did/did-cursor.git
# Example usage 6:  ./Scripts/deployScripts.bash -v -p did-inverter -r https://github.com/hei-ete-did/did-inverter.git
# Example usage 7:  ./Scripts/deployScripts.bash -v -p did-synchro -r https://github.com/hei-ete-did/did-synchro.git
# Example usage 8:  ./Scripts/deployScripts.bash -v -p did-kart-ebs3 -r https://github.com/hei-synd-did/did-kart-ebs3.git
# Example usage 9:  ./Scripts/deployScripts.bash -v -p did-display -r https://github.com/hei-isc-did/did-display.git
# Example usage 10: ./Scripts/deployScripts.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git
# Example usage 11: ./Scripts/deployScripts.bash -v -p car-labs -r https://github.com/hei-isc-car/car-labs.git -d heirv32_sc
# Example usage 12: ./Scripts/deployScripts.bash -v -p car-heirv -r https://github.com/hei-isc-car/car-heirv.git

base_directory="$(dirname "$(readlink -f "$0")")"
pushd $base_directory
base_directory="$base_directory"

SEPARATOR='--------------------------------------------------------------------------------'
INDENT='  '
DATE=`date '+%Y-%m-%d %H:%M:%S'`

echo "$SEPARATOR"
echo "-- ${0##*/} Started!"
echo ""

#-------------------------------------------------------------------------------
# Parse command line options
#
project='did-labs'
repo='https://github.com/hei-synd-did/did-labs.git'
destdir=''

usage='Usage: deployScripts.bash [-p projectName] [-r repourl] [-d destdir] [-v] [-h]'
while getopts 'p:r:d:vh' options; do
  case $options in
    p ) project=$OPTARG;;
    r ) repo=$OPTARG;;
    d ) destdir=$OPTARG;;
    v ) verbose=1;;
    h ) echo -e $usage
          exit 1;;
    * ) echo -e $usage
          exit 1;;
  esac
done

#-------------------------------------------------------------------------------
# Display info
#
if [ -n "$verbose" ] ; then
  echo "$SEPARATOR"
  echo "-- $DATE: Deploy Scripts for Students"
  echo "${INDENT}for $project"
  echo "${INDENT}to $repo"
  echo ""
fi

#-------------------------------------------------------------------------------
# Clone student repo
#
# Create a tmp subdirectory if it doesn't exist
echo "Create tmp folder"
mkdir -p tmp
cd tmp

# Get repo
echo "Clone student repo $project"
# Add login and access token to url
repo_access=$(echo $repo | sed 's/https\?:\/\///')
github_username=tschinz
github_accesstoken=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
repo_access="https://$github_username:$github_accesstoken@${repo_access}"
git clone $repo_access
if [ "$project" == "synd-did-labs" ]; then
  cd did-labs
elif [ "$project" == "ete-did-labs" ]; then
  cd did-labs
elif [ "$project" == "isc-did-labs" ]; then
  cd did-labs
else
  cd $project
fi

library_source=`realpath "./../.."`

# DiD Kart (EBS2 version) has a different project structure
if [ "$project" == "did-kart-ebs2" ]; then
  # Copy needed libraries per project
  mkdir -p "01-StepperMotor/Scripts"
  library_dest=`realpath "./01-StepperMotor/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_libero.pl"    "$library_dest/"
  cp -arf "$library_source/start_libero.pl"     "$library_dest/"


  # Copy needed libraries per project
  mkdir -p "02-DcMotor/Scripts"
  library_dest=`realpath "./02-DcMotor/Scripts"`
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_libero.pl"    "$library_dest/"
  cp -arf "$library_source/start_libero.pl"     "$library_dest/"

  # Copy needed libraries per project
  mkdir -p "03-Sensors/Scripts"
  library_dest=`realpath "./03-Sensors/Scripts"`
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_libero.pl"    "$library_dest/"
  cp -arf "$library_source/start_libero.pl"     "$library_dest/"

  # Copy needed libraries per project
  mkdir -p "04-Controller/Scripts"
  library_dest=`realpath "./04-Controller/Scripts"`
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_libero.pl"    "$library_dest/"
  cp -arf "$library_source/start_libero.pl"     "$library_dest/"


# SEm Labs has also a different project structure
elif [ "$project" == "sem-labs" ]; then
  # Copy needed libraries per project
  mkdir -p "01-WaveformGenerator/Scripts"
  library_dest=`realpath "./01-WaveformGenerator/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

  mkdir -p "02-SplineInterpolator/Scripts"
  library_dest=`realpath "./02-SplineInterpolator/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

  mkdir -p "03-DigitalToAnalogConverter/Scripts"
  library_dest=`realpath "./03-DigitalToAnalogConverter/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

    mkdir -p "04-Lissajous/Scripts"
  library_dest=`realpath "./04-Lissajous/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

    mkdir -p "05-Morse/Scripts"
  library_dest=`realpath "./05-Morse/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

    mkdir -p "06-07-08-09-SystemOnChip/Scripts"
  library_dest=`realpath "./06-07-08-09-SystemOnChip/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

    mkdir -p "10-PipelinedOperators/Scripts"
  library_dest=`realpath "./10-PipelinedOperators/Scripts"`
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"

else

  if [ -n "$destdir" ]; then
    mkdir -p "$destdir/Scripts"
    library_dest=`realpath "$destdir/Scripts"`
  else
    mkdir -p "Scripts"
    library_dest=`realpath "./Scripts"`
  fi

  # Copy needed libraries per project
  echo "Update files in student repo $project"
  echo "    Copy scripts for Windows"
  cp -arf "$library_source/hdlDesigner.bat"     "$library_dest/"
  cp -arf "$library_source/cleanScratch.bat"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bat"  "$library_dest/"
  cp -arf "$library_source/searchPaths.bat"     "$library_dest/"
  echo "    Copy scripts for Linux"
  cp -arf "$library_source/hdlDesigner.bash"    "$library_dest/"
  cp -arf "$library_source/cleanGenerated.bash" "$library_dest/"
  cp -arf "$library_source/generateSSHKey.bash" "$library_dest/"
  echo "    Copy perl scripts for HDL Designer"
  cp -arf "$library_source/trimLibs.pl"         "$library_dest/"
  cp -arf "$library_source/update_ise.pl"       "$library_dest/"
  cp -arf "$library_source/update_libero.pl"    "$library_dest/"
  cp -arf "$library_source/update_diamond.pl"   "$library_dest/"
  cp -arf "$library_source/start_libero.pl"     "$library_dest/"
  cp -arf "$library_source/start_diamond.pl"     "$library_dest/"
fi

# add/commit/push changes to student repo
echo "    Git: Add => Commit => Push"
git add -A
git commit -a -m "$DATE: Automatic Scripts Update with ``deployScripts.bash`` :shipit:"
git push origin main
cd ..

# Delete tmp directory
cd ..
echo "    Delete tmp directory"
rm -rf "./tmp"

#-------------------------------------------------------------------------------
# Exit
#s
echo ""
echo "-- $DATE: $project updated at $repo"
echo "$SEPARATOR"
popd