post_install () {
  # Define root partitions partuuid
  PART_DEV=`findmnt / -o source -n | cut -f1 -d"["`
  PART_NAME=`echo $PART_DEV | cut -d "/" -f 3`
  ROOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${PART_NAME}" | awk '{print $2}')
  FSTYPE=$(lsblk -p -o NAME,FSTYPE | grep "${PART_NAME}" | awk '{print $2}')
  
  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "btrfs" ]]; then
   echo "===> Installing default btrfs cmdline.txt /boot..."
   echo "rootflags=subvol=@ rootfstype=btrfs root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi
  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "ext4" ]]; then
   echo "===> Installing default ext4 cmdline.txt /boot..."
   echo "root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi
  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "f2fs" ]]; then
   echo "===> Installing default f2fs cmdline.txt /boot..."
   echo "root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi

  if [ -f /boot/config.txt ]; then
#    echo "===> Modifing config.txt to load for pi3/4/5 kernels"
    sed -i 's\initramfs initramfs-linux.img followkernel\auto_initramfs=1\g' /boot/config.txt
#    sed -i '/kernel=kernel8.img/I d' /boot/config.txt
   else
    echo "===> Installing default config.txt to /boot/..."
    echo "# See /boot/overlays/README for all available options

# Uncomment some or all of these to enable optional Hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Run in 64bit mode
arm_64bit=1

# Auto load correct initramfs files if found
auto_initramfs=1

# Run as fast as the firmware/board allows
arm_boost=1

# Disable compensation for displays with overscan
disable_overscan=1

# Enable sound
dtparam=audio=on
# Uncomment if no sound thru hdmi
#hdmi_drive=2

# Auto load overlays for detected cameras
camera_auto_detect=1
# Auto load overlays for detected DSI displays
display_auto_detect=1

# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
# For pi4's and above boards uncomment next line & Comment out above line
#dtoverlay=vc4-kms-v3d,cma-512
max_framebuffers=2

# Don't have the firmware create an initial video= setting in cmdline.txt
# Use the kernel default instead
#disable_fw_kms_setup=1

# Disable rainbow screen at boot
disable_splash=1

# RPi 5B/4B/400 ONLY
# For 4k content @ 60 Hz refresh rate, uncomment hdmi_enable_4kp60=1
#hdmi_enable_4kp60=1
# If video breaks with hdmi_enable_4kp60=1 uncomment
#force_turbo=1


[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller
# This line should be remoed if the legacy DWC2 controller is required
otg_mode=1


[all]" > /boot/config.txt
    fi
}

post_upgrade () {
  # Define root partitions partuuid
  PART_DEV=`findmnt / -o source -n | cut -f1 -d"["`
  PART_NAME=`echo $PART_DEV | cut -d "/" -f 3`
  ROOT_PART=$(lsblk -p -o NAME,PARTUUID | grep "${PART_NAME}" | awk '{print $2}')
  FSTYPE=$(lsblk -p -o NAME,FSTYPE | grep "${PART_NAME}" | awk '{print $2}')

  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "btrfs" ]]; then
   echo "===> Installing default btrfs cmdline.txt /boot..."
   echo "rootflags=subvol=@ rootfstype=btrfs root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi   
  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "ext4" ]]; then
   echo "===> Installing default ext4 cmdline.txt /boot..."
   echo "root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi
  if [ ! -f /boot/cmdline.txt ] && [[ "$FSTYPE" == "f2fs" ]]; then
   echo "===> Installing default f2fs cmdline.txt /boot..."
   echo "root=PARTUUID=$ROOT_PART rw rootwait console=serial0,115200 console=tty3 selinux=0 quiet splash plymouth.ignore-serial-consoles smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 usbhid.mousepoll=8 audit=0" > /boot/cmdline.txt
   fi

  if [ -f /boot/config.txt ]; then
#    echo "===> Modifing config.txt to load for pi3/4/5 kernels"
    sed -i 's\initramfs initramfs-linux.img followkernel\auto_initramfs=1\g' /boot/config.txt
#    sed -i '/kernel=kernel8.img/I d' /boot/config.txt
   else
    echo "===> Installing default config.txt to /boot/..."
    echo "# See /boot/overlays/README for all available options

# Uncomment some or all of these to enable optional Hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Run in 64bit mode
arm_64bit=1

# Auto load correct initramfs files if found
auto_initramfs=1

# Run as fast as the firmware/board allows
arm_boost=1

# Disable compensation for displays with overscan
disable_overscan=1

# Enable sound
dtparam=audio=on
# Uncomment if no sound thru hdmi
#hdmi_drive=2

# Auto load overlays for detected cameras
camera_auto_detect=1
# Auto load overlays for detected DSI displays
display_auto_detect=1

# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
# For pi4's and above boards uncomment next line & Comment out above line
#dtoverlay=vc4-kms-v3d,cma-512
max_framebuffers=2

# Don't have the firmware create an initial video= setting in cmdline.txt
# Use the kernel default instead
#disable_fw_kms_setup=1

# Disable rainbow screen at boot
disable_splash=1

# RPi 5B/4B/400 ONLY
# For 4k content @ 60 Hz refresh rate, uncomment hdmi_enable_4kp60=1
#hdmi_enable_4kp60=1
# If video breaks with hdmi_enable_4kp60=1 uncomment
#force_turbo=1


[cm4]
# Enable host mode on the 2711 built-in XHCI USB controller
# This line should be remoed if the legacy DWC2 controller is required
otg_mode=1


[all]" > /boot/config.txt
    fi
}

post_remove () {
  rm -f boot/initramfs_2712
}
