#!/usr/bin/bash

read -p "Start BlueCherry IoT installation? (y/n) " resp

if [ "$EUID" -ne 0 ]; then
    echo "This install script must run with root privileges"
    exit 1;
fi

case $resp in
    yes ) ;;
    y ) ;;
    no ) exit 0;;
    n ) exit 0;;
    * ) exit 1;
esac

echo "Downloading installation files"
wget https://pubfiles.dptechnics.com/zmpr/zemperdata.tar.gz

echo "Checking installation file validity"
md5=$(md5sum zemperdata.tar.gz | cut -f1 -d ' ')

if [ "$md5" != "cb97bcd2347fd0bc14234d840bf2ba71" ]; then
    echo "The installation files are corrupted, please re-copy the files"
    exit 2
fi

if ! command -v nginx &> /dev/null
then
    echo "Installing nginx proxy..."
    apt-get -y update
    apt-get -y install nginx
    if [ $? -ne 0 ]; then
        echo "KO"
        exit 3;
    else
        echo "OK"
    fi
fi

echo -n "Unpacking installation files..."
dir=$(mktemp -d)
tar -xzf zemperdata.tar.gz -C $dir >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 4;
else
    echo "OK"
fi

echo -n "Creating installation directories..."
mkdir -p /etc/bluecherry >/dev/null 2>&1
chmod 777 /etc/bluecherry
if [ $? -ne 0 ]; then
    echo "KO"
    exit 5;
else
    echo "OK"
fi

echo -n "Copying files..."

cp "$dir/default" /etc/nginx/sites-available/default >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    echo "Could not copy default nginx config"
    exit 6;
fi

cp "$dir/bluecherry-client.service" /etc/systemd/system/bluecherry-client.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    echo "Could not copy bluecherry-client.service"
    exit 7;
fi

cp "$dir/ttyd.service" /etc/systemd/system/ttyd.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    echo "Could not copy ttyd.service"
    exit 8;
fi

cp "$dir/bluecherry-client" /usr/sbin/bluecherry-client >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    echo "Could not copy bluecherry-client binary"
    exit 9;
fi

if ! command -v ttyd &> /dev/null
then
    cp "$dir/ttyd.arm" /usr/sbin/ttyd >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "KO"
        echo "Could not copy ttyd binary"
        exit 10;
    fi
fi

cp "$dir/config.yaml" /etc/bluecherry/config.yaml >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    echo "Could not copy bluecherry config.yaml"
    exit 10;
fi

echo "OK"

echo -n "Enabling and reloading services..."
chmod 644 /etc/systemd/system/bluecherry-client.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 11;
fi

chmod 644 /etc/systemd/system/ttyd.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 12;
fi

systemctl daemon-reload
if [ $? -ne 0 ]; then
    echo "KO"
    exit 13;
fi

systemctl enable bluecherry-client.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 14;
fi

systemctl enable ttyd.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 15;
fi

systemctl start ttyd.service >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "KO"
    exit 16;
fi

service nginx restart
service bluecherry-client start

echo "OK"

rm -r $dir

echo "Installation is complete, please wait until the device comes online on"
echo "the BlueCherry.io IoT platform."
