diff options
Diffstat (limited to 'web/ws-replace')
-rwxr-xr-x | web/ws-replace | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/web/ws-replace b/web/ws-replace new file mode 100755 index 0000000..6870033 --- /dev/null +++ b/web/ws-replace @@ -0,0 +1,65 @@ +#!/bin/sh +# Takes: +# ws-replace begin end origin dest commitident txt|csv script +# Begin: <!-- ident:begin --> +# End: <!-- ident:end --> +# Origin: $HOME/docs/wr/orgd/kt/biblio.csv +# Dest: $WEBSITE_PATH/rd/index.html +# Commitident: Biblio (: updated ...) +# Txt|Csv: csv +# script: biblio-by-month +# File If Origin gives a directory (as for example, the script called +# will update a whole directory), then File gives the exact file +# to modify. +# If `txt`: +# Removes all comments; treats each line as an entry, +# and tab as the delimiter. Edits are made with: $EDITOR +# or $VISUAL or nvim or vim or vi. +# If `csv`: +# Calls out to an external script, $SCRIPT to generate +# the HTML. +# Both then substitute the HTML in. + +DELIMITER_BEGIN="$1" +DELIMITER_END="$2" +ORIGIN="$3" +DEST="$4" +COMMITIDENT="$5" +TXTCSV="$6" +SCRIPT="$7" +TABLE_FILE="/tmp/$5-formatted" +FILE="$8" + +# Work out $EDITWITH +if [ "$TXTCSV" = "txt" ] ; then + EDITWITH="$EDITOR" + # TODO: add VISUAL, nvim, vim, vi... +else + EDITWITH="sc-im" +fi + +if [ -z "$FILE" ] ; then + hash_before="$(sha256sum $ORIGIN)" + $EDITWITH "$ORIGIN" + hash_after="$(sha256sum $ORIGIN)" +else + # As $ORIGIN is a directory, so here we specify the exact file. + hash_before="$(sha256sum "$ORIGIN/$FILE")" + $EDITWITH "$ORIGIN/$FILE" + hash_after="$(sha256sum "$ORIGIN/$FILE")" +fi + +if [ "$hash_before" = "$hash_after" ] ; then + echo "No changes made - quitting early!" + exit 0 +fi + +echo "Changes will be made." + +formatted="$($SCRIPT "$ORIGIN")" +echo "$formatted" > "$TABLE_FILE" +sed -i -ne "/$DELIMITER_BEGIN/ {p; r $TABLE_FILE" -e ":a; n; /$DELIMITER_END/ {p; b}; ba}; p" "$DEST" + +# Commit and push the changes +ws-push + |