#!/bin/bash
#
# Run this script to provision a device as a Teleport Station device.
#
# Tested on Ubuntu 16.04 using SystemD.
#
# What this does:
# 1. Installs gphoto2 package along with wget and unzip packages
# 2. Installs app into /opt/teleportstation
# 3. Adds SystemD service to run the app on system startup and provide fault tolerance
#
# Uncomment this statement for debug echos
#DEBUG=1

APP_TARGET="prod"
TARGET_PLATFORM=linux
TARGET_ARCH="amd64" # arm64 | amd64, other architectures available please contact us
TARGET_DIR="/opt/teleportstation"
UPDATEPACKAGES=false
START_DIR=$(pwd)
PACKAGE_LIST="wget unzip gphoto2"

echo "Teleport Station provision script v1.2"

# Be sure we're running as root
if [[ $EUID != 0 ]] ; then
   echo "Must be root."
   exit 1
fi

if [ "$UPDATEPACKAGES" = "true" ] ; then
   echo "Checking for package updates.."
   apt-get update
   #apt-get upgrade -q -y
fi

echo "Installing required packages.. $PACKAGE_LIST"
apt install -y -q $PACKAGE_LIST
if [ "$?" -ne 0 ] ; then
   echo "Non essential package install failed. Please try this command manually:"
   echo "apt-get install -y $PACKAGE_LIST"
   exit 1
fi

echo "Sourcing latest Teleport Station version.."
mkdir -p $TARGET_DIR
cd $TARGET_DIR
wget -q https://teleport.blob.core.windows.net/apps/teleportstation/$TARGET_PLATFORM/$TARGET_ARCH/$APP_TARGET/latest-portable.zip -O teleportstation.zip
unzip -o teleportstation.zip
find . -type f ! -name "*.*" -exec chmod +x {} \;
find . -type f -name "*.sh" -exec chmod +x {} \;
# remove version config, by default we run the installed version
rm -f "ts.version.config"
echo "Installed Teleport Station to $TARGET_DIR"

# Stop is we're aleady running
systemctl stop tssvc

# Create a SystemD service file
cat > tssvc.service <<EOF
[Unit]
Description=Teleport Station service
After=network.target

[Service]
WorkingDirectory=/opt/teleportstation
ExecStart=/opt/teleportstation/teleportstation --norespawn
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# Copy service file to a System location
cp tssvc.service /lib/systemd/system

# Reload SystemD and enable the service, so it will restart on reboots
systemctl daemon-reload 
systemctl enable tssvc

RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Start service
systemctl start tssvc 
echo "${YELLOW}Teleport Station service installed successfully${NC}"

# View service status
echo "Service status"
systemctl status tssvc

echo -e "${YELLOW}Use './ts-follow-log.sh' to show tssvc log. This will show the Teleport Device Pair Key/Url required to provision this device.${NC}"

#Since we are just writing to stdout the output can be examined with journalctl
#journalctl --unit tssvc --follow

# # Stop service
# sudo systemctl stop tssvc 
# systemctl status tssvc 

# # Restart the service
# sudo systemctl start tssvc 
# systemctl status tssvc

# # Cleanup

# # Ensure service is stopped
# sudo systemctl stop tssvc 

# # Disable
# sudo systemctl disable tssvc 

# # Remove and reload SystemD
# sudo rm tssvc.service /lib/systemd/system/tssvc.service 
# sudo systemctl daemon-reload 

# # Verify SystemD is no longer aware of the service - Empty is what we want here
# systemctl --type service |& grep tssvc 