Created
October 8, 2019 12:24
-
-
Save eliask/3804b01a2000b9384b8cbce12f66e70a to your computer and use it in GitHub Desktop.
Fix Stepmania/ITG chart BGCHANGES videos/images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# Fix simple .SM and .SSC chart mistakes when the first entry of #BGCHANGES | |
# points to a file that doesn't exist and when there's another file of the | |
# same extension in the song directory. | |
# | |
# E.g. Ben Speirs' SPEIRMIX GALAXY has a lot of these mismatching filenames. | |
find -type f \ | |
\( -iname '*.sm' -or -iname '*.ssc' \) \ | |
-exec grep -l '#BGCHANGES:[^;]' {} + | | |
while read -r chart; do | |
dir=$(dirname "$chart") | |
orig_bg=$(grep -o '#BGCHANGES:[-0-9.]*=[^=]*' "$chart") | |
avi=$(echo "$orig_bg" | cut -d= -f2) | |
ext=$(echo "$avi" | sed 's/.*\.//') | |
[[ -z $orig_bg ]] && { echo "NO BG: $chart"; break; } | |
[[ -z $avi ]] && { echo "NO AVI: $chart / $orig_bg"; break; } | |
cur_file=$dir/$avi | |
[[ -f $cur_file ]] && continue | |
globbed=$(echo "$dir"/*."$ext") | |
[[ -f $globbed ]] || { echo "No matching file extension: $chart / $globbed"; continue; } | |
new_file=$(basename "$globbed") | |
if grep -q '[,=]' <<< "$new_file"; then | |
echo "INVALID FILENAME (',' and '=' forbidden): $dir / $cur_file / $globbed" | |
continue | |
fi | |
[[ $globbed != $cur_file ]] || continue | |
new_bg="$(echo "$orig_bg" | cut -d= -f1)=$new_file" | |
replace_bg() { | |
python -c ' | |
import sys;_,old,new=sys.argv | |
for line in sys.stdin:print(line.replace(old,new)) | |
' "$orig_bg" "$new_bg" < "$1" | |
} | |
# Sanity checks: | |
new_new_res=$(replace_bg "$chart" | grep '#BGCHANGES:') | |
new_new_bg=$(echo "$new_new_res" | grep -o '#BGCHANGES:[-0-9.]*=[^=]*') | |
new_avi=$(echo "$new_new_bg" | cut -d= -f2) | |
[[ $new_avi == $avi ]] && { | |
echo "Substitution failure! $chart" | |
echo $globbed != $cur_file | |
break | |
} | |
[[ -f $dir/$new_avi ]] || { | |
echo "FATAL: New file somehow doesn't work!" | |
echo "old: $dir/$avi" | |
echo "new: $dir/$new_avi" | |
echo "was supposed to be: $globbed" | |
break | |
} | |
replace_bg "$chart" > "$chart.new" | |
cmp "$chart" "$chart.new" || { echo "Chart didn't change: $chart.new"; break; } | |
cp -a "$chart" "$chart.bak" | |
mv "$chart.new" "$chart" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment