#!/usr/bin/env sh
set -e
# check for params
brand=grow 
chain=grow
domain=growblockchain.net
env=prod
version=v2.6.1-b
while getopts b:e:a:c:v:d: flag
do
    case "${flag}" in
        b) brand=${OPTARG};;
        c) chain=${chain};;
        e) env=${OPTARG};;
        a) arch=${OPTARG};;
        c) cmd=${OPTARG};;
        v) version=${OPTARG};;
        d) domain=${OPTARG};;
    esac
done
if [[ -z "$brand" ]]; then
    echo "ERR: brand variable required, use '-b {BRAND}' and optionally add an environment variable '-e {ENV}'"
    exit
fi
if [[ -z "$chain" ]]; then
    echo "ERR: chain variable required, use '-c {CHAIN}'"
    exit
fi

chain=$(echo "$chain" | tr '[:upper:]' '[:lower:]')
name="$chain-$env"
log="debug"
if [[ -z "$env" || ($env != "dev" && $env != "stage") ]]; then
    env="prod"
    name="$chain"
    log="info"
fi

type="amd64"
if [[ $arch == "" ]]; then
    arch=$(uname -m)
fi
if [[ $arch == "arm64" ]]; then
    type=$arch
fi

if [[ $cmd != "" ]]; then
    # the only command option for now
    cmd="gui"
fi

base_url="https://download.${domain}"
if [ "$env" != "prod" ]; then
    base_url="https://download.${env}.${domain}"
fi

base_url=$(sed 's/.prod//' <<< $base_url)

date=$(date +%s)
download_url="${base_url}/node-binaries/${version}/${chain}-${version}_darwin-$type?$date"
echo "brand=$brand"
echo "chain=$chain"
echo "env=$env"
echo "arch=$arch"
echo "version=$version"
echo "domain=$domain"
echo "download_url=$download_url"
echo "name=$name"

folder="$HOME/Library/Nodes"
node="$folder/$name"
# download and config node
curl $download_url -o $node --create-dirs
ls $node
chmod +x $node
$node config
# install node as a service
args="<string>$node</string>"
if [[ $cmd != "" ]]; then
    args="<string>$node</string><string>$cmd</string>"
fi
agent="$HOME/Library/LaunchAgents"
service="$agent/$name.plist"
mkdir -p $agent
cat > $service <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>EnvironmentVariables</key>
    <dict>
           <key>NODE_LOG_LEVEL</key>
           <string>$log</string>
    </dict>
    <key>Label</key>
    <string>$name</string>
    <key>ServiceDescription</key>
    <string>$name service manager</string>
    <key>ProgramArguments</key>
    <array>
        $args
    </array>
    <key>StandardErrorPath</key>
    <string>$folder/.$name.out</string>
    <key>StandardOutPath</key>
    <string>$folder/.$name.out</string>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <true/>
        <key>NetworkState</key>
		<true/>
    </dict>
</dict>
</plist>
EOF
# start servie
launchctl bootstrap gui/$UID $service
printf "At any time, use the following command to view your node's status: \n    launchctl print gui/\$UID/$name | grep 'active count'"
printf "\nUse the following command to follow the node's log output: \n    tail -f ~/Library/Nodes/.$name.out"
# desktop shortcut
if [[ $cmd == "gui" ]]; then
cat > $HOME/Desktop/$name-node.url <<EOF
[InternetShortcut]
URL=https://localhost:9420/Dashboard/
IconIndex=0
EOF
fi