#!/usr/bin/env bash

# USAGE: bash setup-mappings.sh <version>

mc_version_manifest='https://launchermeta.mojang.com/mc/game/version_manifest.json'

# check dependencies. this script requires jq
if [ $(dpkg-query -W -f='${Status}' jq 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
	echo "'jq' is not installed, run 'sudo apt-get install jq' to use this script"
	exit
fi

# find our version.json file
echo 'Reading version_manifest.json...'
json_file=$(curl -s $mc_version_manifest | jq -r --arg version $1 '.versions[] | select(.id == $version).url')

# find jar and mappings url
echo "Reading $1.json..."
json_file_contents=$(curl -s $json_file)
client_jar=$(echo $json_file_contents | jq -r '.downloads.client.url')
client_mappings=$(echo $json_file_contents | jq -r '.downloads.client_mappings.url')

# download client.jar
echo "Downloading client.jar..."
curl -# -o client.jar $client_jar

# download client.txt
echo "Downloading ProGuard mappings: client.txt..."
curl -# -o client.txt $client_mappings

# delete old mappings
rm -rf client.mappings

echo "Converting ProGuard mappings to Engima mappings: client.mappings..."
./enigma.sh convert-mappings proguard client.txt enigma client.mappings
