#!/bin/bash 
# A script to start UnixAos with a working directory which can be in a
# removable flash store. 
# A flash store is accomodated 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 name.
#   The names in /dev/disk/by-label/ and /dev/disk/by-uuid/
#   are convenient.  For example, a volume labeled A will appear
#   as /dev/disk/by-label/A.  Additional details here.
#   http://reactivated.net/writing_udev_rules.html#builtin
#   https://wiki.debian.org/udev
# * Mounting of the working volume is specified in /etc/fstab.  
#   What if the volume is automounted at a second location?
#   If edit conflict is avoided, a second mount is probably harmless.
# * The following assignments to workingVolume and workingDirectory
#   are consistent with preceding information.
# * An appropriate entry exists in /etc/fstab.  Example. 
#   /dev/A /home/me/A ext2 defaults,user,users,exec,noauto 0 0 
# * /etc/sudoers.d/sudoers is adjusted to allow the user to fsck the 
#   working filesystem.
#   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 A2 revision 10272 the start scipt was /usr/bin/aos.
#   For revision 10272, the start script is /usr/local/bin/a2.
#   For a git repository <A2repo> and system <system> the start
#   script is <A2repo>/<system>/a2.sh.  
#   Eg. /home/me/A2repo/Linux64/a2.sh.
# * aos attempts to create the link <workingDirectory>/.aoshome
#   to aos but a FAT file system does not allow links.  Resolved by 
#   reformatting to ext2.  Ext2 in Wikipedia.
#
# This is the home part of the system drive.
#workingVolume=/dev/sda5
#workingVolume=/dev/disk/by-uuid/b1ed1667-2daf-471b-b8a0-9e284cadfe78
# This is a SD card.
workingVolume=/dev/disk/by-uuid/65b50b9d-5256-4a06-b48b-2425d266387c
echo workingVolume is $workingVolume.
#
workingDirectory=/home/root/AY
#workingDirectory=/home/root/Backup
echo workingDirectory=$workingDirectory.
#
# For any hostname violating Oberon file name syntax, make a conformant name.
# Ref. https://www.tldp.org/LDP/abs/html/comparison-ops.html
if [[ $(hostname) = xo-53-1d-bb* ]]; then
     h="xo"
elif [[ $(hostname) = joule* ]]; then
     h="joule"
# Add more cases here.
else
     h=$(hostname --short)
fi
echo Shortened hostname, h = $h

Aos () {
  aosContextDir=$PWD
  cd $workingDirectory
  if test -f Configuration.$h.XML
  then
    echo Copying Configuration.$h.XML to Configuration.XML.
    /bin/cp Configuration.$h.XML Configuration.XML
    if test -f Oberon.$h.Text
    then
      echo Copying Oberon.$h.Text to Oberon.Text.
      /bin/cp Oberon.$h.Text Oberon.Text
      /bin/rm --force --verbose AOS*.Log
      /bin/rm --force --verbose Trap*.txt
      /bin/rm --force --verbose .tmp.*
      /usr/bin/aos
   else
      echo Oberon.$h.Text not present in $workingDirectory.  Aborting.
    fi
  else
    echo Configuration.$h.XML not present in $workingDirectory.  Aborting.
  fi
  cd $aosContextDir
}

if test -b $workingVolume
then
  if findmnt -rn $workingVolume
  then
    echo Working volume is mounted.
    Aos
  else
    echo Working volume is not mounted.
    if /sbin/fsck -p $workingVolume
    then
      echo Filesystem in $workingVolume passed fsck.
      # The mount is specified in /etc/fstab
      mount -v $workingVolume
      Aos
    else
      echo fsck found a problem in $workingVolume and attempted repair.  Try again.
    fi
  fi
else
  echo $workingVolume containing working files not connected.  Aborting.
fi