#!/bin/bash
# Under the terms of the 'COPYRIGHT.TXT' file.

# Funciones:
add_filesystems() {
  ( cd $TMP
    printf "%-15s %-15s %-11s %-16s %-5s %s\n" \
     "#/dev/cdrom" "/mnt/cdrom" "iso9660,udf" "noauto,owner,ro" "0" "0" >> fstab

    printf "%-15s %-15s %-11s %-16s %-5s %s\n" \
     "/dev/fd0" "/mnt/floppy" "auto" "noauto,owner,rw" "0" "0" >> fstab

    printf "%-15s %-15s %-11s %-16s %-5s %s\n" \
     "proc" "/proc" "proc" "defaults" "0" "0" >> fstab
  )
}

mk_mountpoint() {
  arr=( $(egrep -v '^#|swap|/ ' ${TMP}/fstab | awk '{ print substr($2,2) }') )
  for directory in "${arr[@]}"; do
    if [[ -n $directory ]]; then
      if [[ ! -d /mnt/${directory} ]]; then
        mkdir -p /mnt/${directory}
      fi
    fi
  done
}

mount_nonroot() {
  if [[ -r ${TMP}/fstab ]]; then
    awk '!/^#/ && !/\/ / && !/swap/ { print $1,$2 }' ${TMP}/fstab | \
      while read line ; do
        if [[ -n $line ]]; then
          devname=${line% *}
          mountpt=${line#* }
          umount $devname &>/dev/null
          mount -v $devname /mnt${mountpt} &>${OUTPUT_TTY}
        fi
      done
  fi
}

# Verifica si hay un fstab existente en la particin Root antes
# de copiar el fstab temporal definitivo en la particin Root:
if [[ ! -r /mnt/etc/fstab ]]; then
  mk_mountpoint
  mount_nonroot
  add_filesystems
  mkdir -p /mnt/etc
  cat ${TMP}/fstab > /mnt/etc/fstab
else
  dialog \
   --title "TABLE OF FILE SYSTEM (fstab)" \
   --yesno \
"A table of the file system already exists in your \
Root partition.\n\nOverwrite the file system table, \
only if:\n\n\
- This is a new installation.\n\
- You are installing a new kernel image.\n\
- To create a boot disk with the new kernel.\n\n\
In this case, you must configure the system to\n\
boot properly.\n\n\
Do not overwrite the file system table, only if:\n\n\
- If you are adding software to the existing system.\n\n\
In this case, configure the system and end the installation.\n\n\
Do you want to overwrite the existing fstab for the new?" 0 0
  if (( $? == 0 )); then
    mk_mountpoint
    mount_nonroot
    add_filesystems
    mkdir -p /mnt/etc
    cat ${TMP}/fstab > /mnt/etc/fstab
  fi
fi

