#!/bin/bash
echo_exit() {
    echo "==> $1"
    exit $2
}

clear() {
    (umount "$ISO_MP" || umount -l "$ISO_MP"
    rmdir "$ISO_MP"
    rm -rf "$ISO_ROOT") 2>/dev/null
}


HLP() {
    echo "$0 - Util to rebuild Rosa iso for oem-install"
    echo -e "See: /usr/share/doc/oem-install/README-ru.md\n"
    exit 0
}


[ "$1" ] || echo_exit "Please, specify ISO image as \$1" ${LINENO}

# Show help if requested
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    HLP
fi

[ $(id -un) != "root" ]  && echo_exit  "$0 - need root permissions" ${LINENO}

ISO=$(realpath "$1")

[ -f "$ISO" ] || echo_exit "${ISO}: file not found" ${LINENO}


LABEL=$(xorriso -indev $ISO 2>&1 |sed -n "/Volume id/s/Volume.*\ //p" |sed "s/'//g")

OEM_ISO=$(echo $ISO |sed 's/\.iso/.oem.iso/')
ISO_MP=$(realpath ./ISO_MP)
ISO_ROOT=$(realpath ./ISO_ROOT)

trap 'clear' EXIT

_mkiso() {
    LABEL=$1
    xorrisofs \
-output \
${LABEL}.iso \
-no-emul-boot \
-b \
eltorito.img \
-no-emul-boot \
-boot-load-size \
4 \
-boot-info-table \
-partition_offset \
16 \
--boot-catalog-hide \
--grub2-mbr \
/usr/lib/grub/i386-pc/boot_hybrid.img \
--grub2-boot-info \
-iso_mbr_part_type \
0x83 \
-append_partition \
2 \
0xef \
./efi.img \
-appended_part_as_gpt \
-eltorito-alt-boot \
-e \
/efi.img \
-no-emul-boot \
-no-pad \
-rational-rock \
-joliet \
-volid \
$LABEL \
.

}

mkdir -p "$ISO_MP" "$ISO_ROOT"
mount -o loop "$ISO" "$ISO_MP" 2>/dev/null
cp -fr ${ISO_MP}/* ${ISO_ROOT}/
umount "$ISO_MP"

# fix for different platforms 
grub_cfg='EFI/BOOT/menuentry.cfg'
[ -f ${ISO_ROOT}/$grub_cfg ] || grub_cfg='EFI/BOOT/grub.cfg'

[ -z $LABEL ] && \
LABEL="$(sed -n '/root=live:LABEL=/s/\(^.*root=live:LABEL=\)\(\S\+\)\(.*$\)/\2/1p' ${ISO_ROOT}/$grub_cfg |head -n1)"
echo "ISO label:         $LABEL"
echo "OEM iso file name: $OEM_ISO"

sed -ni '1,/'$LABEL'/p'  "${ISO_ROOT}/$grub_cfg"

cat << "EOF" >> "${ISO_ROOT}/$grub_cfg"
if test "$bootis" == 'UEFI:' ; then
set timeout=1
set default=0
menuentry "$installer $OS" --class rosa --class gnu-linux --class gnu --class os {
    linux /vmlinuz0 root=live:LABEL=@LABEL@ ro rd.live.image quiet rhgb oem.install rhgb splash=silent logo.nologo  inst.lang=$lang_utf locale.LANG=$lang_utf locale.LANGUAGE=$lang_utf
        echo "$loading /vmlinuz0..."
    initrd /initrd0.img
        echo "$loading /initrd0.img..."
}

# menuentry "$OS live" --class rosa --class gnu-linux --class gnu --class os {
#     linux /vmlinuz0 root=live:LABEL=@LABEL@ ro rd.live.image quiet  rhgb  rhgb splash=silent logo.nologo  inst.lang=$lang_utf locale.LANG=$lang_utf locale.LANGUAGE=$lang_utf
#         echo "$loading /vmlinuz0..."
#     initrd /initrd0.img
#         echo "$loading /initrd0.img..."
# }

menuentry "$backToLang" --hotkey 'f2' --id 'lang' {
    configfile ${prefix}/lang.cfg
}
menuentry  "$reboot" {
    reboot
}
else
menuentry  "Legacy bios boot is not supported by oem-install" {
    reboot
}
fi
EOF

sed -i 's/@LABEL@/'$LABEL'/g' "${ISO_ROOT}/$grub_cfg"

echo "
You may add the following files to the root of 
${ISO_ROOT}:

- oem-install.cfg
- anaconda-ks.cfg
- oem-install.css 

Then press ENTER to begin building the ISO, or ctrl-c to abort.
"
read qqq
pushd "${ISO_ROOT}" > /dev/null
 _mkiso $LABEL && echo "Done!"
 mv "$(basename $ISO)" "$OEM_ISO"
popd >/dev/null
