#!/bin/bash
theFile="${1}"
if [[ -z "${3}" ]]; then
  echo "must enter file, trophy text, and trophy item"
  echo "$0 file.txt 'Is this a tardus?' 'mekanismtools:obsidianhoe:1:{ench: [{lvl: 1, id: 12}], Size: 9}'"
  exit 1
fi
if [[ ! -f "${theFile}" ]]; then
  echo "File ${theFile} does not exist"
  exit 1
fi
if ( grep -q 'addRewardItem.*Trophy' "${theFile}" 2>/dev/null ); then
  echo "Trophy already exists in file. Remove trophy first"
  exit 1
fi
name="${2}"
id=$(echo "${3}"|awk -F':' '{print $1":"$2}')
damage=$(echo "${3}"|awk -F':' '{print $3}')
tempstring="${id}:${damage}"
nbt=$(echo "${3}"|sed -e 's@'"${tempstring}"':@@' -e 's@'"${tempstring}"'@@' -e 's@'"${id}"'@@')
if [[ -z "${nbt}" ]]; then
  nbt="{}"
fi
if [[ -z "${damage}" ]]; then
  damage=0
fi
echo 'addRewardItem(<simple_trophies:trophy, nbt:{TrophyName: "85 - '"${name}"'", TrophyColorGreen: 0, TrophyVariant: "neon", TrophyShowsTooltip: 1, TrophyItem: {id: "'"${id}"'", Count: 1, tag: '"${nbt}"', Damage: '"${damage}"'}, TrophyColorBlue: 102, TrophyColorRed: 255}>)' >> "${theFile}"
../organize "${theFile}"
