#!/bin/mksh # -*- mode: sh -*- #- # Copyright © 2021 # mirabilos # # Provided that these terms and disclaimer and all copyright notices # are retained or reproduced in an accompanying document, permission # is granted to deal in this work without restriction, including un‐ # limited rights to use, publicly perform, distribute, sell, modify, # merge, give away, or sublicence. # # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to # the utmost extent permitted by applicable law, neither express nor # implied; without malicious intent or gross negligence. In no event # may a licensor, author or contributor be held liable for indirect, # direct, other damage, loss, or other issues arising in any way out # of dealing in the work, even if advised of the possibility of such # damage or existence of a defect, except proven that it results out # of said person’s immediate fault when using the work as intended. #- # Fixup syllabics in all .mscx files. For more info see the comments # around: https://musescore.org/en/node/321581#comment-1081503 unset LANGUAGE export LC_ALL=C set -e set -o pipefail nl=$'\n' function doone { set -A lst1 set -A lst2 set -A lst3 set -A lst4 curno=0 curs= curv=1 lbeg= lend= nameref lst=lst$curv while IFS= read -r line; do l=${line##*([ ])} case $l { (' ]*)) set -A lst1 set -A lst2 set -A lst3 set -A lst4 curno=0 curs= curv=1 nameref lst=lst$curv ;; (' ]*)) curv=1 nameref lst=lst$curv ;; (''*|'') let ++curv nameref lst=lst$curv ;; (' ]*)) curno=0 curs= lbeg=$line ;; (''+([0-9])'') curno=${l#''} curno=${curno%''} lbeg+=$nl$line ;; (' ]*)) print -ru2 -- "E: $line" exit 1 ;; ('begin') curs=b ;; ('middle') curs=m ;; ('end') curs=e ;; (' ]*)) print -ru2 -- "E: $line" exit 1 ;; ('') lend+=$nl$line line=${line%'<'*} os=${lst[10#$curno]} case $os:$curs { (b:b) curs=m ;; (b:) curs=e ;; (m:b) curs=m ;; (m:) curs=e ;; (e:m) curs=b ;; (e:e) curs= ;; } case $curs { (b) lbeg+=$nl$line'begin' ;; (m) lbeg+=$nl$line'middle' ;; (e) lbeg+=$nl$line'end' ;; } lst[10#$curno]=$curs line=$lbeg$lend lbeg= lend= ;; (*) [[ -z $lbeg ]] || lend+=$nl$line ;; } [[ -n $lbeg ]] || print -r -- "$line" done } function dofile { local fn=$1 print -r -- "I: $fn" doone <"$fn" >"${fn}~" mv "${fn}~" "$fn" } if (( $# )); then for f in "$@"; do dofile "$f" done exit fi cd "$(realpath "$0/../../..")" find music/ -name \*.mscx -print0 | sort -z | while IFS= read -r -d '' fn; do dofile "$fn" done