#!/usr/bin/env bash

set -e

if [[ "$#" -ne 0 && "$#" -ne 2 ]]; then
    echo 'Install a portable version of NSIS which can build the OpenRCT2 Windows installer.'
    echo ''
    echo 'Usage: install-nsis [-d <destination>]'
    echo ''
    echo '    -d <destination>      download prebuilt zip'
    exit 1
fi

if [[ "$1" == "-d" ]]; then
    echo -e "\033[0;36mDownloading prebuilt NSIS from GitHub...\033[0m"
    curl -sSfLO "https://github.com/OpenRCT2/Dependencies/releases/download/v33/nsis-3.08.zip"
    7z -bd -y "-o$2" x nsis-3.08.zip
    rm nsis-3.08.zip
    echo -e "\033[0;32mNSIS downloaded, add "$2/bin" to PATH\033[0m"
    exit 0
fi

# Download NSIS with chocolatey and then download extra plugins
nsisdir="$ProgramData/chocolatey/lib/nsis.portable"
if [[ -d $nsisdir ]]
then
    echo -e "\033[0;36mNSIS already installed.\033[0m"
    exit 0
fi

echo -e "\033[0;36mDownloading NSIS from chocolatey...\033[0m"
cinst nsis.portable --version=3.08

echo -e "\033[0;36mDownloading KillProcDLL for NSIS...\033[0m"
curl -sSfLo nsisxtra.zip "http://nsis.sourceforge.net/mediawiki/images/5/53/KillProcDll%26FindProcDll.zip"
7z x nsisxtra.zip
cp FindProcDLL.dll "$nsisdir/tools/nsis-3.08/Plugins/x86-ansi"

echo -e "\033[0;36mDownloading UAC plugin for NSIS...\033[0m"
curl -sSfLO "http://nsis.sourceforge.net/mediawiki/images/8/8f/UAC.zip"
7z x UAC.zip
cp UAC.nsh "$nsisdir/tools/nsis-3.08/Include"
cp -r Plugins "$nsisdir/tools/nsis-3.08"

echo -e "\033[0;32mNSIS installed!\033[0m"
