#!/bin/bash -e
set -x
here="$(realpath "$(dirname "$0")")"
cd "$here"

echo "---------- initialize venv ----------"
lsof "./venv/bin/python" | awk 'NR > 1 {print $2}' | xargs kill || :
lsof "./venv/Scripts/activate" | awk 'NR > 1 {print $2}' | xargs kill || :
python="python"
"${python}" -m pip install --upgrade pip
"${python}" -m venv venv
source "./venv/bin/activate" || source "./venv/Scripts/activate" || :
"${python}" -m pip install --upgrade pip
"${python}" -m pip install --upgrade -r "src/requirements.txt"
# echo "---------- get updated label-map-util ----------"
# sudo apt-get install -y protobuf-compiler
# wget "https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/protos/string_int_label_map.proto" -O "./src/lib/helpers/object_detection/protos/string_int_label_map.proto"
# insert="\n\1\/\/ Label color for rendering.\n\1optional string color = 9;"
# sed -Ei "s/^(\s*)(optional string display_name.*)$/\1\2\n${insert}\n/" "./src/lib/helpers/object_detection/protos/string_int_label_map.proto"
# protoc "./src/lib/helpers/object_detection/protos/"*.proto --python_out="."
# wget "https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/utils/label_map_util.py" -O "./src/lib/helpers/object_detection/utils/label_map_util.py"
# sed -Ei "s/^(\s*from )(object_detection.protos import .*)$/\1lib.helpers.\2/" "./src/lib/helpers/object_detection/utils/label_map_util.py"
# insert='\1if item.HasField("color"):\n\1  category["color"] = item.color\n\1else:\n\1  category["color"] = ""'
# sed -Ei "s/^(\s*)(categories\.append\(category\))$/${insert}\n\1\2/" "./src/lib/helpers/object_detection/utils/label_map_util.py"
# echo "---------- install libedgetpu ----------"
# # sudo apt-get install -y apt-transport-https curl gnupg
# # curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
# # sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
# # echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
# # sudo apt-get update
# sudo apt install -y build-essential docker # bazel libusb-1.0-0-dev libabsl-dev libflatbuffers-dev
# mkdir -p "$here/tmp"
# cd "$here/tmp"
# # git clone https://github.com/tensorflow/tensorflow || :
# # cd tensorflow
# # git pull
# # cd ..
# git clone "https://github.com/google-coral/libedgetpu" || :
# cd libedgetpu
# git pull
# DOCKER_IMAGE="ubuntu:18.04" DOCKER_TARGETS=libedgetpu make docker-build
# sudo make install
# cd "$here"
#echo "---------- install gxlpy ----------"
#sudo apt-get install -y g++ libc-bin
#mkdir -p "$here/tmp"
#cd "$here/tmp"
#wget --continue --timestamping "http://downloads.get-cameras.com/Galaxy_Linux-x86_Gige-U3_32bits-64bits_1.2.1911.9122.tar.gz"
#7z x -y "Galaxy_Linux-x86_Gige-U3_32bits-64bits_1.2.1911.9122.tar.gz"
#7z x -y "Galaxy_Linux-x86_Gige-U3_32bits-64bits_1.2.1911.9122.tar"
#cd "Galaxy_Linux-x86_Gige-U3_32bits-64bits_1.2.1911.9122"
#chmod +x "Galaxy_camera.run"
#echo -en "\nY\nY\nEn\nY\n" | ./Galaxy_camera.run
#cd "$here/tmp"
#wget --continue --timestamping "http://downloads.get-cameras.com/Galaxy_Linux_Python_1.0.1905.9081.tar.gz"
#7z x -y "Galaxy_Linux_Python_1.0.1905.9081.tar.gz"
#7z x -y "Galaxy_Linux_Python_1.0.1905.9081.tar"
#cd "Galaxy_Linux_Python_1.0.1905.9081/api"
#python3 setup.py build
#python3 setup.py install
#cd "$here"

cd "$here"
