#!/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 quirky: 'easyinit' is an uncompressed cpio archive, mime type application/x-cpio. note, this script has become very clumsy.
#171016 easy os: check BOOT_SPECS if a version upgrade. need to internationalise.

export TEXTDOMAIN=edit-initramfs
export OUTPUT_CHARSET=UTF-8

. /etc/DISTRO_SPECS
. /etc/rc.d/PUPSTATE

[ ! $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
}

#171016 check BOOT_SPECS
if [ "$BASEFILE" == "initrd.q" ];then
 DEVFILE="$(echo -n "${1}" | cut -f 3 -d '/')" #ex: extract sdb1 from /mnt/sdb1/...
 if [ "$DEVFILE" == "$BOOT_DEV" ];then #initrd.q is in the boot partition.
  [ -d /tmp/edit-initramfs-tmp-expand ] && rm -rf /tmp/edit-initramfs-tmp-expand
  mkdir /tmp/edit-initramfs-tmp-expand
  cd /tmp/edit-initramfs-tmp-expand
  cpio -i -d -m -F ${1}
. /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
  #if [ "$DISTRO_VERSION" != "$Q_DISTRO_VERSION" ];then
   BOOT_DRVa="${BOOT_DEV/[1-9]*/}" #from PUPSTATE
   BOOT_DRV="${BOOT_DRVa/0*/0}" #mmcblk0p to mmcblk0
   REQ_BOOTDISKID="$(fdisk -l /dev/${BOOT_DRV} | grep '^Disk identifier: ' | tr -s ' ' | cut -f 3 -d ' ')"
   REQ_BOOTPARTNUM=${BOOT_DEV/*[a-z]/}
   if [ "$REQ_BOOTDISKID" != "$BOOT_DISKID" -o "$REQ_BOOTPARTNUM" != "$BOOT_PARTNUM" ];then
    pupdialog --colors --background "#ffa0a0" --backtitle "${BASEFILE}: $(gettext 'fix?')" --yesno "\Zb$(gettext 'The BOOT_SPECS file inside initrd.q needs to be fixed. Please click 'Yes' button...')\ZB

\Zb$(gettext 'Note:')\ZB
$(gettext 'The initrd.q file in the downloaded *.img.gz file, is configured to run in a USB Flash drive. As it is now in a different drive, the BOOT_SPECS file needs to be modified. You must do this, otherwise next bootup will fail.')" 0 0
    if [ $? -eq 0 ];then
     WKG_DRVa="${WKG_DEV/[1-9]*/}" #from PUPSTATE
     WKG_DRV="${WKG_DRVa/0*/0}" #mmcblk0p to mmcblk0
     REQ_WKGDISKID="$(fdisk -l /dev/${WKG_DRV} | grep '^Disk identifier: ' | tr -s ' ' | cut -f 3 -d ' ')"
     echo "BOOT_DISKID='${REQ_BOOTDISKID}'
BOOT_PARTNUM=${BOOT_DEV/*[a-z]/}
BOOT_FS='${BOOT_FS}'
BOOT_DIR='${BOOT_DIR}'
WKG_DISKID='${REQ_WKGDISKID}'
WKG_PARTNUM=${WKG_DEV/*[a-z]/}
WKG_FS='${WKG_FS}'
WKG_DIR='${WKG_DIR}'
Q_DISTRO_VERSION=${Q_DISTRO_VERSION}" > /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
     #now put it together again...
     find . | cpio -o -H newc > ${1}
     sync
     pupdialog --colors --background "#a0ffa0" --backtitle "${BASEFILE}: $(gettext 'FIXED')" --msgbox "\Zb$(gettext 'The file BOOT_SPECS inside initrd.q has been edited.')\ZB
     
\Zb$(gettext 'Technical notes:')\ZB
\Zb1.\ZB $(gettext 'If you click on initrd.q again, there will be an offer to open it up, and you may examine the contents, and optionally make changes.')
\Zb2.\ZB $(gettext 'File BOOT_SPECS has been changed to boot successfully. This is the new content:')
BOOT_DISKID='${REQ_BOOTDISKID}'
BOOT_PARTNUM=${BOOT_DEV/*[a-z]/}
BOOT_FS='${BOOT_FS}'
BOOT_DIR='${BOOT_DIR}'
WKG_DISKID='${REQ_WKGDISKID}'
WKG_PARTNUM=${WKG_DEV/*[a-z]/}
WKG_FS='${WKG_FS}'
WKG_DIR='${WKG_DIR}'
Q_DISTRO_VERSION=${Q_DISTRO_VERSION}" 0 0
     cd /
     rm -rf /tmp/edit-initramfs-tmp-expand
     exit
    else
     pupdialog --colors --background "#ffa0a0" --backtitle "${BASEFILE}: $(gettext 'NOT FIXED')" --msgbox "\Zb$(gettext 'Not fixed, next bootup will fail!')\ZB" 0 0
    fi
   fi
  #fi
  cd /
  rm -rf /tmp/edit-initramfs-tmp-expand
 fi
fi

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}: $(gettext 'update?')" --yesno "$(gettext '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}: $(gettext 'fail')" --msgbox "$(gettext '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}: $(gettext 'success')" --msgbox "${1} $(gettext 'has been updated with the contents of /root/initrd-expanded.')" 0 0
  fi
   
 fi
 pupdialog --background "yellow" --backtitle "${BASEFILE}: $(gettext 'finished')" --yesno "$(gettext '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}: $(gettext 'expand?')"  --yesno "${BASEFILE}: $(gettext 'Do you want to open it up, and optionally edit it?')" 0 0
 if [ $? -eq 0 ];then
  compr_func "$1"
  if [ $? -ne 0 ];then
   pupdialog --background '#FF8080' --backtitle "${BASEFILE}: $(gettext 'fail')" --msgbox "${BASEFILE}: $(gettext 'Sorry, could not recognise compression type, 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}: $(gettext 'expanded')" --msgbox "${BASEFILE} $(gettext '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 it 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###
