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

DISTRO="Dragora GNU/Linux"
VERSION="$DISTRO 2.2"

PATH=${PATH}:/usr/lib/setup

TMP=${TMP:-/var/tmp/installer}
rm -rf $TMP
mkdir --mode=700 "$TMP"

OUTPUT_TTY=${OUTPUT_TTY:-/dev/tty4}
printf "%s\n\n" >${OUTPUT_TTY}  # Asigna unos espacios a la terminal.

export PATH TMP OUTPUT_TTY

# Activar particiones LVM (si hay alguna):
vgchange -a y 1> $OUTPUT_TTY 2>&1

# Detecta si hay al menos una particin "Linux":
if probe -l 2> /dev/null | grep -q -m 1 'Linux$' ; then
  probe -l | grep 'Linux$' | sort 1> ${TMP}/SeTplist 2> /dev/null
else
  dialog --title "Linux partition NOT DETECTED" --msgbox \
"We did not detect any Linux partitions \
on your machine, needs at least one partition for \
install GNU/Linux.\n\nIt is recommended to create a \
Linux partition.\n\n\
Use 'fdisk' or 'cfdisk' and then, re-run the \
installer." 11 60
  exit 99;
fi

# Muestra el men (loop) principal:
while : ; do
  dialog \
   --backtitle "$VERSION" \
   --title "MAIN MENU" \
   --no-cancel \
   --menu \
"The installer of ${DISTRO}.\n\n\
Use the arrow keys:\n\n\
UP/DOWN, +/-, ENTER, SPACE and TAB.\n\n\
For more information, see the help file.\n\n\
Options:" 19 55 4 \
   "Help"      "View the help file" \
   "Keyboard"  "Changing the keymap" \
   "Start"     "Initial installation" \
   "Exit"      "Exit the installer" \
    2> ${TMP}/return-main-menu
  CODE=$?
  if (( $CODE != 0 )); then
    dialog --clear
    exit $CODE;
  fi
  # Mira por las opciones:
  case "$(cat ${TMP}/return-main-menu)" in
    Help)
      dialog --backtitle "$VERSION" --textbox "/usr/lib/setup/HELP.TXT" 21 78
      ;;
    Keyboard)
      /usr/lib/setup/keyconfig
      ;;
    Start)
      break;
      ;;
    Exit)
      dialog --clear
      exit;
      ;;
  esac
done

# El resto de las partes.

mk_swap
CODE=$?
if (( $CODE == 99 )); then
  exit $CODE;
fi
# Desactiva el archivo swap (segn opcin):
if swapon -s | grep -q -m1 swapfile ; then
  swapoff -v /mnt/swapfile 1> /dev/null 2>&1
fi

mk_fs
if (( $? == 3 )); then  # Opcin "Done":
  mount-root
  CODE=$?
  if (( $CODE == 99 )); then
    break;
  fi

  mk_swapfile
  # Comprueba el archivo-swap para que tome lugar:
  if [[ -f /mnt/swapfile ]]; then
    if ! swapon -s | grep -q -m1 swapfile ; then
      swapon -f /mnt/swapfile 1> /dev/null 2>&1
    fi
  fi

  fstab_edit
  fstab_update
else  # Opcin "Main Menu" o ESC.
  exec $0
fi

menu-medium
CODE=$?
if (( $CODE != 0 )); then
  exit $CODE;
fi

# Determina el medio escogido desde el cual se instalar el sistema:
if grep -q dev/ ${TMP}/SRC_INS ; then
  SRC_INS=/cdrom/packages
else  # Desde el disco rgido:
  SRC_INS=$(cut -f1 -d ' ' ${TMP}/SRC_INS)
  SRC_INS=${SRC_INS}/packages
fi
export SRC_INS

menu-series
CODE=$?
if (( $CODE != 0 )); then
  rm -f ${TMP}/show_series
  exit $CODE;
fi

run-install
CODE=$?
if (( $CODE != 0 )); then
  exit $CODE;
fi
sleep 1

run-updates

set-kernel

# Es necesario linkear /dev con /mnt/dev, para poder instalar GRUB,
# y adems para que passwd(1) no falle en el entorno chroot(1):
umount /mnt/dev &>/dev/null
mkdir -p /mnt/dev
mount -v --bind /dev /mnt/dev &>${OUTPUT_TTY}

menu-bootloader
CODE=$?
if (( $CODE != 0 )); then
  exit $CODE;
fi
set-passwd

set-keymap

conf-mouse
conf-services
conf-services-rc.d
conf-wm
conf-locale
conf-timezone

# Desmontamos /mnt/dev:
umount -v /mnt/dev &>${OUTPUT_TTY}

sleep 1

if [[ -f /mnt/etc/fstab ]]; then
  printf "\n%s\n\n" "Installation complete."

  if mountpoint -q /cdrom ; then
    umount /cdrom > /dev/null 2>&1
    if [[ -r ${TMP}/SRC_INS ]]; then
      echo "Ejecting the CD/DVD-ROM drive..."
      eject $(cut -f1 -d ' ' ${TMP}/SRC_INS) && \
      echo "Remove the CD/DVD-ROM and then..."
    fi
  fi

  printf "\n%s\n" "Press Ctrl-Alt-Del to restart."
  exit 0;
fi

