#!/bin/bash

# echo "nameserver 8.8.8.8" > /etc/resolv.conf && echo "nameserver 8.8.4.4" >> /etc/resolv.conf && mkdir -p /usr/local/nebula
# wget -O /usr/local/nebula/nebula.sh https://ff:4lKmvA9Up4op@pkg.dev.snaju.com/freefall/release/nebula.sh && bash /usr/local/nebula/nebula.sh install -u "ff" -p "4lKmvA9Up4op" && bash /usr/local/nebula/nebula.sh start

CMD=$1

USER="ff"
PASS="4lKmvA9Up4op"

ENDPOINT="https://$USER:$PASS@pkg.dev.snaju.com/freefall/release"

# -u and -p for user and password (will default to above)
while getopts ":u:p:" opt; do
  case $opt in
    u) USER="$OPTARG"
    ;;
    p) PASS="$OPTARG"
    ;;
  esac
done

PHP=$(which php)

update_app() {
  export COMPOSER_ALLOW_SUPERUSER=1
  apt-get update
  apt-get install freefall-dev
}

config_env() {
  sed -i '/^\tProxyPreserveHost/d' /etc/apache2/sites-available/000-default.conf 
  sed -i '/^\tProxyRequests/d' /etc/apache2/sites-available/000-default.conf
  sed -i '/^\tProxyPass/d' /etc/apache2/sites-available/000-default.conf
  sed -i '/^<\/VirtualHost>/i \\tProxyPreserveHost On\n\tProxyRequests Off\n\tProxyPass / http://localhost:8888/' /etc/apache2/sites-available/000-default.conf
  systemctl restart apache2
  mv /usr/local/app/.env-example /usr/local/app/.env
  printf "\nDB_CONFIG_FILE=db.php\n" | tee -a /usr/local/app/.env
}

make_link() {
  ln -s /usr/local/nebula/nebula.sh /bin/nebula
}

fix_permissions() {
    chmod +x /usr/local/nebula/nebula.sh
    # give sudo to user
}

case $CMD in

start)
  # start app
  service apache2 start
  exit
  ;;

stop)
  # stop app
  service apache2 stop
  exit
  ;;

restart)
  # restart app
  bash /usr/local/nebula/nebula.sh stop
  bash /usr/local/nebula/nebula.sh start
  exit
  ;;

install)
  wget https://rpm.snaju.com/freefall/setup.sh -O /tmp/setup.sh
  bash /tmp/setup.sh
  exit
  ;;

update)
  apt-get install freefall-dev
  exit
  ;;

pull)
  echo "Updating this script..."
  wget -O /usr/local/nebula/nebula.sh https://ff:4lKmvA9Up4op@pkg.dev.snaju.com/freefall/release/nebula.sh
  fix_permissions
  exit
  ;;

fix)
  echo "Fixing Permissions..."
  fix_permissions
  exit
  ;;

systemd)
  echo "Updating Systemd"
  make_system_d
  exit
  ;;

py)
  SUBCMD=$2

  FOLDER="/opt/nebula/null"
  BIN="null"
  ZIP="null.zip"

  case $SUBCMD in
  main)
    # Update the MSC software
    FOLDER="/opt/nebula/freefall_py"
    BIN="main"
    ZIP="freefall_py.zip"
    ;;
  *)
    echo "py [module]"
    echo "  main"
    ;;

    # Exec the stuff

  esac

  if [ $BIN != "null" ]; then
    echo "Please validate the following to continue..."
    echo "Selected Dest Folder: $FOLDER"
    echo "Binary: $BIN"
    echo "Nebula ZIP: $ZIP"
    read -p "If this look correct? (y/N) " correctInput

    if [ $correctInput == "y" ]; then

      echo "Cleaning tmp dirs..."

      rm -rf /tmp/py_installer
      mkdir -p /tmp/py_installer

      echo "Downloading python Bins using $ZIP..."

      wget -O /tmp/py_installer/$ZIP $ENDPOINT/py/$ZIP

      echo "Creating $FOLDER..."

      mkdir -p $FOLDER

      echo "Cleaning up old bins from $FOLDER..."

      rm -rf $FOLDER/*.py
      rm -f $FOLDER/$BIN

      echo "Unzip $ZIP..."

      unzip -o /tmp/py_installer/$ZIP -d /tmp/py_installer

      echo "cp $BIN to $FOLDER"

      cp /tmp/py_installer/$BIN $FOLDER/.

      echo "Updating chmod on $FOLDER..."
      chmod ugo+rwx $FOLDER/$BIN

      echo "Python update for $BIN complete!"
    else
      echo "Aborting..."
    fi
  fi
  ;;


*)
  echo "nebula [cmd]"
  echo "    start - Start Nebula without Systemd"
  echo "    stop - Stop Nebula without Systemd"
  echo "    fix - Fixes the File Permissions for Nebula"
  echo "    install - Installs Nebula onto system from Snaju Central Repo"
  echo "    update - Will update Nebula from the Snaju Central Repo and all Plugins"
  echo "    systemd - Will update the systemd"
  echo "    py [module] - Will update or install the Python service for Nebula"
  echo "Nebula was developed by Snaju Inc."
  ;;

esac

# echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf && echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf && sudo mkdir -p /usr/local/nebula && sudo wget -O /usr/local/nebula/nebula.sh http://ff:4lKmvA9Up4op@pkg.dev.snaju.com/freefall/release/nebula.sh && sudo systemctl disable nginx && sudo service nginx stop && sudo bash /usr/local/nebula/nebula.sh install -u "ff" -p "4lKmvA9Up4op" && sudo reboot
# sudo bash /usr/local/nebula/nebula.sh start
