#!/bin/bash
source ./colors.sh
source ./platform_recognizer.sh

# Exit when any installment failed.
set -e

while :; do
  case $1 in
    -y|-Y) FLAG_AUTO_CONFIRM="y"  # Automatic confirm.
    ;;
    -m) FLAG_AUTO_MAX_PERF="true"  # Use maximum frequency.
    ;;
    *) break
  esac
  shift
done

if [[ "$platform" != "edgetpu_devboard" ]]; then
  operating_frequency_notice=$(cat << EOM
Warning: During normal operation, the Edge TPU Accelerator may heat up, depending
on the computation workloads and operating frequency. Touching the metal part of the
device after it has been operating for an extended period of time may lead to discomfort
and/or skin burns. As such, when running at the default operating frequency, the device is
intended to safely operate at an ambient temperature of 35C or less. Or when running at
the maximum operating frequency, it should be operated at an ambient temperature of
25C or less.

Google does not accept any responsibility for any loss or damage if the device is operated
outside of the recommended ambient temperature range.
.............................................................
Would you like to enable the maximum operating frequency? Y/N
EOM
                             )

  if [[ "$FLAG_AUTO_CONFIRM" == "y" ]] || [[ "$FLAG_AUTO_MAX_PERF" ==  "true" ]]; then
    if [[ "$FLAG_AUTO_MAX_PERF" ==  "true" ]]; then
      use_maximum_frequency="y"
    else
      use_maximum_frequency="n"
    fi
  else
    read -p "$operating_frequency_notice" use_maximum_frequency
  fi
  case "$use_maximum_frequency" in
    [yY])
      echo "Using maximum operating frequency."
      use_maximum_frequency="y"
      ;;
    *)
      echo "Using default operating frequency."
      use_maximum_frequency="n"
      ;;
  esac
fi

if [[ "$platform" == "glinux" ]] || [[ "$platform" == "x86_64_linux" ]]; then
  if [[ "$use_maximum_frequency" == "y" ]]; then
    libedgetpu_src="libedgetpu/libedgetpu_x86_64.so"
  else
    libedgetpu_src="libedgetpu/libedgetpu_x86_64_throttled.so"
  fi
  libedgetpu_dst="/lib/x86_64-linux-gnu/libedgetpu.so"
elif [[ "$platform" == "raspberry_pi_3b" ]] || [[ "$platform" == "raspberry_pi_3b+" ]];then
  if [[ "$use_maximum_frequency" == "y" ]]; then
    libedgetpu_src="libedgetpu/libedgetpu_arm32.so"
  else
    libedgetpu_src="libedgetpu/libedgetpu_arm32_throttled.so"
  fi
  libedgetpu_dst="/lib/arm-linux-gnueabihf/libedgetpu.so"
elif [[ "$platform" == "edgetpu_devboard" ]]; then
  echo -e "${GREEN}Edgetpu Python API is pre-installed on DevBoard!${DEFAULT}"
else
  echo -e "${RED}Platform not supported!${DEFAULT}"
  exit
fi

# Install dependent libraries.
echo -e "${GREEN}Install dependent libraries.${DEFAULT}"
sudo apt-get install -y libusb-1.0-0-dev python3-setuptools python3-numpy python3-dev zlib1g-dev libgoogle-glog-dev swig libjpeg-dev libunwind-dev libc++-dev libc++abi-dev

# Check udev rules.
udev_rule_path="/etc/udev/rules.d/99-edgetpu-accelerator.rules"
if [[ -f "$udev_rule_path" ]]; then
  echo -e "${YELLOW}Found udev rules for edgetpu. We'll replace it.${DEFAULT}"
  echo -e "${RED}Remove existing 99-edgetpu-accelerator.rules.${DEFAULT}"
  sudo rm $udev_rule_path
fi

echo -e "${GREEN}Copy 99-edgetpu-accelerator.rules to udev directory.${DEFAULT}"
sudo cp -p "99-edgetpu-accelerator.rules" $udev_rule_path
sudo udevadm control --reload-rules && udevadm trigger

# Check driver (libedgetpu.so).
if [[ -f "$libedgetpu_dst" ]]; then
  echo -e "${YELLOW}libedgetpu.so exists! To avoid version mismatch between edgetpu driver and our API, we'll replace it with ours.${DEFAULT}"
  echo -e "${RED}Remove existing libedgetpu.so.${DEFAULT}"
  sudo rm $libedgetpu_dst
fi

echo -e "${GREEN}Copy libedgetpu.so to system directory.${DEFAULT}"
sudo cp -p $libedgetpu_src $libedgetpu_dst

echo -e "${GREEN}Copy precompiled _edgetpu_cpp_wrapper.so.${DEFAULT}"
pre_compiled_dir="edgetpu/swig/compiled_so"
output_path="edgetpu/swig/_edgetpu_cpp_wrapper.so"
if [[ "$platform" == "glinux" ]] || [[ "$platform" == "x86_64_linux" ]]; then
  cp "$pre_compiled_dir/_edgetpu_cpp_wrapper_x86_64.so" "$output_path"
elif [[ "$platform" == "raspberry_pi_3b" ]] || [[ "$platform" == "raspberry_pi_3b+" ]]; then
  cp "$pre_compiled_dir/_edgetpu_cpp_wrapper_arm32.so" "$output_path"
fi

# Python wrapper generated by SWIG.
cp "$pre_compiled_dir/edgetpu_cpp_wrapper.py" edgetpu/swig/

python3 setup.py develop --user
