#!/bin/bash

echo "--- Getting executable path"
EXECPATH="`dirname \"$0\"`"              # relative
EXECPATH="`( cd \"$EXECPATH\" && pwd )`"  # absolutized and normalized
if [ -z "$EXECPATH" ] ; then
  # error; for some reason, the path is not accessible
  # to the script (e.g. permissions re-evaled after suid)
  exit 1  # fail
fi
echo "$EXECPATH"
TOREPLACE="PATH_TO_EXECUTABLE"


echo "--- Updating packages"
sudo apt --yes update
sudo apt --yes upgrade

echo "--- Installing packages via apt"
# needed for the CosmicPi software
sudo apt --yes install git python-pip htop
# needed for create_ap
sudo apt --yes install git util-linux procps hostapd iproute2 iw haveged dnsmasq

echo "--- Installing python packages via pip"
sudo pip install pyserial configparser numpy matplotlib flask flask_googlemaps Flask-BasicAuth
echo "we might need some more packages"

echo "--- Installing create_ap via git"
git clone https://github.com/oblique/create_ap --depth 1
cd create_ap
sudo make install
cd ..
sudo rm -rf create_ap
# copy the config file
sudo cp -f install_files/create_ap.conf /etc/create_ap.conf


echo "--- Setting up systemd services"
sed -i -e "s+$TOREPLACE+$EXECPATH+g" install_files/CosmicPi-detector.service
sed -i -e "s+$TOREPLACE+$EXECPATH+g" install_files/CosmicPi-UI.service
sudo cp -f install_files/*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable CosmicPi-detector.service
sudo systemctl enable CosmicPi-UI.service
sudo systemctl enable create_ap

echo "--- Finished setup!"

