#!/bin/bash

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

usage='Usage: deployLibs.bash [-p projectName] [-r repourl] [-v] [-h]'
while getopts 'p:r:vh' options; do
  case $options in
    p ) project=$OPTARG;;
    r ) repo=$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 Libraries for Students"
  echo "${INDENT}for $project"
  echo "${INDENT}to $repo"
  echo ""
fi

#-------------------------------------------------------------------------------
# Clean current repo
#
echo "Clean parent repo from intermediate files"
./cleanGenerated.bash

#-------------------------------------------------------------------------------
# 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_dest=`realpath "./Libs"`
library_source=`realpath "./../../.."`
mkdir -p $library_dest

# Copy needed libraries per project
echo "Update files in student repo $project"
if [ "$project" == "synd-did-labs" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, NanoBlaze"
  cp -arf "$library_source/Gates/"          "$library_dest/"
  cp -arf "$library_source/IO/"             "$library_dest/"
  cp -arf "$library_source/Sequential/"     "$library_dest/"
  cp -arf "$library_source/Common/"         "$library_dest/"
  cp -arf "$library_source/Common_test/"    "$library_dest/"
  cp -arf "$library_source/NanoBlaze/"      "$library_dest/"
elif [ "$project" == "ete-did-labs" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Operators, Common, NanoBlaze"
  cp -arf "$library_source/Gates/"          "$library_dest/"
  cp -arf "$library_source/IO/"             "$library_dest/"
  cp -arf "$library_source/Sequential/"     "$library_dest/"
  cp -arf "$library_source/Operators/"      "$library_dest/"
  cp -arf "$library_source/Common/"         "$library_dest/"
  cp -arf "$library_source/Common_test/"    "$library_dest/"
  cp -arf "$library_source/NanoBlaze/"      "$library_dest/"
elif [ "$project" == "isc-did-labs" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, NanoBlaze"
  cp -arf "$library_source/Gates/"          "$library_dest/"
  cp -arf "$library_source/IO/"             "$library_dest/"
  cp -arf "$library_source/Sequential/"     "$library_dest/"
  cp -arf "$library_source/Common/"         "$library_dest/"
  cp -arf "$library_source/Common_test/"    "$library_dest/"
  cp -arf "$library_source/NanoBlaze/"      "$library_dest/"
elif [ "$project" == "car-labs" ]; then
  echo "    Copy libraries: Common, Gates, Memory, Operators, Sequential"
  library_dest=`realpath "./heirv32_sc/Libs"`
  cp -arf "$library_source/Common/"         "$library_dest/"
  cp -arf "$library_source/Common_test/"    "$library_dest/"
  cp -arf "$library_source/Gates/"          "$library_dest/"
  cp -arf "$library_source/Lattice/"        "$library_dest/"
  cp -arf "$library_source/Memory/"         "$library_dest/"
  cp -arf "$library_source/Operators/"      "$library_dest/"
  cp -arf "$library_source/Sequential/"     "$library_dest/"
elif [ "$project" == "car-heirv" ]; then
  echo "    Copy libraries: Common, Gates, Memory, Operators, Sequential"
  cp -arf "$library_source/Common/"         "$library_dest/"
  cp -arf "$library_source/Common_test/"    "$library_dest/"
  cp -arf "$library_source/Gates/"          "$library_dest/"
  cp -arf "$library_source/Lattice/"        "$library_dest/"
  cp -arf "$library_source/Memory/"         "$library_dest/"
  cp -arf "$library_source/Operators/"      "$library_dest/"
  cp -arf "$library_source/Sequential/"     "$library_dest/"
elif [ "$project" == "ele_labs" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Operators, Common, Memory, Modulation, NanoBlaze"
  cp -ar "$library_source/Gates"           "$library_dest/"
  cp -ar "$library_source/IO"              "$library_dest/"
  cp -ar "$library_source/Sequential"      "$library_dest/"
  cp -ar "$library_source/Operators"       "$library_dest/"
  cp -ar "$library_source/Common"          "$library_dest/"
  cp -ar "$library_source/Common_test"     "$library_dest/"
  cp -arf "$library_source/Lattice/"       "$library_dest/"
  cp -ar "$library_source/Memory"          "$library_dest/"
  cp -ar "$library_source/Memory_test"     "$library_dest/"
  cp -ar "$library_source/Modulation"      "$library_dest/"
  cp -ar "$library_source/Modulation_test" "$library_dest/"
  cp -ar "$library_source/NanoBlaze"       "$library_dest/"
  cp -ar "$library_source/NanoBlaze_test"  "$library_dest/"
elif [ "$project" == "sem-labs" ]; then
  echo "    Copy libraries: Common, RS232, AhbLite, Memory, RiscV, NanoBlaze"
  cp -ar "$library_source/Common"         "$library_dest/"
  cp -ar "$library_source/Common_test"    "$library_dest/"
  cp -ar "$library_source/RS232"          "$library_dest/"
  cp -ar "$library_source/RS232_test"     "$library_dest/"
  cp -ar "$library_source/AhbLite"        "$library_dest/"
  cp -ar "$library_source/AhbLite_test"   "$library_dest/"
  cp -arf "$library_source/Lattice/"       "$library_dest/"
  cp -ar "$library_source/Memory"         "$library_dest/"
  cp -ar "$library_source/Memory_test"    "$library_dest/"
  cp -ar "$library_source/RiscV"          "$library_dest/"
  cp -ar "$library_source/RiscV_test"     "$library_dest/"
  cp -ar "$library_source/NanoBlaze"      "$library_dest/"
  cp -ar "$library_source/NanoBlaze_test" "$library_dest/"
elif [ "$project" == "did-cursor" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Lcd, Memory"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Lcd"         "$library_dest/"
  cp -ar "$library_source/Lcd_test"    "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
elif [ "$project" == "did-chrono" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Lcd, Memory, RS232"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Lcd"         "$library_dest/"
  cp -ar "$library_source/Lcd_test"    "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
  #cp -ar "$library_source/RS232"       "$library_dest/"
  #cp -ar "$library_source/RS232_test"  "$library_dest/"
elif [ "$project" == "did-kart-ebs2" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Lcd, Memory, RS232"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -ar "$library_source/I2C"         "$library_dest/"
  cp -ar "$library_source/I2C_test"    "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
  cp -ar "$library_source/RS232"       "$library_dest/"
  cp -ar "$library_source/RS232_test"  "$library_dest/"
elif [ "$project" == "did-kart-ebs3" ]; then
  echo "    Copy libraries: Common, Gates, I2C, Memory, RS232, IO, Sequential, UVM"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/I2C"         "$library_dest/"
  cp -ar "$library_source/I2C_test"    "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
  cp -ar "$library_source/RS232"       "$library_dest/"
  cp -ar "$library_source/RS232_test"  "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/UVM"         "$library_dest/"
  cp -ar "$library_source/UVM_test"    "$library_dest/"
elif [ "$project" == "did-synchro" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Cordic"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Operators"   "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
  cp -ar "$library_source/RS232"       "$library_dest/"
  cp -ar "$library_source/RS232_test"  "$library_dest/"
elif [ "$project" == "did-inverter" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Cordic"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Operators"   "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -ar "$library_source/Cordic"      "$library_dest/"
  cp -ar "$library_source/Cordic_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
elif [ "$project" == "eln_support" ]; then
  echo "Nothing todo, no Libararies needed"
elif [ "$project" == "did-display" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common, Lcd, Memory, RS232"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Lcd"         "$library_dest/"
  cp -ar "$library_source/Lcd_test"    "$library_dest/"
  cp -ar "$library_source/Memory"      "$library_dest/"
  cp -ar "$library_source/Memory_test" "$library_dest/"
elif [ "$project" == "eptm_radio" ]; then
  echo "    Copy libraries: Gates, IO, Sequential, Common"
  cp -ar "$library_source/Gates"       "$library_dest/"
  cp -ar "$library_source/IO"          "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
  cp -ar "$library_source/Sequential"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
elif [ "$project" == "eptm_audioamp" ]; then
  echo "    Copy libraries: AD_DA, Common, Filter"
  cp -ar "$library_source/AD_DA"       "$library_dest/"
  cp -ar "$library_source/AD_DA_test"  "$library_dest/"
  cp -ar "$library_source/Common"      "$library_dest/"
  cp -ar "$library_source/Common_test" "$library_dest/"
  cp -ar "$library_source/Filter"      "$library_dest/"
  cp -ar "$library_source/Filter_test" "$library_dest/"
  cp -arf "$library_source/Lattice/"   "$library_dest/"
elif [ "$project" == "cansat" ]; then
  echo "    Copy libraries: AhbLite, AhbLiteComponents, Common, Commandline, Memory, NanoBlaze, RS232"
  cp -ar "$library_source/AhbLite"                "$library_dest/"
  cp -ar "$library_source/AhbLite_test"           "$library_dest/"
  cp -ar "$library_source/AhbLiteComponents"      "$library_dest/"
  cp -ar "$library_source/AhbLiteComponents_test" "$library_dest/"
  cp -ar "$library_source/Common"                 "$library_dest/"
  cp -ar "$library_source/Commandline"            "$library_dest/"
  cp -ar "$library_source/Commandline_test"       "$library_dest/"
  cp -arf "$library_source/Lattice/"              "$library_dest/"
  cp -ar "$library_source/Memory"                 "$library_dest/"
  cp -ar "$library_source/Memory_test"            "$library_dest/"
  cp -ar "$library_source/NanoBlaze"              "$library_dest/"
  cp -ar "$library_source/NanoBlaze_test"         "$library_dest/"
  cp -ar "$library_source/RS232"                  "$library_dest/"
  cp -ar "$library_source/RS232_test"             "$library_dest/"
fi

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

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

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