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

# Re-order fstab adding information

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

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"  \
 "proc"        "/proc"         "proc"       "nosuid,noexec,nodev" "0" "0"  \
 >> "${SUBTMPDIR}/fstab"

# 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
        _status=0

        dialog --colors \
         --backtitle "\ZbFile system table modification" \
         --title "CURRENT FILE SYSTEM TABLE" \
         --help-button --help-label "View Diff" \
         --yesno \
"The file system table has been modified.\n\n\
Would you like to apply the changes made?.  If you say \"No\",\n\
the installer will continue with the default fstab." 9 65 || _status=$?

        case $_status 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;
            ;;
        *) ## Any other code
            return $_status
            ;;
        esac
    done
fi

unset _status

