#!/bin/bash

ME=${0##*/}

export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/live/bin

BOOT_UUID_FNAME="bios-dev-uuid"
HOME="/root"

    GRUB_CFG="/boot/grub/grub.cfg"
SYSLINUX_CFG="/boot/syslinux/syslinux.cfg"

usage() {
    cat<<Usage
Usage: $ME <editor> [editor options]

Only for use on the live system.  Will try to find the
syslinux.cfg file and grug.cfg file for this live system
and then call:

    <editor> <editor options> <config files>

Must be called as root since we may need to mount partitions.
Usage
    exit 0
}

main() {
    [ $# -lt 1 ] && usage

    [ "$(id -u)" -eq 0 ] || fatal "$ME must be run as root"

    local orig=$1  editor=$1; shift
    test -x $editor || editor=$(which $editor 2>/dev/null)
    test -x $editor || fatal "Could not find editor %s" "$orig"

    if ! [ -e /live/config/remasterable ]; then
        fatal "Can't update %s on read-only boot media" "$(pquote gfxboot)"
        return 1
    fi

    . /live/config/initrd.out
    BOOT_LOADER_MP=$BOOT_MP

    # Change to /live/bios if needed (changed the called scripts)
    local uuid_file="$SQFILE_DIR/$BOOT_UUID_FNAME"
    if ! mount_boot_partition "$uuid_file"; then
        fatal "Could not mount the BIOS boot partition"
        exit 1
    fi

    local missing found
    local file_1="$BOOT_LOADER_MP$SYSLINUX_CFG" 
    echo $file_1
    if test -w "$file_1"; then
        set -- "$@" "$file_1"
        found=true
    else
        missing="$SYSLINUX_CFG"
    fi

    local file_2="$BOOT_LOADER_MP$GRUB_CFG"
    echo $file_2
    if test -w "$file_2"; then
        set -- "$@" "$file_2"
        found=true
    else
        missing="$missing $SYSLINUX_CFG"
    fi

    [ "$found" ] || fatal "Could not find any files to edit"

    [ -n "$missing" ] && warn "Could not find file(s) %s" "$missing"

    echo "$editor" "$@"     
    [ -n "$DEBUG" ] && exit 0
    "$editor" "$@"
}

#------------------------------------------------------------------------------
# Mount out bios/boot partition if we were encrypted or frugal
#------------------------------------------------------------------------------
mount_boot_partition() {
    local uuid_file=$1

    # If there is no uuid file then proceed as normal
    test -r "$uuid_file" || return 0

    echo "uuid file: $uuid_file"

    local boot_uuid=$(cat $uuid_file | head -n1 2>/dev/null)
    echo "boot uuid; $boot_uuid"
    mount_boot_dev "$boot_uuid" $BIOS_MP || return 1
}

#------------------------------------------------------------------------------
# Mount boot device for case of frugal install or encrypted
# Set BOOT_LOADER_MP to the mount point
#------------------------------------------------------------------------------
mount_boot_dev() {
    uuid=$1  mp=$2

    debug "uuid=$uuid  mp=$mp"

    if [ -z "$uuid" ]; then
        fatal "Could not find UUID of boot device for remounting"
        return 1
    fi

    local boot_dev=$(blkid -U $uuid)
    debug "boot_dev: $boot_dev"
    if [ -z "$boot_dev" ]; then
        fatal "Could not find boot device for remounting"
        return 1
    fi

    local existing_mp=$(grep "^$boot_dev " /proc/mounts | cut -d" " -f2)
    debug "existing_mp=$existing_mp"
    if [ -n "$existing_mp" ]; then
        BOOT_LOADER_MP=$existing_mp
        return 0
    fi

    echo_cmd mkdir -p $mp
    echo_cmd mount UUID=$uuid $mp
    if ! grep -q "^$boot_dev $mp " /proc/mounts; then
        fatal "Could not remount boot device"
        return 1
    fi
    TO_UMOUNT="$mp"

    BOOT_LOADER_MP=$mp
    return 0
}

debug() { return; echo "db: $*"; }

echo_cmd() { echo " > $*" >/dev/null ; "$@" ; }

fatal() {
    local fmt=$1 ; shift
    printf "$ME Error: $fmt\n" "$@" >&2
    yad_box Error "$fmt" "$@"
    exit 2
}

warn() {
    local fmt=$1 ; shift
    printf "$ME Warning: $fmt\n" "$@" >&2
    yad_box Warning "$fmt" "$@"
}

yad_box() {
    local type=$1  fmt=$2 ; shift 2
    [ -n "$DISPLAY" ]      || return
    which yad &> /dev/null || return
    local txt=$(printf "$fmt"  "$@")
    yad --title="$ME: $type" \
        --width=500 --height=200 --center \
        --text-align=center --text="\n\n$txt"
}

main "$@"
