Oberon/A2/mya2

#!/bin/bash
# A script to start UnixAos or repository based A2 with a working
# directory which can be in a removable flash store. 
# A flash store is accommodated in multiple machines by copying 
# machine specific configurations into Configuration.XML and 
# Oberon.Text. 
# 
# Conditions for this script to work. 
# * The volume containing the working files has a reliable identity.
#   Contemporary systems recognize a UUID.  Refer to lsblk and blkid
# * If /etc/fstab has a specification for the working volume, consider
#   consistency with the names and mount command below.  Eg.
#   UUID=40d81923-8f9d-4964-b724-87bcf2731432a /home/me/A ext2 defaults,user,users,exec,noauto 0 0 
# * The user can fsck the working filesystem.
#   A line such as the following may be required in /etc/sudoers.d/sudoers. 
#   me mycomputer = NOPASSWD: /sbin/fsck
#   Test fsck interactively.
# * If the working volume is removeable, the user can mount it.
#   Note the user option in /etc/fstab. 
# * Prior to revision 10272, UnixAos attempts to create the link
#  <workingDirectory>/.aoshome to /usr/aos but the FAT file system
#  doesn't provide links.  Resolved by reformatting to ext2.
#  Ext2 in Wikipedia.
#
workingDirectory=/mnt/A
#workingDirectory=$HOME/Backup
printf "workingDirectory=%s.\n" "$workingDirectory"
#
# For UnixAOS preceding revision.10272.
#startScript=/usr/bin/aos
# For UnixAOS revision.10272.
#startScript=/usr/local/bin/a2
# For repository based A2.
# startScript=<repository>/<platform>/<script>
startScript=A2/Linux64/a2.sh
printf "startScript=%s\n" "$startScript"
#
A2 () {
  contextDir=$PWD
  cd $workingDirectory
  printf "Switched to working directory.\n"
  if ! test -f Configuration.$h.XML ; then
    printf "Configuration.%s.XML not present in %s.  Aborting.\n" "$h"
  else
    printf "Copying Configuration.%s.XML to Configuration.XML.\n" "$h"
    /bin/cp Configuration.$h.XML Configuration.XML
    if ! test -f Oberon."$h".Text ; then
      printf "Oberon.%s.Text not present in %s.  Aborting.\n" "$h" "$workingDirectory"
    else
      printf "Copying Oberon.%s.Text to Oberon.Text.\n" "$h"
      /bin/cp Oberon.$h.Text Oberon.Text
      /bin/rm --force --verbose AOS*.Log
      /bin/rm --force --verbose Trap*.txt
      /bin/rm --force --verbose .tmp.*
      printf "Starting A2 system.\n"
      "$workingDirectory"/"$startScript"
    fi
  fi
  cd $contextDir
}
printf "A2 function declared.\n"

printf "Evaluating working volume.\n"
# UUID of a stainless steel Kingston USB store.
workingVolume=40d81969-8f9d-4964-b214-87bcf273192a
printf "workingVolume is %s\n" "$workingVolume"
#
# For any hostname violating Oberon file name syntax, make a conformant name.
# https://tldp.org/LDP/abs/html/testconstructs.html
if [[ $(hostname) = xo-53-1d-bb* ]]; then
     h="xo"
elif [[ "$(hostname)" = imager ]]; then
     h="imager"
# Add more cases here.
else
     h="$(hostname --short)"
fi
printf "Shortened hostname, h = %s\n" "$h"

if !(lsblk -alno UUID | grep -qF -- "$workingVolume"); then
  printf "%s containing working files not connected.  Aborting.\n" "$workingVolume"
else
  printf "%s containing working files is connected.\n" "$workingVolume"
#  if [ -n "$(findmnt -nrS UUID=$workingVolume)" ]; then
  if mountpoint -q "$workingDirectory"; then
    printf "Working volume is mounted as intended.  Possibly consistent with the subsequent mount command.\n"
    A2
  else
    printf "Working volume is not mounted.\n"
    if fsck -p UUID=$workingVolume ; then
      printf "Filesystem in $workingVolume passed fsck.\n"
      # busybox mount lacks the -U option.  Therefore refer to /dev/*.
      mount -v "$(blkid --uuid $workingVolume)" $workingDirectory
      A2
    else
      printf "fsck attempted repair of $workingVolume.  Try again.\n"
    fi
  fi
fi
exit 0