#!/bin/bash
# ━━━━━━━━━━━━━
# publish-files - Update the Web server’s copy of my public workstation files
# ━━━━━━━━━━━━━
#
#      publish-files  [<additional imaging options>]
#
#   Any <additional imaging options> are passed verbatim to `waycast-web-image`.


    function run {
        /home/mike/_/Web_service/ensure-hard-links

        echo
        echo 'Rebuild the Web imager'
        echo '──────────────────────'
        cd ~/work/
        wayic/Web/imager/build-all || return # Build the Web imager, or refresh the present build.
        echo

        echo 'Clean the Web image on the server of orphan image files'
        echo '───────────────────'
        sudo --user=public --preserve-env=JDK_HOME Breccia/Web/imager/bin/web-image-clean \
              $mountpoint/var/www/public/htdocs/
          # --preserve-env : Retain variable `JDK_HOME` for the shebang of `web-image-clean`,
          #     q.v. at `http://reluk.ca/project/Breccia/Web/imager/bin/web-image-clean`.
        echo

        echo 'Synchronize the Web image on the server with the original files on the workstation'
        echo '─────────────────────────'
        sudo --user=public sync-web || return
        echo

        echo 'Re-image Breccian source files on the server that need refreshing'
        echo '──────────────────────────────'
        sudo --user=public --group=users --preserve-env=JDK_HOME wayic/Web/imager/bin/waycast-web-image \
              -author-home-directory='/home/mike' \
              -co-service-directory='/_/Web_service' \
              -math \
              $* \
              -reference-mapping=';^(?:~|/home/mike)/(?:work|code/WP3);${boundary}/project;||;^(?:~|/home/mike)(?=/|$);${boundary};||;^/;http://reluk.ca/sys/computer/workstation/;' \
              -reference-mapping=';^http://reluk.ca/sys/computer/workstation(/.*\.brec$);${boundary}/sys/computer/Primeval$1;||;^http://reluk.ca(/.*\.brec$);${boundary}$1;' \
              $mountpoint/var/www/public/htdocs/ ;}
          # `sudo` options:
          #     --group=users : Gives user `public` access (for the present step) to select files that
          #         are unpublished because their permissions stopped `public` (in the preceding step)
          #         from copying them to the server.  Most such files are always unreadable by `public`,
          #         but a select few (e.g. some boneyard `._/` directories) I have made readable to the
          #         `users` group, and thus to `public` for the present step.
          #             The purpose is to allow the Web imager to report broken references
          #         that might otherwise go unnoticed.
          #     --preserve-env : Retain variable `JDK_HOME` for the shebang of `waycast-web-image`,
          #         q.v. at `http://reluk.ca/project/wayic/Web/imager/bin/waycast-web-image`.
          # `waycast-web-image` options:
          #     -author-home-directory : Mine, as though I were the user instead of `public`.
          #     -reference-mapping (1) : The replacement string in the default mapping of `/` could have
          #         been formed as the relative reference `${boundary}/sys/computer/Primeval/`, but not
          #         `${boundary}/sys/computer/workstation/`.  Referring to the latter requires the full
          #         URI `http://reluk.ca/sys/computer/workstation/` because Apache serves it as an alias
          #         of the former.  It has no separate existence in the server’s file system.
          #     -reference-mapping (2) : Locally remapping Breccian references that target my Web site,
          #         so ensuring (as a workaround for deferral of HTTP fetches) full parsing
          #         of referent clauses and hyperlinking of their referents.
          #         http://reluk.ca/project/Breccia/Web/imager/working_notes.brec.xht#deferral,hTTP,fetches
          #             Here the first replacement for `/` in `-reference-mapping` (1) above must,
          #         for the reason given there, be reformed as `${boundary}/sys/computer/Primeval/`.


    mountpoint=/mnt/lan/server/unbak
    if [[ ! $(findmnt --mountpoint "$mountpoint") ]]; then
        sudo mount $mountpoint/ || exit
        was_mounted_here=1
        fi
    run $*
    status=$?
    [ $was_mounted_here ] && (sudo umount $mountpoint/ || exit)
    exit $status