### Slackware Installer script ######################################################
# Script......: /usr/share/hwm-configure/platform/aarch64/installer/helper.scr/rk3399
# Called from : /usr/lib/setup/armedslack-postinstall-scripts/001-hwm-os-configure
# Purpose.....: Operating System configuration helper for Hardware Models:
#               * RockPro64
#               * Pinebook Pro
# Author......: Stuart Winter <mozes@slackware.com>
# Date........: 01-Feb-2022
# Contributors: Brenton Earl
### Slackware Installer script ######################################################

# The newly installed OS' /etc/fstab:
fstabfile=${T_PX}/etc/fstab

# U-Boot Boot Loader configuration file:
bootloaderconf=${T_PX}/boot/extlinux/extlinux.conf

# Hardware Model settings:
case $HWM in
   "Pine64 RockPro64"*)
      hwm=rockpro64 ;;

   "Pine64 Pinebook Pro"*)
      hwm=pinebookpro

      # Hibernation configuration for the Pinebook Pro.
      # -=-=-
      ## Hibernation isn't yet supported upstream, so this work is incomplete.
      # -=-=-
      # The state is stored on the swap partition, and the Kernel needs to
      # be configured for this through the 'resume=' Kernel cmdline operator.
      # We'll add this to the 'APPEND' configuration of the Boot Loader configuration.
      # Determine the configured swap partition is.  If there is >1 we'll 
      # use the first.
      # We need to experiment as to whether LABEL= works. If not we'll determine the
      # UUID and use that.
      #swappart=$( grep -E '^/|^LABEL=' ${fstabfile} | awk '$2~/swap/ {print $1}' | head -n1 )
      #[ -z "${swappart}" ] && exit 0
      #printf "RESUME=UUID=$(blkid | awk -F\" '/swap/ {print $2}')\n"

      # Configure the Kernel to know where the swap partition is so that it can
      # write state.  Add resume=<swap partition>
      # Only update the APPEND lines that are not commented.
      #sed -i '\?#.*APPEND?!s?APPEND ?APPEND resume='"${swappart}"' ?' ${bootloaderconf}
      ;;
esac

# Install a useful ALSA configuration if the ALSA directory
# can be found within the OS.  It'd not be present if the user didn't
# install the alsa packages.
if [ ! -z "${hwm}" ]; then
   if [ -d ${T_PX}/var/lib/alsa ]; then
      if [ -f ${hwm_osconfigpath}/assets/rk3399/snd/${hwm}/asound.state ]; then
         install -pm644 ${hwm_osconfigpath}/assets/rk3399/snd/${hwm}/asound.state ${T_PX}/var/lib/alsa/
         # We'll probably want to re-visit this in the future but for now,
         # this enables in the OoBE (Out of Box Experience):
         # It's not required, but I'll leave it here as an example of something
         # similar we may wish to do at a future date.
         #chmod +x ${T_PX}/etc/rc.d/rc.alsa
      fi
   fi
fi
