#!/bin/bash
# Author: Steven Shiau <steven _at_ clonezilla org>
# License: GPL
#
# This script is used to remove the pagefile and hibernate file 
# The file name: pagefile.sys swapfile.sys and hiberfil.sys
# Thanks to Kristof Vansant for this idea.

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

#
check_if_root

# Settings
files_to_be_rm="pagefile.sys hiberfil.sys swapfile.sys PAGEFILE.SYS HIBERFIL.SYS SWAPFILE.SYS"

# Functions
USAGE() {
  echo "To remove the page file or hibernation file in partition."
  echo "Usage: $0 PARTITION_NAME"
  echo "PARTITION_NAME can be with or without /dev/, e.g., /dev/sda1 or sda1."
  echo "Ex: $0 /dev/sda1"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

partition_list="$(format_dev_name_with_leading_dev $*)"

if [ -z "$partition_list" ]; then
  echo "No partition is assigned!"	 
  USAGE >& 2
  echo "Program terminated!!!"
  exit 1
fi

hd_img="$(mktemp -d /tmp/hd_img.XXXXXX)"
for part in $partition_list; do
  # Check if it's busy.
  # If the partition is not ntfs or fat, skip. Since normally MS windows only uses ntfs or fat.
  fs="$(ocs-get-dev-info $part filesystem)"
  echo "$part partition file system: $fs"
  if [ -z "$(echo "$fs" | grep -iE "(ntfs|fat)")" ]; then
    echo "Not a FAT or NTFS partition. Skip this partition: $part."
    continue
  fi
  case "$fs" in
   ntfs)
     if type ntfs-3g &>/dev/null; then
       echo -n "Mounting $part... "
       ntfs-3g $part $hd_img
       mrc=$?
       if [ "$mrc" -eq 0 ]; then
         echo "done!"
       else
         echo "Fail to mount $part! Skip this partition."
	 continue
       fi
     else
       echo "ntfs-3g is not found! Skip this partition $part."
       continue
     fi
     ;;
   fat|vfat|fat16|fat32)
     echo -n "Mounting $part... "
     mount $part $hd_img >/dev/null 2>&1
     mrc=$?
     if [ "$mrc" -eq 0 ]; then
       echo "done!"
     else
       echo "Fail to mount $part! Skip this partition."
       continue
     fi
     ;;
  esac
  echo "Trying to remove page and hibernation files if they exist..."
  for i in $files_to_be_rm; do
    [ -f "$hd_img/$i" ] && rm -fv $hd_img/$i
    sync;sync;sync
  done
  if [ "$mrc" -eq 0 ]; then 
    unmount_wait_and_try $part
    sleep 1
    sync;sync;sync
  fi
done
# //NOTE// Do not use "rm -rf" here, since if umount fails, all the files of that partition will be removed!
[ -d "$hd_img" -a -n "$hd_img" ] && rmdir $hd_img
echo "Done!"
