#!/usr/bin/env bash
set -euo pipefail

install_packages(){
  echo "checking installed packages"
  packages=("python3.6" "python-six")
  need_install=false
  for pkg in ${packages[@]}; do
      if ! dpkg -s $pkg >/dev/null 2>&1; then
        echo "${pkg} needs install"
        need_install=true
      fi
  done
  if ${need_install}; then
      sudo add-apt-repository ppa:deadsnakes/ppa -y
      sudo apt-get update
      # python3.6 required to run fly
      sudo apt install -y python3.6 python-six
  fi
}

install_packages

export TEMP_HOME=~
sudo rm -rf /usr/bin/fly ${TEMP_HOME}/.fly
curl --silent http://firefly.web.zooxlabs.com/fly > ${TEMP_HOME}/.fly
sudo chmod 0755 ${TEMP_HOME}/.fly
sudo ln -s ${TEMP_HOME}/.fly /usr/bin/fly
echo "done"
