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

# Comprueba la opcin:
if [[ ! -f ${TMP}/set-swapfile ]]; then
  exit;
fi

dialog --title "SWAP FILE" --form \
"You have selected to make the swap file (before).\n\n\
Determine the size of the new swap file in megabytes,\n\
and multiply it by 1024 to determine the number of blocks.\n\
For example, the block size of 128MB swap file is 131072.\n\n\
Note: \`dd' will be used to create the swap file." 15 62 0 \
 "bs    =" 1 1 "1024"   1 10 10 0 \
 "count =" 2 1 "131072" 2 10 10 0 \
  2> ${TMP}/return-mk_swpfile
if (( $? == 0 )); then
  BS=$(sed -n '1p' ${TMP}/return-mk_swpfile)
  COUNT=$(sed -n '2p' ${TMP}/return-mk_swpfile)
  dd if=/dev/zero of=/mnt/swapfile bs=$BS count=$COUNT 1> $OUTPUT_TTY 2>&1
  if (( $? == 0 )); then
    printf "%-15s %-15s %-11s %-16s %-5s %s\n" \
     "/swapfile" "swap" "swap" "defaults" "0" "0" >> ${TMP}/fstab
    dialog \
     --title "/mnt/swapfile" --sleep 1 --infobox "Activating swap file..." 3 27
    swapoff -v /mnt/swapfile &>/dev/null
    chmod 0600 /mnt/swapfile &>${OUTPUT_TTY}
    mkswap -v1 -f /mnt/swapfile &>${OUTPUT_TTY}
    swapon -f /mnt/swapfile
    swapon -s &>${OUTPUT_TTY}
  else
    rm -f /mnt/swapfile
  fi
fi

