#!/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. mime type application/initramfs[-gz]
#161212 'easyinit' is an uncompressed cpio archive, mime type application/x-cpio. note, this script has become very clumsy.

[ ! $1 ] && exit 1
[ ! -f "$1" ] && exit 1
BASEFILE="`basename "$1"`"
#161212 acceptable names...
BADNAME=1
case $BASEFILE in
 initrd.gz|initrd.q|easyinit) BADNAME=0 ;;
esac
[ $BADNAME -eq 1 ] && exit 1

COMPTYPE=''
[ "`echo -n "$BASEFILE" | rev | cut -c 1,2 | rev`" == ".q" ] && COMPTYPE='none'
[ "$BASEFILE" == "easyinit" ] && COMPTYPE='none' #161212

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

if [ -d initrd-expanded ];then
 
 pupdialog --background "yellow" --backtitle "${BASEFILE}: update?" --yesno "A CPIO-archive 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
   case $BASEFILE in #161212
    easyinit)
     mv -f initrd "$1"
    ;;
    *)
     [ -f initrd ] && mv -f initrd initrd.q
     mv -f initrd.${EXT} "$1"
    ;;
    esac
    
   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/${BASEFILE}" ] && cp -f "$1" /root/ #161212
   [ "${BASEFILE}" == "initrd.gz" ] && mv -f initrd.gz initrd.${EXT} 2>/dev/null
   [ -f initrd.${EXT} ] && ${UNCOMPREXE} initrd.${EXT} #161212
   [ -f initrd.q ] && mv -f initrd.q initrd
   [ -f easyinit ] && mv -f easyinit initrd #161212
   [ -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
[ -f /tmp/easyinit ] && rm -f /tmp/easyinit #161212
###END###
