## Script Configuration
download_url_stable=https://install.portrait.app/stable.txt
## No changes required from here on

function checkUnzip {
  if ! command -v unzip &> /dev/null
  then
    echo "This installer requires 'unzip'. Install it please, and then run this installer again."
    exit
  fi
}


function checkDocker {
  if ! command -v docker &> /dev/null
  then
    echo "This installer requires 'docker'. Install it please, and then run this installer again."
    exit
  fi
}

function checkDockerCompose {
  if ! command -v docker-compose &> /dev/null
  then
    echo "This installer requires 'docker-compose'. Install it please, and then run this installer again."
    exit
  fi
}

function downloadLatestVersion {
  curl $download_url_stable --output requestedVersion.txt
  requestedVersion=$(head -n 1 requestedVersion.txt)
  curl "$requestedVersion" --output portrait-installer.zip
  mkdir $1
  unzip portrait-installer.zip -d $1
  rm portrait-installer.zip
  chmod -R 755 $1
}

# check prerequisites
checkUnzip
checkDocker
checkDockerCompose

# configuration
read -p "Enter the installation path (default: ./portrait-app): "  installLocation
installLocation=${installLocation:-'portrait-app'}

# intro
echo ""
echo "##############################"
echo "Downloading latest version..."
echo "##############################"

downloadLatestVersion "$installLocation"

echo ""
echo "##############################"
echo "Starting installation..."
echo "##############################"

chmod a+x "$installLocation/setup.sh"
cd "$installLocation" || exit
./setup.sh