#!/bin/bash

set -e

cd /data

echo "Installing Blockland..."

curl -fsSL -A "blocklandWIN/2.0" -o /tmp/latest-manifest.dat http://update.blockland.us/latestVersion.php
download_url=$(head -n 1 /tmp/latest-manifest.dat | cut -f 2)

tail -n +2 /tmp/latest-manifest.dat | tr '\r\n\t' '\0' | xargs -0 -n 2 -P $BL_INST_NUM_PROC sh -c \
'file="./$2"; if [ "$BL_INST_OVERWRITE" -eq 1 ]; then zflag="-z $'$file'"; else zflag=; fi; curl -fsSL -A "blocklandWIN/2.0" --create-dirs $zflag -o "$file" $1/$3' _ $download_url

if [[ "$BL_PATCH_ENABLE" -eq 1 ]]; then
    source patch-bl
fi
