# This file is part of the 'dragora-installer'.
#
# Purpose: Edition of the file system table (fstab).

# Re-order fstab adding information

sort -k 1 "${SUBTMPDIR}/fstab" | uniq > "${SUBTMPDIR}/fstab.sorted"

# To avoid populating the fstab twice (eg. when 'PRESERVE' is given)
if test ! -e "${SUBTMPDIR}/FstabEdit.flag"
then
    cat << EOF > "${SUBTMPDIR}/fstab"
#
# /etc/fstab - Static information about the filesystems.
#
# For more information, type "man 5 fstab".
#
# Filesystem				Mountpoint	Type		Options		Dump|Check
EOF
    cat "${SUBTMPDIR}/fstab.sorted" >> "${SUBTMPDIR}/fstab"

    printf '%-43s %-14s %-12s %-16s %-3s %s\n' \
     "/dev/fd0"    "/media/floppy" "auto"       "noauto,owner,rw"     "0" "0" \
     "#/dev/cdrom" "/media/cdrom" "iso9660,udf" "noauto,owner,ro"     "0" "0" \
     "tmp"         "/tmp"         "tmpfs"       "defaults"            "0" "0" \
     >> "${SUBTMPDIR}/fstab"

    touch "${SUBTMPDIR}/FstabEdit.flag"
fi

# Show menu to edit the produced fstab (if desired)

dialog --no-shadow --colors \
 --backtitle "\\ZbFile system table information" \
 --title "FILE SYSTEM TABLE EDITOR" \
 --ok-label "Save / Continue" --no-cancel \
 --defaultno --editbox "${SUBTMPDIR}/fstab" \
 $((LINES - 8)) $COLUMNS 2> "${SUBTMPDIR}/fstab.edit"

# Check for differences and offer menu to view or apply changes

diff -u "${SUBTMPDIR}/fstab" "${SUBTMPDIR}/fstab.edit" \
 > "${SUBTMPDIR}/fstab.diff" 2> /dev/null || true

if test -s "${SUBTMPDIR}/fstab.diff"
then
    while true
    do
        dialog --colors \
         --backtitle "\\ZbFile system table modification" \
         --title "FILE SYSTEM TABLE FROM THE INSTALLER" \
         --help-button --help-label "View Diff" \
         --cr-wrap --yesno \
"The file system table has been modified.

Would you like to apply the changes made?.  If you choose 'No',
the installer will continue with the default fstab file.
" 8 66 || _status=$?

        case "${_status:-0}" in
        0) ## Button "Yes"
            cp -f -- "${SUBTMPDIR}/fstab.edit" "${SUBTMPDIR}/fstab"
            break;
            ;;
        1) ## Button "No"
            break;
            ;;
        2) ## Button "View Diff"
            dialog --no-shadow --colors \
             --backtitle \
"\\ZbFile system table modification (unified difference)" \
             --title "FILE SYSTEM TABLE CHANGES" \
             --exit-label "Return" \
             --cr-wrap --textbox "${SUBTMPDIR}/fstab.diff" \
             $((LINES - 8)) $COLUMNS || continue;
            ;;
        *) ## "Escape from L.A"
            unset -v _status
            return 99;
        esac
    done

    unset -v _status
fi

