#!/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.
#150215 'initrd.q' cpio archive is not compressed.

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

COMPTYPE=''
if [ "`echo -n "$1" | rev | cut -c 1,2 | rev`" == ".q" ];then
 COMPTYPE='none'
fi

compr_func() {
 #find out compression type...
 if [ "$COMPTYPE" == "none" ];then
  UNCOMPREXE=''; COMPREXE=''; EXT='q'
  return 0
 fi
 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
[ -f /tmp/initrd.q ] && rm -f /tmp/initrd.q
[ "$1" = "/root/initrd.gz" ] && cp -f /root/initrd.gz /tmp/
[ "$1" = "/root/initrd.q" ] && cp -f /root/initrd.q /tmp/

if [ -d initrd-expanded ];then
 
 pupdialog --background "yellow" --backtitle "${BASEFILE}: update?" --yesno "An initrd 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 "${BASEFILE}: fail" --msgbox "Sorry, could not recognise compression type, unable to update ${BASEFILE}." 0 0
  else
   cd initrd-expanded
   find . | cpio -o -H newc > ../initrd
   sync
   cd ..
   ${COMPREXE} initrd
   sync
   [ -f initrd ] && mv -f initrd initrd.q
   mv -f initrd.${EXT} "$1"
   pupdialog --background '#80FF80' --backtitle "${BASEFILE}: success" --msgbox "File ${1} has been updated with the contents of /root/initrd-expanded." 0 0
  fi
   
 fi
 pupdialog --background "yellow" --backtitle "${BASEFILE}: 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 "${BASEFILE}: expand?"  --yesno "Do you want to open up ${BASEFILE}, and optionally edit it?" 0 0
 if [ $? -eq 0 ];then
  compr_func "$1"
  if [ $? -ne 0 ];then
   pupdialog --background '#FF8080' --backtitle "${BASEFILE}: fail" --msgbox "Sorry, could not recognise compression type of ${1}, unable to expand it." 0 0
  else
   [ "$1" != "/root/initrd.gz" -a "$1" != "/root/initrd.q" ] && cp -f "$1" /root/
   [ "${BASEFILE}" == "initrd.gz" ] && mv -f initrd.gz initrd.${EXT} 2>/dev/null
   ${UNCOMPREXE} initrd.${EXT}
   [ -f initrd.q ] && mv -f initrd.q initrd
   [ -f /tmp/initrd.gz ] && mv -f /tmp/initrd.gz /root/ #why?
   mkdir initrd-expanded
   cd initrd-expanded
   cat ../initrd | cpio -i -d -m
   sync
   rm -f ../initrd
   cd ..
   pupdialog --colors --background '#80FF80' --backtitle "${BASEFILE}: expanded" --msgbox "File ${BASEFILE} 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
[ -f /tmp/initrd.q ] && rm -f /tmp/initrd.q
###END###
