#!/bin/sh
#(c) Copyright Barry Kauler 2012, bkhome.org
#License GPL3 (/usr/share/doc/legal)
#shared-mime-info pkg has assigned initrd.gz mime-type application/initramfs-gz (by me).
#Click on initrd.gz in ROX-Filer, this script will run (see /root/Choices/MIME-types/application_initramfs-gz).
#note: script not internationalized, as this is a developer's tool.

[ ! $1 ] && exit 1
[ ! -f "$1" ] && exit 1
BASEFILE="`basename "$1"`"
[ "$BASEFILE" != "initrd.gz" ] && exit 1

compr_func() {
#find out compression type...
UNCOMPREXE=gunzip; COMPREXE=gzip; EXT=gz
gunzip -t "$1"
if [ $? -ne 0 ];then
 UNCOMPREXE=bunzip2; COMPREXE=bzip2; EXT=bz2
 bunzip2 -t "$1"
 if [ $? -ne 0 ];then
  UNCOMPREXE=unxz; COMPREXE=xz; EXT=xz
  unxz -t "$1"
  if [ $? -ne 0 ];then
   return 1
  fi
 fi
fi
return 0
}

cd /root
[ -f initrd ] && rm -f initrd
[ -f /tmp/initrd.gz ] && rm -f /tmp/initrd.gz
[ "$1" = "/root/initrd.gz" ] && cp -f /root/initrd.gz /tmp/

if [ -d initrd-expanded ];then
 
 pupdialog --background "yellow" --backtitle "initrd.gz: update?" --yesno "An initrd.gz is already expanded at /root/initrd-expanded. Is this correct, do you want to use /root/initrd-expanded to update ${1}?" 0 0
 if [ $? -eq 0 ];then
  
  compr_func "$1"
  if [ $? -ne 0 ];then
   pupdialog --background '#FF8080' --backtitle "initrd.gz: fail" --msgbox "Sorry, could not recognise compression type, unable to update initrd.gz." 0 0
  else
   cd initrd-expanded
   find . | cpio -o -H newc > ../initrd
   sync
   cd ..
   ${COMPREXE} initrd
   sync
   mv -f initrd.${EXT} "$1"
   pupdialog --background '#80FF80' --backtitle "initrd.gz: success" --msgbox "File ${1} has been updated with the contents of /root/initrd-expanded." 0 0
  fi
   
 fi
 pupdialog --background "yellow" --backtitle "initrd.gz: finished" --yesno "Do you want to delete /root/initrd-expanded? If in doubt, please choose Yes" 0 0
 if [ $? -eq 0 ];then
  rox -D /root/initrd-expanded 2>/dev/null
  rm -rf /root/initrd-expanded
 fi
  
else
 
 pupdialog --background "yellow" --backtitle "initrd.gz: expand?"  --yesno "Do you want to open up initrd.gz, and optionally edit it?" 0 0
 if [ $? -eq 0 ];then
  compr_func "$1"
  if [ $? -ne 0 ];then
   pupdialog --background '#FF8080' --backtitle "initrd.gz: fail" --msgbox "Sorry, could not recognise compression type of ${1}, unable to expand it." 0 0
  else
   [ "$1" != "/root/initrd.gz" ] && cp -f "$1" /root/
   mv -f initrd.gz initrd.${EXT}
   ${UNCOMPREXE} initrd.${EXT}
   [ -f /tmp/initrd.gz ] && mv -f /tmp/initrd.gz /root/
   mkdir initrd-expanded
   cd initrd-expanded
   cat ../initrd | cpio -i -d -m
   sync
   rm -f ../initrd
   cd ..
   pupdialog --colors --background '#80FF80' --backtitle "initrd.gz: expanded" --msgbox "File initrd.gz has been expanded at \Zb/root/initrd-expanded\ZB. You may edit the contents if you wish. \Zb\Z1\n\nAfterward, if you click on ${1} again\Zn\ZB, it will be updated with the contents of /root/initrd-expanded." 0 0
   rox -d /root/initrd-expanded -x /root/initrd-expanded
  fi
 fi

fi

[ -f /tmp/initrd.gz ] && rm -f /tmp/initrd.gz
###END###
