#!/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.
#180408 handle nvme drives.
#181129 renamed 'initrd.q' to just 'initrd'. improved fixing BOOT_SPECS

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

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

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

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

if which rox >/dev/null ;then #181129
 MTYPE="$(rox -m ${PARAM1})"
 [ "${MTYPE}" == "application/x-cpio" ] && COMPTYPE='none'
else
 MTYPE="$(file ${PARAM1} | grep -o 'ASCII cpio archive')"
 [ "${MTYPE}" == "ASCII cpio archive" ] && COMPTYPE='none'
fi

compr_func() {
 #find out compression type...  181129...
 if [ "$COMPTYPE" == "none" ];then
  UNCOMPREXE=''; COMPREXE=''; dotEXT='.q'
  [ "$BASEFILE" == "easyinit" ] && dotEXT='' #161212
  [ "$BASEFILE" == "initrd" ] && dotEXT=''
  return 0
 fi
 UNCOMPREXE=gunzip; COMPREXE=gzip; dotEXT='.gz'
 gunzip -t "$1"
 if [ $? -ne 0 ];then
  UNCOMPREXE=bunzip2; COMPREXE=bzip2; dotEXT='.bz2'
  bunzip2 -t "$1"
  if [ $? -ne 0 ];then
   UNCOMPREXE=unxz; COMPREXE=xz; dotEXT='.xz'
   unxz -t "$1"
   if [ $? -ne 0 ];then
    return 1
   fi
  fi
 fi
 return 0
}

#171016 check BOOT_SPECS  181129...
if [ ! -d initrd-expanded ];then
 if [ "$COMPTYPE" == "none" ];then
  if [ -f ${DIRPATH}/easy.sfs ];then
   [ -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 ${PARAM1} #expand.
   if [ -f /tmp/edit-initramfs-tmp-expand/BOOT_SPECS ];then
. /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
    
    aBOOT_DEV="$(echo -n "${PARAM1}" | cut -f 3 -d '/')" #ex: extract sdb1 from /mnt/sdb1/...
    aDRV="${aBOOT_DEV%%[1-9]}" #remove number off end.
    aBOOT_DRV="${aDRV%%[1-9]}" #repeat, in case another number on end.
    [ "$aBOOT_DRV" != "sdp" ] && aBOOT_DRV="${aBOOT_DRV%%p}" #ex: mmcblk0p becomes mmcblk0
    aDISKID="$(fdisk -l /dev/${aBOOT_DRV} | grep '^Disk identifier: ' | tr -s ' ' | cut -f 3 -d ' ')" #ex: 0x64685683
    aBOOT_DIR="$(echo -n "$DIRPATH" | cut -f 4- -d '/')" #ex: easy/0.9.9
    aBOOT_PARTNUM="${aBOOT_DEV/*[a-z]/}"
    
    aFS="$(blkid /dev/${aBOOT_DEV} | tr ' ' '\n' | grep '^TYPE=' | cut -f 2 -d '"')"
    if [ "$aFS" != "vfat" ];then
     aWKG_PARTNUM="$aBOOT_PARTNUM"
     aBOOT_FS="$aFS"
     aWKG_FS="$aFS"
     aWKG_DIR="$aBOOT_DIR"
    else
     aWKG_PARTNUM=2
     aBOOT_FS='vfat'
     aWKG_FS='ext4'
     aBOOT_DIR=''
     aWKG_DIR=''
    fi
    
    FIXflg=0
    [ "$aDISKID" != "$BOOT_DISKID" ] && FIXflg=$(($FIXflg+1))
    [ "$aBOOT_DIR" != "$BOOT_DIR" ] && FIXflg=$(($FIXflg+1))
    [ "$Q_DISTRO_VERSION" != "$DISTRO_VERSION" ] && FIXflg=$(($FIXflg+1))
    [ "$aBOOT_PARTNUM" != "$BOOT_PARTNUM" ] && FIXflg=$(($FIXflg+1))
    if [ $FIXflg -gt 0 ];then
     pupdialog --colors --background "#ffc060" --backtitle "${BASEFILE}: $(gettext 'fix?')" --yesno "\Zb$(gettext 'The BOOT_SPECS file inside initrd may need to be fixed')\ZB

$(gettext 'If the initrd file is placed where it will be booted from, it seems that the BOOT_SPECS file is incorrect. Choose the Yes button to automatically fix it.')

$(gettext 'If you choose No, then you will be able to manually edit the BOOT_SPECS file.') \Zb\Z1$(gettext 'If you do not fully understand what is being asked here, choose No.')\Zn\ZB" 0 0
     if [ $? -eq 0 ];then
      sed -i -e "s%^BOOT_DISKID=.*%BOOT_DISKID='${aDISKID}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^WKG_DISKID=.*%WKG_DISKID='${aDISKID}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^BOOT_PARTNUM=.*%BOOT_PARTNUM=${aBOOT_PARTNUM}%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^WKG_PARTNUM=.*%WKG_PARTNUM=${aWKG_PARTNUM}%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^BOOT_FS=.*%BOOT_FS='${aBOOT_FS}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^WKG_FS=.*%WKG_FS='${aWKG_FS}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^BOOT_DIR=.*%BOOT_DIR='${aBOOT_DIR}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      sed -i -e "s%^WKG_DIR=.*%WKG_DIR='${aWKG_DIR}'%" /tmp/edit-initramfs-tmp-expand/BOOT_SPECS
      pupdialog --colors --background "#a0ffa0" --backtitle "${BASEFILE}: $(gettext 'FIXED')" --yesno "\Zb$(gettext 'The file BOOT_SPECS inside initrd has been automatically edited. This is the new content:')\ZB

$(cat /tmp/edit-initramfs-tmp-expand/BOOT_SPECS)

$(gettext 'Where BOOT_DISKID is drive: ') \Zb${aBOOT_DRV}\ZB

$(gettext 'Please study this very carefully. If it is correct, click the Yes button. Otherwise, click the No button, and you may then manually edit the BOOT_SPECS file.')" 0 0
      if [ $? -eq 0 ];then
       #now put it together again...
       find . | cpio -o -H newc > ${PARAM1}
       sync
       cd /
       rm -rf /tmp/edit-initramfs-tmp-expand
       pupdialog --background '#80FF80' --backtitle "${BASEFILE}: $(gettext 'success')" --msgbox "$(gettext 'BOOT_SPECS inside this file has been updated. If you wish, you may click on it again to open it up and check the contents.')" 0 0
      else
       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
       cp -a /tmp/edit-initramfs-tmp-expand /root/initrd-expanded
       sync
       cd /
       rm -rf /tmp/edit-initramfs-tmp-expand
       rox -d /root/initrd-expanded -x /root/initrd-expanded
      fi
      exit
     else
      true
     fi
    fi
   fi
   cd /
   rm -rf /tmp/edit-initramfs-tmp-expand
  fi
 fi
fi

cd /root
[ -f initrd ] && rm -f initrd
[ -f initrd${dotEXE} ] && rm -f initrd${dotEXE}

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 "$PARAM1"
  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
   mv -f initrd${dotEXE} "$PARAM1"
   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 "$PARAM1"
  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
   cp -f "$PARAM1" /root/initrd${dotEXT}
   ${UNCOMPREXE} initrd${dotEXT}
   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

###end###
