#!/bin/bash
AUTHOR='Akgnah <setq@radxa.com>'
VERSION='0.14'
PI_MODEL=`tr -d '\0' < /proc/device-tree/model`
RPI_DEB="https://s3.setq.io/rockpi/deb/raspi-sata-${VERSION}.deb"
PYPI_DL="https://s3.setq.io/rockpi/pypi"
AGPIO="Adafruit-GPIO-v1.0.6"
SSD1306="Adafruit_SSD1306-v1.6.2"


confirm() {
  printf "%s [Y/n] " "$1"
  read resp < /dev/tty
  if [ "$resp" == "Y" ] || [ "$resp" == "y" ] || [ "$resp" == "yes" ]; then
    return 0
  fi
  if [ "$2" == "abort" ]; then
    echo -e "Abort.\n"
    exit 0
  fi
  return 1
}

apt_check() {
  packages="python3-rpi.gpio python3-setuptools python3-pip python3-pil python3-spidev pigpio python3-pigpio net-tools"
  need_packages=""

  idx=1
  for package in $packages; do
    if ! apt list --installed 2> /dev/null | grep "^$package/" > /dev/null; then
      pkg=$(echo "$packages" | cut -d " " -f $idx)
      need_packages="$need_packages $pkg"
    fi
    ((++idx))
  done

  if [ "$need_packages" != "" ]; then
    echo -e "\nPackage(s) $need_packages is required.\n"
    confirm "Would you like to apt-get install the packages?" "abort"
    apt-get update
    apt-get install --no-install-recommends $need_packages -y
  fi
}

deb_install() {
  TEMP_DEB="$(mktemp)"
  curl -sL "$RPI_DEB" -o "$TEMP_DEB"
  dpkg -i "$TEMP_DEB"
  rm -f "$TEMP_DEB"
}

dtb_enable() {
  python3 /usr/bin/rockpi-sata/misc.py open_w1_i2c
}

pip_fixpath() {
  path=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
  if [ ! -d $path ]; then
    mkdir -p $path
  fi
}

pip_clean() {
  packages="Adafruit-GPIO Adafruit-PureIO Adafruit-SSD1306"
  for package in $packages; do
    if pip3 list 2> /dev/null | grep "$package" > /dev/null; then
      sudo -H pip3 uninstall "$package" --break-system-packages -y 
    fi
  done
}

pip_install() {
  pip_fixpath
  pip_clean

  for pack in $AGPIO $SSD1306; do 
    temp_zip="$(mktemp)"
    temp_dir="$(mktemp -d)"
    curl -sL "$PYPI_DL/$pack.zip" -o "$temp_zip"
    unzip "$temp_zip" -d "$temp_dir" > /dev/null
    cd "$temp_dir/$pack"
    python3 setup.py install && cd -
    rm -rf "$temp_zip" "$temp_dir"
  done
}

main() {
  if [[ "$PI_MODEL" =~ "Raspberry" ]]; then
    apt_check
    pip_install
    deb_install
    dtb_enable
  else
    echo 'nothing'
  fi
}

main
