#!/bin/bash

#================================================================================
# deployLab.bash - updates github repos for individual labos
# Example usage 01: ./Scripts/deployLab.bash -v -p synd-did-labs -r https://github.com/hei-synd-did/did-labs.git
# Example usage 02: ./Scripts/deployLab.bash -v -p ete-did-labs -r https://github.com/hei-ete-did/did-labs.git
# Example usage 03: ./Scripts/deployLab.bash -v -p isc-did-labs -r https://github.com/hei-isc-eln/did-labs.git
# Example usage 04: ./Scripts/deployLab.bash -v -p did-chrono -r https://github.com/hei-synd-did/did-chrono.git
# Example usage 05: ./Scripts/deployLab.bash -v -p did-cursor -r https://github.com/hei-synd-did/did-cursor.git
# Example usage 06: ./Scripts/deployLab.bash -v -p did-inverter -r https://github.com/hei-ete-did/did-inverter.git
# Example usage 07: ./Scripts/deployLab.bash -v -p did-synchro -r https://github.com/hei-ete-did/did-synchro.git
# Example usage 08: ./Scripts/deployLab.bash -v -p did-kart-ebs3 -r https://github.com/hei-synd-did/did-kart-ebs3.git
# Example usage 09: ./Scripts/deployLab.bash -v -p did-display -r https://github.com/hei-isc-did/did-display.git

# Example usage 10: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 01-WaveformGenerator
# Example usage 11: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 01-WaveformGenerator
# Example usage 12: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 02-SplineInterpolator
# Example usage 13: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 03-DigitalToAnalogConverter
# Example usage 14: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 04-Lissajous
# Example usage 15: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 05-Morse
# Example usage 16: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 06-07-08-09-SystemOnChip
# Example usage 17: ./Scripts/deployLab.bash -v -p sem-labs -r https://github.com/hei-synd-sem/sem-labs.git -d 10-PipelinedOperators

# Example usage 18: ./Scripts/deployLab.bash -v -p car-labs -r https://github.com/hei-isc-car/car-labs.git -s 1
# Example usage 19: ./Scripts/deployLab.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='synd-did-labs'
repo='https://github.com/hei-synd-did/did-labs.git'
dir='01-StepperMotor'
sourceDirUp=0

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

repo_dest=`realpath "./"`
repo_source="./"
# If Scripts folder is at root of project, needs to get up 3 times (scripts, project name, tmp)
# If more is needed (i.e. see car-labs structure), can be added with -s switch
for ((i=0;i<$sourceDirUp+3;i++)); do
  repo_source="$repo_source/.."
done
repo_source=`realpath "$repo_source"`

echo "Update files in student repo $project"
# Copy needed files per project
if [ "$project" == "synd-did-labs" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  rm -v -f ./Num/hdl/sinewaveGenerator_comb.vhd

elif [ "$project" == "ete-did-labs" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  rm -v -f ./Num/hdl/sinewaveGenerator_comb.vhd

elif [ "$project" == "isc-did-labs" ]; then
    # copy root files except some specific ones
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
    # iterate over folders to choose what to copy
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  rm -v -f ./Num/hdl/sinewaveGenerator_comb.vhd

elif  [ "$project" == "car-labs" ]; then
  # bem
  echo "copy $repo_source/bem"
  cp -ar "$repo_source/bem" ./
  # isa
  echo "copy $repo_source/isa"
  cp -ar "$repo_source/isa" ./
  # heirv32_sc
  mkdir -p heirv32_sc
    # copy root files except some specific ones
  find $repo_source/hdl -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest/heirv32_sc \;
    # iterate over folders to choose what to copy
  for folder in $(find $repo_source/hdl -maxdepth 1 -type d )
  do
    if [[ "$repo_source/hdl" != "$folder" && "$repo_source/hdl/.git" != "$folder" && "$repo_source/hdl/Libs" != "$folder" && "$repo_source/hdl/Scripts" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder heirv32_sc/
    else
      echo "skip $folder"
    fi
  done

  rm -v -f heirv32_sc/Board/concat/concatenated.vhd
  rm -v -f heirv32_sc/Board/concat/car-labs.vhd

  rm -v -f heirv32_sc/HEIRV32/hdl/ALU_rtl.vhd
  rm -v -f heirv32_sc/HEIRV32/hdl/extend_rtl.vhd
  rm -v -f heirv32_sc/HEIRV32/hdl/aludecoder_entity.vhg
  rm -v -f heirv32_sc/HEIRV32/hdl/aluDecoder_rtl.vhd
  rm -v -f -r heirv32_sc/HEIRV32/hds/alu@decoder
  rm -v -f heirv32_sc/HEIRV32/hds/_aludecoder._epf

  rm -v -f heirv32_sc/HEIRV32/SingleCycle/hdl/mainDecoder_entity.vhg
  rm -v -f heirv32_sc/HEIRV32/SingleCycle/hdl/mainDecoder_rtl.vhd
  rm -v -f -r heirv32_sc/HEIRV32/SingleCycle/hds/main@decoder
  rm -v -f heirv32_sc/HEIRV32/SingleCycle/hds/_maindecoder._epf

  rm -v -f heirv32_sc/Simulation/empty_ram.txt

elif [ "$project" == "car-heirv" ]; then
  # heirv32_mc
    # copy root files except some specific ones
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
    # iterate over folders to choose what to copy
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source/" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done

  rm -v -f ./Board/concat/concatenated.vhd
  rm -v -f ./Board/concat/car-labs.vhd

  rm -v -f ./HEIRV32/hdl/ALU_rtl.vhd
  rm -v -f ./HEIRV32/hdl/extend_rtl.vhd
  rm -v -f ./HEIRV32/hdl/aludecoder_entity.vhg
  rm -v -f ./HEIRV32/hdl/aluDecoder_rtl.vhd
  rm -v -f -r ./HEIRV32/hds/alu@decoder
  rm -v -f ./HEIRV32/hds/_aludecoder._epf

  rm -v -f ./HEIRV32/MultiCycle/hdl/instrdecoder_entity.vhg
  rm -v -f ./HEIRV32/MultiCycle/hdl/instrDecoder_rtl.vhd
  rm -v -f ./HEIRV32/MultiCycle/hdl/mainfsm_entity.vhg
  rm -v -f ./HEIRV32/MultiCycle/hdl/mainfsm_fsm.vhg
  rm -v -f -r ./HEIRV32/MultiCycle/hds/instr@decoder
  rm -v -f -r ./HEIRV32/MultiCycle/hds/main@f@s@m
  rm -v -f ./HEIRV32/MultiCycle/hds/_instrdecoder._epf
  rm -v -f ./HEIRV32/MultiCycle/hds/_mainfsm._epf

  rm -v -f ./HEIRV32_test/hdl/universalTester_test.vhd
  rm -v -f -r ./HEIRV32_test/hds/universal@tester
  rm -v -f ./HEIRV32_test/hds/_universaltester._epf

  rm -v -f ./Simulation/code_mc_disassembled.svg
  rm -v -f ./Simulation/code_mc_disassembled_labels.svg
  rm -v -f ./Simulation/code_mc_ghidra_labels.png
  rm -v -f ./Simulation/empty_ram.txt

  #search='design_root = Board.ebs2_sc(struct)ebs2_sc/struct.bd'
  #replace='design_root = Board.ebs2_mc(struct)ebs2_mc/struct.bd'
  #sed -i "s/$search/$replace" Prefs/hds.hdp

elif [ "$project" == "ele_labs" ]; then
  echo "Error: Not implemented yet"

elif [ "$project" == "sem-labs" ]; then
  mkdir -p $dir
  repo_dest=`realpath "./$dir/"`
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/01-WaveformGenerator/Scripts" != "$folder" && "$repo_source/02-SplineInterpolator/Scripts" != "$folder" && "$repo_source/03-DigitalToAnalogConverter/Scripts" != "$folder" && "$repo_source/04-Lissajous/Scripts" != "$folder" && "$repo_source/05-Morse/Scripts" != "$folder" && "$repo_source/06-07-08-09-SystemOnChip/Scripts" != "$folder" && "$repo_source/10-PipelinedOperators/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder $repo_dest
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for sem-labs $dir"

  if [ "$dir" == "01-WaveformGenerator" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "02-SplineInterpolator" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "03-DigitalToAnalogConverter" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "04-Lissajous" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "05-Morse" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "06-07-08-09-SystemOnChip" ]; then
    echo "Info: Nothing to be deleted"
  elif [ "$dir" == "10-PipelinedOperators" ]; then
    echo "Info: Nothing to be deleted"
  fi

elif [ "$project" == "did-cursor" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-cursor"
  rm -v -f ./Cursor/hdl/amplitudeControl_RTL.vhd
  rm -v -f ./Cursor/hdl/bridgeControl_RTL.vhd
  rm -v -f ./Cursor/hdl/decelerationPositions_RTL.vhd
  rm -v -f ./Cursor/hdl/divider_RTL.vhd
  rm -v -f ./Cursor/hdl/findDistance_RTL1.vhd
  rm -v -f ./Cursor/hdl/positionCounter_RTL.vhd
  rm -v -f ./Cursor/hdl/pulseWidthModulator_RTL.vhd
  rm -v -f ./Cursor/hds/_amplitudecontrol._epf
  rm -v -f ./Cursor/hds/_bridgecontrol._epf
  rm -v -f ./Cursor/hds/_control._epf
  rm -v -f ./Cursor/hds/_decelerationpositions._epf
  rm -v -f ./Cursor/hds/_divider._epf
  rm -v -f ./Cursor/hds/_positioncounter._epf
  rm -v -f ./Cursor/hds/_pulsewidthmodulator._epf
  rm -v -f ./Cursor/hds/.hdlsidedata/_amplitudecontrol_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_amplitudeControl_RTL.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_bridgecontrol_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_bridgeControl_RTL.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_control_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_control_fsm.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_decelerationpositions_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_decelerationPositions_RTL.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_divider_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_divider_RTL.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_findDistance_RTL1.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_positioncounter_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_positionCounter_RTL.vhd._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_pulsewidthmodulator_entity.vhg._fpf
  rm -v -f ./Cursor/hds/.hdlsidedata/_pulseWidthModulator_RTL.vhd._fpf
  rm -v -f -r ./Cursor/hds/amplitude@control
  rm -v -f -r ./Cursor/hds/bridge@control
  rm -v -f -r ./Cursor/hds/control
  rm -v -f -r ./Cursor/hds/deceleration@positions
  rm -v -f -r ./Cursor/hds/divider
  rm -v -f -r ./Cursor/hds/position@counter
  rm -v -f -r ./Cursor/hds/pulse@width@modulator
  rm -v -f -r ./Cursor/hds/rising@detector
  rm -v -f -r ./Board/concat/concatenated.vhd
  rm -v -f -r ./Board/concat/did-cursor.vhd

elif [ "$project" == "did-chrono" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-chrono"
  rm -v -f ./Chronometer/hdl/coilControl_RTL.vhd
  rm -v -f ./Chronometer/hdl/divider1Hz_RTL.vhd
  rm -v -f ./Chronometer/hdl/tickLengthCounter_RTL.vhd
  rm -v -f ./Chronometer/hdl/lcdDisplay_masterVersion.vhd
  rm -v -f ./Chronometer/hds/_coilcontrol._epf
  rm -v -f ./Chronometer/hds/_control._epf
  rm -v -f ./Chronometer/hds/_divider1hz._epf
  rm -v -f ./Chronometer/hds/_ticklengthcounter._epf
  rm -v -f ./Chronometer/hds/_coilcontrol._epf
  rm -v -f ./Chronometer/hds/_coilcontrol._epf
  rm -v -f ./Chronometer/hds/_coilcontrol._epf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_coilcontrol_entity.vhg._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_coilControl_RTL.vhd._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_control_entity.vhg._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_control_fsm.vhg._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_divider1hz_entity.vhg._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_divider1Hz_RTL.vhd._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_ticklengthcounter_entity.vhg._fpf
  rm -v -f ./Chronometer/hds/.hdlsidedata/_tickLengthCounter_RTL.vhd._fpf
  rm -v -f -r ./Chronometer/hds/coil@control/
  rm -v -f -r ./Chronometer/hds/control/
  rm -v -f -r ./Chronometer/hds/divider1@hz/
  rm -v -f -r ./Chronometer/hds/rising@detector/
  rm -v -f -r ./Chronometer/hds/tick@length@counter/
  rm -v -f -r ./Board/concat/concatenated.vhd
  rm -v -f -r ./Board/concat/did-chrono.vhd

elif [ "$project" == "did-kart-ebs2" ]; then
  mkdir -p $dir
  repo_dest=`realpath "./$dir/"`
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder $repo_dest
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-kart-ebs2 $dir"

  if [ "$dir" == "01-StepperMotor" ]; then
    rm -v -f ./01-StepperMotor/StepperMotor/hdl/angleDifference_RTL.vhd
	rm -v -f ./01-StepperMotor/StepperMotor/hdl/coilControl_RTL.vhd
	rm -v -f ./01-StepperMotor/StepperMotor/hdl/stepperCounter_RTL.vhd
	rm -v -f ./01-StepperMotor/StepperMotor/hds/angle@control/master@version.bd
	rm -v -f ./01-StepperMotor/StepperMotor/hds/coil@control/master@version_counter@demux.bd
	rm -v -f ./01-StepperMotor/StepperMotor/hds/coil@control/master@version_shift@reg.bd
  elif [ "$dir" == "02-DcMotor" ]; then
    rm -v -f ./02-DcMotor/DcMotor/hdl/dcMotorPwm_RTL.vhd
  elif [ "$dir" == "03-Sensors" ]; then
    rm -v -f ./03-Sensors/Sensors/hdl/hallCounters_RTL.vhd
	rm -v -f ./03-Sensors/Sensors/hdl/ultrasoundRanger_RTL.vhd
  elif [ "$dir" == "04-Controller" ]; then
    echo "Info: Nothing to be deleted"
  fi

elif [ "$project" == "did-kart-ebs3" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-kart-ebs3"
  rm -v -f ./DcMotor/hdl/dcMotorPwm_RTL.vhd
  rm -v -f ./Kart_test/hdl/txFIFO_tester_test.vhd
  rm -v -f -r ./Kart_test/hds/tx@f@i@f@o_tb/
  rm -v -f -r ./Kart_test/hds/tx@f@i@f@o_tester/
  rm -v -f ./Kart_test/hds/_txfifo_tb._epf
  rm -v -f ./Kart_test/hds/_txfifo_tester._epf
  rm -v -f -r ./Kart_test/hds/kart@controller_full_tb/
  rm -v -f ./Kart_test/hds/_kartcontroller_full_tb._epf
  rm -v -f ./Sensors/hdl/hallCounters_RTL.vhd
  rm -v -f ./Sensors/hdl/ultrasoundRanger_RTL.vhd
  rm -v -f ./Stepper/hdl/angleDifference_RTL.vhd
  rm -v -f ./Stepper/hdl/coilControl_RTL.vhd
  rm -v -f ./Stepper/hdl/stepperCounter_RTL.vhd
  rm -v -f ./Stepper/hds/angle@control/master@version.bd
  rm -v -f ./Stepper/hds/coil@control/master@version_counter@demux.bd
  rm -v -f ./Stepper/hds/coil@control/master@version_shift@reg.bd
  rm -v -f ./Stepper_test/hdl/stepperMotorRegisters_tester_test.vhd
  rm -v -f -r ./Stepper_test/hds/stepper@motor@registers_tb/
  rm -v -f -r ./Stepper_test/hds/stepper@motor@registers_tester/
  rm -v -f ./Stepper_test/hds/_steppermotorregisters_tb._epf
  rm -v -f ./Simulation/Kart/UVM/uvmCommands.txt
  rm -v -f ./Simulation/Kart/kartUVM.do
  rm -v -f ./Simulation/Kart/txFIFO.do
  rm -v -f ./Simulation/Stepper/stepperMotorRegisters.do
  rm -v -f -r ./Simulation/UART/
  rm -v -f -r ./UART_test/

  rm -v -f ./Board/concat/*.vhd
  find ./Board/libero/designer/ ! \( -name "*.prjx" -o -name "*.adb" -o -name "*.ide_des" -o -name "*.pdb" -o -name "*_syn.prj" \) -type f -exec rm -v -f -r {} +
  rm -v -f -r ./Documentation/
  rm -v -f -r ./CommandInterpreter/src/

elif [ "$project" == "did-synchro" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-synchro"
  rm -v -f -r ./Board/concat/concatenated.vhd
  rm -v -f -r ./Board/concat/did-synchro.vhd

elif [ "$project" == "did-inverter" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-inverter"
  rm -v -f -r ./Board/concat/concatenated.vhd
  rm -v -f -r ./Board/concat/did-inverter.vhd

elif [ "$project" == "did-display" ]; then
  find $repo_source -maxdepth 1 -type f \! \( -name .gitmodules -o -name .gitlab-ci.yml -o -name README.md -o -name LICENSE \) -exec cp -ar '{}' $repo_dest \;
  for folder in $(find $repo_source -maxdepth 1 -type d )
  do
    if [[ "$repo_source" != "$folder" && "$repo_source/.git" != "$folder" && "$repo_source/Libs" != "$folder" && "$repo_source/Scripts" != "$folder" && "$repo_source/img" != "$folder" ]] ; then
      echo "copy $folder"
      cp -ar $folder ./
    else
      echo "skip $folder"
    fi
  done
  echo ""
  echo "Delete solutions blocs for did-display"
  find ./Board/ise/ -name "*.mcs" -type f -exec rm -v -f {} +
  rm -v -f -r ./Display/concat/
  find ./Display/hdl/ -name "*.vhg" -type f -exec rm -v -f {} +
  rm -v -f ./Display/hdl/test_pattern_calculated_rtl.vhd
  rm -v -f ./Display/hdl/vgaDataCreator_rgb.vhd
  rm -v -f ./Display/hdl/vgaDataSelector_rtl.vhd
  rm -v -f ./Display/hds/display@circuit/master@version.bd
  rm -v -f -r ./Display/hds/vga@data@calculated/
  rm -v -f -r ./Display/hds/vga@data@selector/

  rm -v -f -r ./Display_test/concat/

  rm -v -f -r ./Board/concat/concatenated.vhd
  rm -v -f -r ./Board/concat/did-display.vhd

elif [ "$project" == "eln_support" ]; then
  echo "Error: Not implemented yet"

elif [ "$project" == "eptm_radio" ]; then
  echo "Error: Not implemented yet"

elif [ "$project" == "eptm_audioamp" ]; then
  echo "Error: Not implemented yet"

elif [ "$project" == "cansat" ]; then
  echo "Error: Not implemented yet"
fi

echo "Source taken from $repo_source to $repo_dest"

#-------------------------------------------------------------------------------
# change from masterVersion to studentVersion and delete all masterVersion
#
if [ "$project" == "synd-did-labs" ]; then
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a masterVersion -n studentVersion -r
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a master@version -n student@version -r
elif [ "$project" == "ete-did-labs" ]; then
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a masterVersion -n studentVersion -r
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a master@version -n student@version -r
elif [ "$project" == "isc-did-labs" ]; then
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a masterVersion -n studentVersion -r
  ./../../changeDefaultViews.bash -p "Scripts/tmp/did-labs" -a master@version -n student@version -r
else
  ./../../changeDefaultViews.bash -p "Scripts/tmp/$project" -a masterVersion -n studentVersion -r
  ./../../changeDefaultViews.bash -p "Scripts/tmp/$project" -a master@version -n student@version -r
fi

# add/commit/push changes to student repo
git add -A
git commit -a -m "$DATE: Automatic Laboratory Update with ``deployLab.bash`` :shipit:"
git push origin main
cd ..

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

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