#!/bin/bash
if dpkg-query -W -f='${Status}' nginx 2>/dev/null | grep -q "ok installed"; then
  echo "Package nginx is installed"
else
  apt-get update
  apt install nginx
  echo "Package nginx has been installed"
fi
WORKING_DIR="/home/pi/test"
rm -rf $WORKING_DIR
mkdir $WORKING_DIR
echo "Directory $WORKING_DIR created!"
wget http://files.domcore.pl/configurator/arm64/internal-api.tar.gz -P $WORKING_DIR
echo "internal-api downloaded"
tar -xvzf $WORKING_DIR/internal-api.tar.gz -C /var/www/
echo "internal-api files moved"
chmod 777 /var/www/internal-api
chmod +x /var/www/internal-api/Imby.Configurator.InternalAPI.Host
echo "Added execution access"
wget http://files.domcore.pl/configurator/arm64/internal-api.service -P /etc/systemd/system/
echo "Added cronjob service"
wget http://files.domcore.pl/configurator/configurator-web-app.tar.gz -P $WORKING_DIR
echo "configurator-web-app downloaded"
tar -xvzf $WORKING_DIR/configurator-web-app.tar.gz -C /var/www/
echo "configurator-web-app files moved"
chmod 777 /var/www/configurator-web-app
wget http://files.domcore.pl/configurator/configurator-web-app.service -P /etc/systemd/system/
echo "Added cronjob service"
rm /etc/nginx/sites-available/default
wget http://files.domcore.pl/configurator/default -P /etc/nginx/sites-available/
ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
systemctl daemon-reload
systemctl enable internal-api.service
systemctl enable configurator-web-app.service
echo "restart and enable app"
systemctl start internal-api.service
systemctl start configurator-web-app.service
systemctl reload nginx
sed -i 's/] }/,{"destination": "http:\/\/127.0.0.1:80"}] }/g' /etc/socketxp/config.json
rm /home/pi/updateApp.sh
wget http://files.domcore.pl/configurator/updateApp.sh -P /home/pi/
chmod +x /home/pi/updateApp.sh
chown pi:pi /home/pi/updateApp.sh
rm -rf $WORKING_DIR
systemctl restart socketxp

