# Warning: Save as UTF-8 (no BOM) # Purpose: update the :revision: in a *.mscx file using file modification date # Format: yyyyμR(m)dd OR yyyyμR(m)dd hh:mm # BEGIN { revisionMonth[01]="①" revisionMonth[02]="②" revisionMonth[03]="③" revisionMonth[04]="④" revisionMonth[05]="⑤" revisionMonth[06]="⑥" revisionMonth[07]="⑦" revisionMonth[08]="⑧" revisionMonth[09]="⑨" revisionMonth[10]="⑩" revisionMonth[11]="⑪" revisionMonth[12]="⑫" } function getTag() { tag = gensub(/^ *<([a-zA-Z]+).*/,"\\1","g",$0) t = $0 # may not be on the same line - read as many as needed while ((index(t,"") == 0) && (match(t,"/> *$") == 0)) { if (getline > 0) t = t "\n" $0; else break; } # t = gensub(/^( *)(.*)/,"\\1","g",t) return t } function getMetaTagIndent() { return gensub(/^([ \t]+).*/,"\\1","g",$0) } function getMetaTagContent() { return gensub(/^.*)(.*)<\/metaTag>.*/,"\\1","g",t) } function getEmptyMetaTag(name) { return metaTagIndent "" } function getRevisionDate() { currentLine = $0 if (FILENAME == "-") { # use current time/date if used with pipeline or redirection $0 = strftime("%Y-%m-%d %H:%M:%S",systime()) } else { # get file date and time (last modified) # ls returns following columns $1=attributes $2=position $3=size $4=date $5=time $6...=file-name # $4 (date) = yyyy-mm-dd # $5 (time) = hh:mm cmd = "ls -o -g --time-style=long-iso \"" FILENAME "\"" cmd | getline close(cmd) # Shift the date/time to the front ($1 = $4; $2 = $5) $0 = $4 " " $5 } # construct and print the revision string # metaTag name revision = metaTagIndent "" # add date: "yyyyμR(m)dd" revision = revision substr($1,1,4) "μR" revisionMonth[substr($1,6,2)+0] substr($1,9,2) # add time: " hh:mmm" # revision = revision " " $2 # close metaTag revision = revision "" $0 = currentLine return revision } # -- start scanning the file: pass == 1 check for (FNR == 1) { metaTagFound = 0 metaTagFoundComposer = 0 metaTagFoundMixerType = 0 metaTagFoundMovementTitle = 0 metaTagFoundRevision = 0 metaTagFoundSource = 0 metaTagFoundTune = 0 metaTagFoundWorkTitle = 0 metaTagIndent = "" metaTagSource = "" } # delete some metaTags /^ */ { metaTagIndent = getMetaTagIndent() metaTagFound = 1 getTag() # delete = don't print and go to the NEXT line next } # update "revision" before in place /^ * 0) { metaTagFoundComposer = 1 } if (index($0, " 0) { metaTagFoundMixerType = 1 } if (match($0, " 0) { metaTagFoundMovementTitle = 1 } if (index($0, " 0) { metaTagSource = gensub(/(.*)/, "\\1+\\2","g",$0) } if (index($0, " 0) { metaTagFoundSource = 1 } if (index($0, " 0) { metaTagFoundTune = 1 } if (index($0, " 0) { metaTagFoundWorkTitle = 1 } if (index($0, " 0) { metaTagFoundRevision = 1 $0 = getRevisionDate() } print # after printing go skip the rest of this script and fetch the NEXT line of text next } # add missing metaTags after all metaTags if it was missing (metaTagFound == 1) && (metaTagFoundComposer == 0) { metaTagFoundComposer = 1 print getEmptyMetaTag("composer-") } (metaTagFound == 1) && (metaTagFoundMixerType == 0) { metaTagFoundMixerType = 1 print getEmptyMetaTag("mixerType") } (metaTagFound == 1) && (metaTagFoundMovementTitle == 0) { metaTagFoundMovementTitle = 1 print getEmptyMetaTag("movementTitle-") } (metaTagFound == 1) && (metaTagFoundRevision == 0) { metaTagFoundRevision = 1 print getRevisionDate() } (metaTagFound == 1) && (metaTagFoundSource == 0) { metaTagFoundSource = 1 if (metaTagSource == "") print getEmptyMetaTag("source+") else print metaTagSource } (metaTagFound == 1) && (metaTagFoundTune == 0) { metaTagFoundTune = 1 print getEmptyMetaTag("tune") } (metaTagFound == 1) && (metaTagFoundWorkTitle == 0) { metaTagFoundWorkTitle = 1 print getEmptyMetaTag("workTitle+") } # all non-affected xml lines go here { print } END {}