# ShellFunctions 2026.02.07.

# Reference
# https://lists.debian.org/debian-user/2019/03/msg00196.html
# https://lists.debian.org/debian-user/2024/06/msg00788.html

abi() { case "$#" in
  0) printf "abi <filename>\n" ;;
  1) DISPLAY=:0.0 abiword "$1" & ;;
  *) printf "Too many arguments.\n" ;; esac }

Backup() { \
  printf "0 arguments is OK.\n"
  exclusions=( \
  'A2*' \
  'AOS*' \
  'Configuration.XML' \
  'Trap*' \
  '.Trash*' \
  '.aoshome' \
  "lost+found" \
  '*.Bak' \
  '*.GofU' \
  '*.GofUu' \
  '*.SymU' \
  '*.SymUu' \
  '*.bundle' \
  '*.iso' \
  '*.mp3' \
  '*.mp4' \
  '*.dxf' \
  '*.sig' \
  '*.webm' \
  '*.zd' \
  '*.zip' \
  '.tmp*' );
  echo exclusions = "${exclusions[@]}"
  i=0
  while [ "$i" -lt "${#exclusions[@]}" ]; do
    exclusions[$i]="--exclude=${exclusions[$i]}"
    ((i++))
  done
  printf "i = %i\n" "$i"
  echo "exclusions = ${exclusions[@]}"
  printf "Executing sync and rsync.\n"
  sync
  if ( grep -- EasyOS /etc/DISTRO_SPECS > /dev/null ); then
    printf "This is an EasyOS system.\n"
    source="/mnt/A/"
    destination="/mnt/B/root/Backup"
  elif [ -f "/etc/devuan_version" ]; then
    printf "Probably a Devuan system.\n"
    source="/home/root/A/"
    destination="/home/root/Backup"
  fi
  printf "source is %s.\n" "$source"
  printf "A source directory ending / is not created in the destination.\n"
  printf "With /, the source is the content and not the directory itself.\n"
  printf "destination is %s.\n" "$destination"
  rsync -auv "${exclusions[@]}" "$source" "$destination"
  /bin/ls -ld "$destination"/MailMessages
  printf "du -hs $destination => "
  du -hs "$destination"
}

BackupBinaries() { \
  if [ $# -gt 0 ]; then
    printf "Too many arguments.";
  else
    destination1=/home/root/BackupBin;
    printf "destination1 is $destination1.\n";
    printf "Executing sync and rsync.\n";
    sync;
    printf "dxfs ...\n"
    rsync       -auv *.dxf  $destination1 ;
    printf "jpgs ...\n"
    rsync       -auv *.jpg  $destination1 ;
    printf "jpegs ...\n"
    rsync       -auv *.jpeg  $destination1 ;
    printf "pngs ...\n"
    rsync       -auv *.png  $destination1 ;
    printf "pdfs ...\n"
    rsync       -auv *.pdf  $destination1 ;
    printf "du -hs $destination1 => \n";
    du -hs "$destination1";
  fi
}

# ( *#) matches a line begining with blanks and a "#".
# ( *$) matches a line containing only blanks.
collect () { case $# in
  0) printf "collect <filename>\n" ;;
  1) egrep -v '^(( *#)|( *$)|( *;))' $1 ;;
  *) printf "Too many arguments.\n" ;; esac }

dil () { case $# in
  0) DISPLAY=:0.0 dillo file:///home/root/A/Peter.html#Links & ;;
  1) DISPLAY=:0.0 dillo $1 & ;;
  *) printf "Too many arguments.\n" ;; esac }

em () { gedit /home/olpc/A/Mail.Out.Text ; }

et () { case $# in
  0) gedit /home/olpc/A/a & ;;
  *) gedit "$@" & ;; esac }

ev () { DISPLAY=:0.0 /usr/bin/evince "$@" & }

ethoinstall  () { qemu-system-i386 \
  -drive file=/home/root/A/OberonCF0.Dsk,if=floppy,format=raw \
  -drive file=/dev/sdb,if=ide,format=raw \
  -vga std -boot order=a ; }

#  -vga std -boot order=a \
#  -nic tap,model=ne2k_pci ; }                                                   

etho () { qemu-system-i386 \
  -drive file=/dev/KingstonCF,if=ide,format=raw \
  -vga std -boot order=c \
  -nic tap,model=ne2k_pci ; }

# Reference
# https://lists.debian.org/debian-user/2024/06/msg00788.html

FilesToDVD () { \
  printf "Insert open or new DVD-R.\n";
  read t;
  startPath=$PWD;
  printf "startPath is $startPath\n";
  source=/home/root/Backup;
  printf "source is $source\n";
  cd $source;
  destination=/dev/sr0;
  printf "destination is $destination\n";
  xorriso -for_backup \
          -dev "$destination" \
          -assert_volid 'BOB.*' fatal \
          -volid BOB."$(date '+%Y.%m.%d.%H%M%S')" \
          -update_r . / \
          -commit \
          -toc -check_md5 failure -- \
          -eject all \
          -rollback_end ;
  cd $startPath ;
  printf "xorriso -dev /dev/sr0 -toc  \n";
  printf "sudo mount -o sbsector=nnnnnn /dev/sr0 /mnt/iso  \n"; }

FilesToHDD () { \
devuanSource="/home/root/Backup";
printf "devuanSource=%s\n" "$devuanSource";
easySource="/mnt/B/root/Backup";
printf "easySource=%s\n" "$easySource";
#printf "destination is the serial number of a LaCie HDD connected by USB.\n";
#destination="10000E000D959403";
#printf "destination is the vendor:product of a LaCie HDD connected by USB.\n";
#destination="059f:0651";
printf "destination is the size of a LaCie HDD connected by USB.\n";
destination="465.8G";
printf "destination=%s\n" "$destination";

FTH () { \
  printf "source is %s.\n" "$source";
  printf "Press any key to continue.\n";
  read -n 1 c;
  if ! test -d "$source"; then
    printf "Intended source does not exist.\n";
  else
    printf "source is $source\n";
    printf "Intended source exists. Evaluate destination.\n";
    #if ! lsblk -o serial | grep -F -- "$destination" > /dev/null ; then
    if ! lsblk -o size | grep -F -- "$destination" > /dev/null ; then
      printf "destination device not connected or the size\n";
      printf "assigned to destination is wrong.  Aborting.\n";
    else
      printf "Obtain the kernel name for device, dev.\n";
      dev=$(lsblk -o name,size | grep -F -- "$destination" | cut -d " " -f1);
      printf "dev=%s\n" "$dev";
      dest="/dev/$dev";
      #dest="/dev/null";
      printf "dest = %s\n" "$dest";
      printf "dest is ready to receive backup.\n"
      printf "Press any key to continue.\n";
      read -n 1 c;
      xorriso -for_backup \
        -dev stdio:"$dest" \
        -assert_volid 'BOB.*' fatal \
        -volid BOB."$(date '+%Y.%m.%d.%H:%M:%S')" \
        -update_r "$source" / \
        -commit \
        -toc \
        -check_md5 failure -- \
        -rollback_end ;
      printf "xorriso -dev stdio:/dev/sdX -toc \n";
      printf "mount -o sbsector=nnnnnn /dev/sdX /mnt/hdd \n";
      printf "ET.OpenAscii /mnt/hdd/MailMessages\n";
    fi;
  fi;
}

#          -check_md5 failure -- \
#          -rollback_end ;  # Add rollback_end for a dryrun.

if ( grep -- EasyOS /etc/DISTRO_SPECS > /dev/null ); then
  printf "This is an EasyOS system.\n"
  source="$easySource";
  FTH 
elif [ -f "/etc/devuan_version" ]; then
  printf "Probably a Devuan system.\n";
  source="$devuanSource";
  FTH 
else
  printf "OS is not recognized.\n";
fi;
}

#          -check_md5 failure -- \
#          -rollback_end ;  # Add rollback_end for a dryrun.

fire () { case "$#" in
  0) DISPLAY=:0.0 firefox    & ;;
  1) DISPLAY=:0.0 firefox $1 & ;;
  *) printf "Too many arguments." ;;
  esac ;
  exit 0 ; }
  
freedosinstall () { qemu-system-i386 \
  -cdrom /home/root/FreeDOS/FD14LIVE.iso \
  -drive file=/dev/sdb,if=ide,format=raw \
  -vga std -boot order=d ; }

get () { case "$#" in
  0) DISPLAY=:0.0 gedit /home/root/a & ;;
  *) DISPLAY=:0.0 gedit "$@" & ;; esac }

gea () { case "$#" in
  0) DISPLAY=:0.0 geany /root/a & ;;
  *) DISPLAY=:0.0 geany "$@" & ;; esac }

gim () { DISPLAY=:0.0 gimp "$@" ; }

lc () { case $# in
  0) DISPLAY=:0.0 librecad & ;;
  1) DISPLAY=:0.0 librecad "$1" & ;;
  *) printf "Too many arguments.\n" ;; esac }
  
lin () { cd ; DISPLAY=:0.0 linphone & }

nfiles () { case $# in 
  0) /bin/ls -1d ./* | wc -l ;;
  1) /bin/ls -1d $1/* | wc -l ;;
  *) printf "Too many arguments." ;; esac }

mantxt () { case $# in
  0) printf "mantxt [<section>] <command>\n" ;;
  1) man -Tascii $1    | col -bx >   $1".man" ;;
  2) man -Tascii $1 $2 | col -bx > $2"."$1".man" ;;
  *) printf "Too many arguments." ;; esac }

mya2 () { \
#!/usr/bin/ash
# A function or 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 to work. 
# * The volume containing the working files has a reliable identity.
#   All Unix-like systems recognize a UUID.  Refer to lsblk and blkid
# * If /etc/fstab has a specification for the working volume it
#   should be consistent 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.
#
# UUID of a stainless steel Kingston USB store.
workingVolume=40d81969-8f9d-4964-b214-87bcf273192a
printf "workingVolume is $workingVolume\n"
#
easyosWorkingDirectory=/mnt/A
printf "easyosWorkingDirectory=%s\n" "$easyosWorkingDirectory"
devuanWorkingDirectory=/home/root/A
printf "devuanWorkingDirectory=%s\n" "$devuanWorkingDirectory"
#
# For UnixAOS preceding revision.10272.
oldAosStartScript=/usr/bin/aos
printf "oldAosStartScript=%s\n" "$oldAosStartScript\n"
# For UnixAOS revision.10272.
newAosStartScript=/usr/local/bin/a2Ffiled
printf "newAosStartScript=%s\n" "$newAosStartScript\n"
# For repository based A2.
# repositoryStartScript=<somewhere>/<repository>/<platform>/<script>
repositoryStartScript=/mnt/A/A2/Linux64/a2.sh
printf "repositoryStartScript=%s\n" "$repositoryStartScript"
#
# 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) = joule* ]]; then
     h="joule"
# Add more cases here.
else
     h=$(hostname --short)
fi
printf "Shortened hostname, h = $h\n"
A2 () {
  contextDir=$PWD
  cd $workingDirectory
  printf "Switched to working directory.\n"
  if ! test -f Configuration.$h.XML ; then
    printf "Configuration.$h.XML not present in %s.  Aborting.\n" "$workingDirectory"
  else
    printf "Copying Configuration.$h.XML to Configuration.XML.\n"
    /bin/cp Configuration.$h.XML Configuration.XML
    if ! test -f Oberon.$h.Text ; then
      printf "Oberon.$h.Text not present in $workingDirectory.  Aborting.\n"
    else
      printf "Copying Oberon.$h.Text to Oberon.Text.\n"
      /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"
      $startScript
    fi
  fi
  cd $contextDir
  printf "mya 2 exited normally.\n"
}

EvaluateWorkingVolume () { 
  printf "startScript=%s\n" "$startScript"
  printf "Evaluating working volume.  Stand by for several seconds.\n"
  if ! lsblk -alno UUID | grep -q $workingVolume ; then
    printf "$workingVolume containing working files not connected.  Aborting.\n"
  else
    printf "$workingVolume containing working files is connected.\n"
    if test -n "$(findmnt -nrS UUID=$workingVolume)" ; then
      printf "Working volume is mounted.  Ideally, 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 mount /dev/*.
        mount -v "$(blkid --uuid $workingVolume)" $workingDirectory
        A2
      else
        printf "fsck attempted repair of $workingVolume.  Try again.\n"
      fi
    fi
  fi
}

if ( grep -- EasyOS /etc/DISTRO_SPECS > /dev/null ) ; then
  printf "This is an EasyOS system.\n"
  workingDirectory="$easyosWorkingDirectory"
  startScript=$repositoryStartScript
  EvaluateWorkingVolume
elif [ -f "/etc/devuan_version" ] ; then
  printf "Probably a Devuan system.\n"
  workingDirectory="$devuanWorkingDirectory"
  startScript=$repositoryStartScript
  EvaluateWorkingVolume
elif [ -f "/etc/devuan32bit" ] ; then
  printf "Probably a Devuan 32 bit system.\n"
  workingDirectory="$devuanWorkingDirectory"
  startScript=$newAosStartScript
  EvaluateWorkingVolume
else
  printf "OS is not recognized and A2 system not started.\n"
fi
}

pi () { case $# in
  0) printf "pi <ipAddress>\n" ;;
  1) ping -c 3 $1 ;;
  *) printf "Too many arguments.\n" ;; esac }

Restore() { \
  source=/home/root/Backup/
  printf "A / ending source means dont create the directory.\n"
  printf "source is $source.\n"
  destination=/home/root/A
  printf "destination is $destination.\n"
  rsync --exclude "lost+found" -anuv  $source  $destination ; }
  
Test() { \
devuanSource="/home/root/Backup"
printf "devuanSource=$devuanSource\n"
easySource="/mnt/ackup"
printf "easySource=$easySource\n"
a="/mnt/Backup"
printf "a=$(a)\n" ; }

Desktops.OpenDoc telnet://localhost/
pushd ~/A
sftp -oPort=50687 easthope@banana.exacthosting.com:/home/easthope/public_html/

put ShellFunctions
quit
popd
