#!/bin/sh
#
# Script Name: rpi_initrd
# Description: Enable/Disable initrd usage on boot partition
# Author     : Konstantinos Mantzaris <kmanjaris@gmail.com>
# Web Site   : http://SlaXBMC.blogspot.com
#

REV=$(uname -r|cut -d. -f2)

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_disable_new() {
  if [ -d /mnt/boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /mnt/boot
    if [ -f /mnt/boot/config.txt ] && [ "`grep '#initramfs' /mnt/boot/config.txt`" == "" ] ; then
      sed -i 's/initramfs/#initramfs/g' /mnt/boot/config.txt
    fi
    sync;sync
    umount /mnt/boot
  elif [ -d /boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /boot
    if [ -f /boot/config.txt ] && [ "`grep '#initramfs' /boot/config.txt`" == "" ] ; then
      sed -i 's/initramfs/#initramfs/g' /boot/config.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"
    echo "NOTE: initramfs line in config.txt boot file."
  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
}

initrd_enable_new() {
  if [ -d /mnt/boot ] ; then
    mount -t vfat /dev/mmcblk0p1 /mnt/boot
    if [ -f /mnt/boot/config.txt ] ; then
      sed -i 's/#initramfs/initramfs/g' /mnt/boot/config.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/#initramfs/initramfs/g' /boot/config.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')
    if [ $REV -gt 6 ] ; then
	initrd_enable_new
    else
	initrd_enable
    fi
    ;;
'disable')
    if [ $REV -gt 6 ] ; then
	initrd_disable_new
    else
	initrd_disable
    fi
    ;;
*)
  echo "Usage: `basename $0` [enable|disable]"
esac
