#
#  install - Mycodo install script
#
#  Do not execute this script if the Mycodo archive has been downloaded and extracted.
#  If Mycodo has been extracted (/opt/Mycodo/ already exists), then execute:
#
#  sudo /bin/bash /opt/Mycodo/install/setup.sh
#
#
#  If Mycodo has not yet been downloaded/extracted, execute the following to install:
#
#  curl -L https://kizniche.github.io/Mycodo/install | bash
#

INSTALL_DIRECTORY=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )

if [ "$EUID" -eq 0 ]; then
    printf "Do not run as root. Run as non-root user: \"/bin/bash %s/install\"\n" "${INSTALL_DIRECTORY}"
    exit 1
fi

if [ -d /opt/Mycodo ]; then
  printf "## Error: Install aborted. Cause: The /opt/Mycodo directory already exists. The install cannot continue because a previous Mycodo install was detected. Please either move or delete the /opt/Mycodo directory and rerun this script to initiate the install or run /opt/Mycodo/install/setup.sh.\n"
  exit 1
fi

# Install dependencies
sudo apt update
sudo apt install -y jq dialog python3 wget

# Create install directory
sudo mkdir -p /opt/Mycodo
sudo chown -R ${USER}: /opt/Mycodo
cd /opt

# Download and install
curl -s https://api.github.com/repos/kizniche/Mycodo/releases/latest | \
jq -r '.tarball_url' | sudo wget -i - -O mycodo-latest.tar.gz
sudo tar xzf mycodo-latest.tar.gz -C Mycodo --strip-components=1
sudo rm -f mycodo-latest.tar.gz
cd Mycodo/install
sudo /bin/bash ./setup.sh
