#!/bin/sh

# get_partition_type : get patition type
get_partition_type()
{
  fstype=$(blkid /dev/$1)
    fstype=$(expr "$fstype" : '.*TYPE="\([A-Za-z0-9]*\)".*')
}

# If found swap partition, enable this.
cat /proc/diskstats | sort -r | while read maj min dev other ;
do
  get_partition_type "$dev"
  if [ "$fstype" = "swap" ];
  then
    swapon /dev/$dev
  fi
done

# If running in Extreme mode (Not squashfs), alloc the swap file.
memory_size_kb=$( free | grep Mem | awk '{print $2}' )

if [ ! -f /.bootparam ];
then
  if [ ! -f /.swapfile ];
  then
    # if memory size is under 96MB...
    if [ $memory_size_kb -lt 98304 ];
    then
      # Make swapfile
      dd if=/dev/zero of=/.swapfile bs=1M count=32
      chmod 600 /.swapfile
      mkswap /.swapfile
    fi
  fi

  if [ -f /.swapfile ];
  then
    # Mount swapfile
    swapon /.swapfile
  fi
fi

