#!/bin/bash
set -e

VERSION=0.3
URL_VERSION="https://raw.githubusercontent.com/home-assistant/hassio/master/version.json"
URL_GENERIC_HC="https://raw.githubusercontent.com/home-assistant/hassio-build/master/generic-hc"

# Check system options
if [ -x $(command -v systemctl) ]; then
    INIT_SYSTEM=systemd
else
    INIT_SYSTEM=sysvinit
fi

#
# MAIN loop
#
while read cmd
do
    IFS=" " read -r -a parse <<< $cmd

    if [ ${parse[0]} == "info" ]; then
        HOSTNAME=$(hostname)
        OS=$(uname -o)
        LAST=$(curl -s $URL_VERSION | jq -e -r '.generic')
        FEATURES="[\"shutdown\", \"reboot\", \"update\"]"

        echo "{ \"type\": \"generic\", \"version\": \"$VERSION\", \"last_version\": \"$LAST\", \"hostname\": \"$HOSTNAME\", \"features\": $FEATURES, \"os\": \"$OS\" }"
        continue
    fi
    if [ ${parse[0]} == "reboot" ]; then
        if [ "$INIT_SYSTEM" == "systemd"]; then
            systemctl reboot && echo "OK" || echo "ERROR"
        else
            nohup reboot > /dev/null 2>&1 && echo "OK" || echo "ERROR"
        fi
        continue
    fi
    if [ ${parse[0]} == "shutdown" ]; then
        if [ "$INIT_SYSTEM" == "systemd"]; then
            systemctl poweroff && echo "OK" || echo "ERROR"
        else
            nohup halt -p > /dev/null 2>&1 && echo "OK" || echo "ERROR"
        fi
        continue
    fi
    if [ ${parse[0]} == "update" ]; then
        if [ ! -z ${parse[1]} ]; then
            VERSION=${parse[1]}
        else
            VERSION=$(curl -s $URL_VERSION | jq -e -r '.generic')
        fi
        CMD_BIN=$(command -v hassio-hc)

        curl -sL "$URL_GENERIC_HC/$VERSION" > "$CMD_BIN"
        if [ $? -eq 0 ]; then
            chmod a+x "$CMD_BIN"
            echo "OK"

            # restart service
            if [ "$INIT_SYSTEM" == "systemd" ]; then
                nohup systemctl restart hassio-hc.service > /dev/null 2>&1
            fi
        else
            echo "ERROR"
        fi
        continue
    fi

    echo "WRONG"
done
