#!/bin/sh
#
# /usr/local/bin/rpi_initrd
#
# Enable/Disable initrd usage on boot partition
#
#

initrd_disable() {
  if [ -d /mnt/boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /mnt/boot
    if [ -f /mnt/boot/config.txt ] && [ "`grep '#ramfsfile' /mnt/boot/config.txt`" == "" ] && [ "`grep '#ramfsaddr' /mnt/boot/config.txt`" == "" ] ; then 
      sed -i 's/ramfsfile=/#ramfsfile=/g' /mnt/boot/config.txt
      sed -i 's/ramfsaddr=/#ramfsaddr=/g' /mnt/boot/config.txt  
    fi
    if [ -f /mnt/boot/cmdline.txt ] && [ "`grep '#initrd' /mnt/boot/cmdline.txt`" == "" ] ; then sed -i 's/initrd=/#initrd=/g' /mnt/boot/cmdline.txt ; fi
    sync;sync
    umount /mnt/boot
  elif [ -d /boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /boot
    if [ -f /boot/config.txt ] && [ "`grep '#ramfsfile' /boot/config.txt`" == "" ] && [ "`grep '#ramfsaddr' /boot/config.txt`" == "" ] ; then 
      sed -i 's/ramfsfile=/#ramfsfile=/g' /boot/config.txt
      sed -i 's/ramfsaddr=/#ramfsaddr=/g' /boot/config.txt  
    fi
    if [ -f /boot/cmdline.txt ] && [ "`grep '#initrd' /boot/cmdline.txt`" == "" ] ; then sed -i 's/initrd=/#initrd=/g' /boot/cmdline.txt ; fi
    sync;sync
    umount /boot
  else
    echo "Could not map SlaXBMCRPi boot partition /dev/mmcblk0p1"
    echo "NOTE: Please mount the partition manually and comment the initrd"
    echo "NOTE: ramfsfile and ramfsaddr definitions on config.txt and cmdline.txt boot files"
  fi
}

initrd_enable() {
  if [ -d /mnt/boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /mnt/boot
    if [ -f /mnt/boot/config.txt ] ; then
      sed -i 's/#ramfsfile=/ramfsfile=/g' /mnt/boot/config.txt
      sed -i 's/#ramfsaddr=/ramfsaddr=/g' /mnt/boot/config.txt
    fi
    if [ -f /mnt/boot/cmdline.txt ] ; then sed -i 's/#initrd=/initrd=/g' /mnt/boot/cmdline.txt ; fi
    sync;sync
    umount /mnt/boot
  elif [ -d /boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /boot
    if [ -f /boot/config.txt ] ; then
      sed -i 's/#ramfsfile=/ramfsfile=/g' /boot/config.txt
      sed -i 's/#ramfsaddr=/ramfsaddr=/g' /boot/config.txt
    fi
    if [ -f /boot/cmdline.txt ] ; then sed -i 's/#initrd=/initrd=/g' /boot/cmdline.txt ; fi
    sync;sync
    umount /boot
  else
    echo "Could not map SlaXBMCRPi boot partition /dev/mmcblk0p1"
    echo "NOTE: Please mount the partition manually and uncomment the initrd"
    echo "NOTE: definitions on config.txt and cmdline.txt boot files"
  fi
}

case "$1" in
'enable')
  initrd_enable
  ;;
'disable')
  initrd_disable
  ;;
*)
  echo "Usage: `basename $0` [enable|disable]"
esac
