--- /dev/null
+#!/bin/bash
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+source "$DIR/config.sh"
+
+if [ -z $1 ]; then
+ exit
+fi
+
+sed -i '1s/.*/<?xml version="1.0" encoding="ISO-8859-1" ?>/' "${path_to_stats}"
+difficulties=(Challenge Edit Hard Medium)
+step_types=("dance-double" "dance-single")
+tiers=("1.00" "0.99" "0.98" "0.96" "0.94" "0.92" "0.89" "0.86" "0.83" "0.80" "0.76" "0.72" "0.68" "0.64" "0.60" "0.55")
+
+if [ "$1" = "push" ]; then
+ if find "${DIR}"/*.favourites.txt -type f -print -quit 2>/dev/null | grep -q .; then
+ while read -d $'\0' file; do
+ file=$(basename "$file")
+
+ IFS=$'\n' read -d '' -r -a lines < "${DIR}/$file"
+ person="${file%.*.*}"
+ if [ "$person" = "$me" ]; then
+ persons_folder="$faves_folder"
+ else
+ formatted_person="$(tr '[:lower:]' '[:upper:]' <<< ${person:0:1})${person:1}"
+ persons_folder=${external_favourite_naming_scheme/\%person\%/$formatted_person}
+ fi
+
+
+ for d in "${difficulties[@]}"; do
+ for t in "${step_types[@]}"; do
+ xmlstarletcmd="$xmlstarletcmd -o \"NEWLIST:$person:$t:$d\" -n \
+ -v '//SongScores//Song[starts-with(@Dir, \"Songs/$persons_folder\")]//Steps[@Difficulty=\"$d\" and @StepsType=\"$t\"]//HighScore[1]//Name[text()=\"EVNT\" or text()=\"$me_highscore_name\"]/following-sibling::PercentDP' \
+ -n -o \"SONGS\" -n \
+ -v '//SongScores//Song[starts-with(@Dir, \"Songs/$persons_folder\")]//Steps[@Difficulty=\"$d\" and @StepsType=\"$t\"]//HighScore[1]//Name[text()=\"EVNT\" or text()=\"$me_highscore_name\"]/ancestor::Song/@Dir'"
+ done
+ done
+ done < <(find "${DIR}"/*.favourites.txt -type f -print0)
+
+ scores_and_shit=$(eval "xmlstarlet sel -t $xmlstarletcmd -n -o \"NEWLIST\" ${path_to_stats}")
+ IFS=$'\n' read -rd '' -a scores_list <<<"$scores_and_shit"
+
+ echo "$me_highscore_name" > "${DIR}/$me.scores.txt"
+ declare -a scores
+ declare -a folders
+ for i in "${scores_list[@]}"; do
+ if [[ ${i:0:7} = "NEWLIST" ]]; then
+ for j in "${!scores[@]}"; do
+ write_out="${folders[$j]}:${d}:${t}:${scores[$j]}\n$write_out"
+ done
+
+ scores=()
+ folders=()
+ past_scores=false
+ p=$(echo $i | cut -sd : -f2)
+ t=$(echo $i | cut -sd : -f3)
+ d=$(echo $i | cut -sd : -f4)
+
+ continue
+ fi
+
+ if [ "$i" = "SONGS" ]; then
+ past_scores=true
+ continue
+ fi
+
+ if [ "$past_scores" = true ]; then
+ song_dir=$(echo $i | rev | cut -sd / -f2 | rev)
+ folders+=("${p}/${song_dir}")
+ else
+ scores+=("$i")
+ fi
+ done
+
+ echo -e "$write_out"
+ echo -e "$write_out" >> "${DIR}/$me.scores.txt"
+ scp "$DIR/$me.scores.txt" favesync@cameron1729.xyz:/mnt/media/FaveSync
+ fi
+fi
+
+if [ "$1" = "apply" ]; then
+ declare -A scores
+ declare -a all_scores
+ declare -a bros
+ declare -A pranks
+ xmlstarlet_deletes=""
+ if find "${DIR}"/*.scores.txt -type f -print -quit 2>/dev/null | grep -q .; then
+ while read -d $'\0' file; do
+ basename=$(basename "$file")
+ person="${basename%.*.*}"
+
+ if [ "$person" = "$me" ]; then
+ persons_folder="$faves_folder"
+ else
+ formatted_person="$(tr '[:lower:]' '[:upper:]' <<< ${person:0:1})${person:1}"
+ persons_folder=${external_favourite_naming_scheme/\%person\%/$formatted_person}
+ fi
+ song_dir="Songs/$persons_folder"
+ xmlstarlet_deletes="-d '//SongScores//Song[starts-with(@Dir, \"${song_dir}\")]' ${xmlstarlet_deletes}"
+
+ IFS=$'\n' read -d '' -r -a lines < $file
+ highscorename=${lines[0]}
+ bros+=("$highscorename")
+ for i in "${lines[@]:1}"; do
+ song_score=$(echo $i | rev | cut -sd : -f1 | rev)
+ song_steps_type=$(echo $i | rev | cut -sd : -f2 | rev)
+ song_difficulty=$(echo $i | rev | cut -sd : -f3 | rev)
+ song_path=$(echo $i | rev | cut -sd : -f4 | rev)
+ scores["${highscorename}:${song_path}:${song_difficulty}:${song_steps_type}"]+="${song_score}"
+ all_scores+=("${song_path}:${song_difficulty}:${song_steps_type}")
+ done
+ done < <(find "${DIR}"/*.scores.txt -type f -print0)
+ fi
+
+ readarray -t all_scores_unique < <(printf '%s\n' "${all_scores[@]}" | sort -u)
+ highest=""
+ for i in "${all_scores_unique[@]}"; do
+ for b in "${bros[@]}"; do
+ if [ -z "$highest" ]; then
+ highest="$b"
+ else
+ if [ -z "${scores[${b}:${i}]}" ]; then
+ scores["${b}:${i}"]+="0.000000"
+ fi
+
+ if [ -z "${scores[${highest}:${i}]}" ]; then
+ scores["${highest}:${i}"]+="0.000000"
+ fi
+
+ if [ 1 -eq "$(echo "${scores[${b}:${i}]} > ${scores[${highest}:${i}]}" | bc)" ]; then
+ highest="$b"
+ fi
+ fi
+ done
+
+ if ! [ "${scores[${highest}:${i}]}" = "0.000000" ]; then
+ pranks["${i}"]+="${highest}:${scores[${highest}:${i}]}"
+ fi
+ done
+
+ declare -A processed_pranks
+ new_nodes=""
+ for j in "${!pranks[@]}"; do
+ if [ -z "${processed_pranks[$j]}" ]; then
+ song_pranker=$(echo "${pranks[$j]}" | cut -sd : -f1)
+ song_score=$(echo "${pranks[$j]}" | cut -sd : -f2)
+ song_steps_type=$(echo $j | rev | cut -sd : -f1 | rev)
+ song_difficulty=$(echo $j | rev | cut -sd : -f2 | rev)
+ song_faver=$(echo $j | rev | cut -sd : -f3 | rev | cut -sd / -f1)
+ song_tier=1
+ for tier in "${tiers[@]}"; do
+ if [ 1 -eq "$(echo "$song_score < ${tier}" | bc)" ]; then
+ song_tier=$((song_tier+1))
+ fi
+ done
+ song_tier=$(printf "Tier%02d" $song_tier)
+
+ if [ "$song_faver" = "$me" ]; then
+ persons_folder="$faves_folder"
+ else
+ formatted_person="$(tr '[:lower:]' '[:upper:]' <<< ${song_faver:0:1})${song_faver:1}"
+ persons_folder=${external_favourite_naming_scheme/\%person\%/$formatted_person}
+ fi
+
+ song=$(echo $j | rev | cut -sd : -f3 | rev | cut -sd / -f2)
+ song_dir="Songs/${persons_folder}/${song}/"
+ new_nodes="$new_nodes\n<Song Dir=\"$song_dir\" >\
+\n<Steps Difficulty=\"$song_difficulty\" StepsType=\"dance-single\" >\
+\n<HighScoreList>\
+\n<HighScore>\
+\n<Name>$song_pranker</Name>\
+\n<Grade>$song_tier</Grade>\
+\n<PercentDP>$song_score</PercentDP>\
+\n<Score>42069</Score>\
+\n</HighScore>\
+\n</HighScoreList>\
+\n</Steps>"
+
+ processed_pranks[$j]+="pranked"
+
+ echo "Song : $song_dir"
+ echo "Difficulty : $song_difficulty"
+ echo "Steps type : $song_steps_type"
+ echo "Pranker : $song_pranker"
+ echo "Score : $song_score"
+ echo "Tier : $song_tier"
+ echo -e "\n"
+
+ for d in "${difficulties[@]}"; do
+ for t in "${step_types[@]}"; do
+ # there are pranks for other difficulties of this chart
+ # we need to process them now because they need to live in the
+ # same song node
+ if ! [ -z "${pranks[${song_faver}/${song}:${d}:${t}]}" ]; then
+ if ! [ "$d" = "$song_difficulty" ] || ! [ "$t" = "$song_steps_type" ]; then
+ whatever="${pranks[${song_faver}/${song}:${d}:${t}]}"
+ ssong_pranker=$(echo $whatever | cut -sd : -f1)
+ ssong_score=$(echo $whatever | cut -sd : -f2)
+ ssong_tier=1
+ for tier in "${tiers[@]}"; do
+ if [ 1 -eq "$(echo "$ssong_score < ${tier}" | bc)" ]; then
+ ssong_tier=$((ssong_tier+1))
+ fi
+ done
+ ssong_tier=$(printf "Tier%02d" $ssong_tier)
+
+ processed_pranks["${song_faver}/${song}:${d}:${t}"]+="pranked"
+ new_nodes="$new_nodes\n<Steps Difficulty=\"${d}\" StepsType=\"${t}\" >\
+\n<HighScoreList>\
+\n<HighScore>\
+\n<Name>$ssong_pranker</Name>\
+\n<Grade>$ssong_tier</Grade>\
+\n<PercentDP>$ssong_score</PercentDP>\
+\n<Score>42069</Score>\
+\n</HighScore>\
+\n</HighScoreList>\
+\n</Steps>"
+
+ echo "Song : $song_dir"
+ echo "Difficulty : $d"
+ echo "Steps type : $t"
+ echo "Pranker : $ssong_pranker"
+ echo "Score : $ssong_score"
+ echo "Tier : $ssong_tier"
+ echo -e "\n"
+ fi
+ fi
+ done
+ done
+
+ new_nodes="$new_nodes\n</Song>"
+ fi
+ done
+
+ # blow away all existing nodes in the favourites folders, we will make them ourselves and append to the end of the file
+ cmd="xmlstarlet ed -L ${xmlstarlet_deletes} \"${path_to_stats}\""
+ $(eval "$cmd")
+
+ last_tags_removed=$(head -n -2 "${path_to_stats}")
+ echo -e "${last_tags_removed}${new_nodes}\n</SongScores>\n</Stats>" > "${path_to_stats}"
+fi
+
+
+exit
+if [ "$1" = "push" ]; then
+ if find "${DIR}"/*.favourites.txt -type f -print -quit 2>/dev/null | grep -q .; then
+ echo "$me_highscore_name" > "${DIR}/$me.scores.txt"
+ find "${DIR}"/*.favourites.txt -type f -print0 | while read -d $'\0' file; do
+ file=$(basename "$file")
+
+ IFS=$'\n' read -d '' -r -a lines < "${DIR}/$file"
+ unmodded_person="${file%.*.*}"
+ person="${file%.*.*}"
+ if [ "$person" = "$me" ]; then
+ persons_folder="$faves_folder"
+ else
+ person="$(tr '[:lower:]' '[:upper:]' <<< ${person:0:1})${person:1}"
+ persons_folder=${external_favourite_naming_scheme/\%person\%/$person}
+ fi
+
+ xpaths=""
+ for i in "${lines[@]}"; do
+ just_song=$(echo $i | cut -sd / -f2)
+ just_song=${just_song/\`/\\\`}
+ song_dir="Songs/$persons_folder/$just_song/"
+
+ for d in "${difficulties[@]}"; do
+ my_score_xpath='(//SongScores//Song[@Dir=\"'"$song_dir"'\"]//Steps[@Difficulty=\"'"$d"'\" and @StepsType=\"dance-single\"]//Name[text()=\"EVNT\" or text()=\"'"$me_highscore_name"'\"]/following-sibling::PercentDP)[1]'
+ highest_score_name_xpath='(//SongScores//Song[@Dir=\"'"$song_dir"'\"]//Steps[@Difficulty=\"'"$d"'\" and @StepsType=\"dance-single\"]//Name)[1]'
+ xpaths="-o \"$unmodded_person/$just_song:\" -v \"$my_score_xpath\" -o \":\" -v \"$highest_score_name_xpath\" -o \":$d\" -n $xpaths"
+ done
+ done
+
+ cmd="xmlstarlet sel -T -t $xpaths \"${path_to_stats}\""
+ result=$(eval "$cmd")
+
+ IFS=$'\n' read -d '' -r -a array <<< "$result"
+ for i in "${array[@]}"; do
+ echo "$i" >> "${DIR}/$me.scores.txt"
+ done
+ done
+ fi
+ scp "$DIR/$me.scores.txt" favesync@cameron1729.xyz:/mnt/media/FaveSync
+fi
+
+if [ "$1" = "pull" ]; then
+ scp favesync@cameron1729.xyz:/mnt/media/FaveSync/* "$DIR"
+fi
+
+sed -i '1s/.*/<?xml version="1.0" encoding="UTF-8" ?>/' "${path_to_stats}"