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

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

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