#!/bin/bash

set -eu

PACKAGE="http://feeds.amco.gr:8080/temp/hcdb-setup-script.tar.gz"
QT_INSTALLER="http://feeds.amco.gr:8080/tools/qt-opensource-linux-x64-5.7.1.run"
STAGE_PATH="./hcdb-stage"
INSTALL_PATH="$STAGE_PATH/script"

download() {
    local url=$1
    local path=$2
    local filename="$path/$(basename "$url")"

    if [ ! -f "$filename" ]; then
        echo "Downloading : $url"
        mkdir -p "$path"
        wget -O "$filename" "$url"
    else
        echo "Already present: $filename"
    fi
}

mkdir -p "$STAGE_PATH"
mkdir -p "$INSTALL_PATH"
download "$QT_INSTALLER" "$INSTALL_PATH"
download "$PACKAGE" "$STAGE_PATH"

# Extract
file="$STAGE_PATH/$(basename $PACKAGE)"
echo "Extracting : $file"
tar -xzf "$file" -C "$INSTALL_PATH"

# Execute
pushd $INSTALL_PATH
./install.sh
popd

echo "Cleaning up"
rm -rf $STAGE_PATH
echo "DONE - Please reboot"
