################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

#
# create and enable swapfile
#
# runlevels: openelec, textmode

. /etc/swap.conf

if [ -f $HOME/.config/swap.conf ]; then
  . $HOME/.config/swap.conf
fi

(
  if [ -e /proc/swaps ]; then
    SWAP=`blkid -t TYPE="swap" -o device`

    if [ -z "$SWAP" -a ! -f "$SWAPFILE" ]; then
      progress "creating swapfile"
      mkdir -p `dirname $SWAPFILE`
      dd if=/dev/zero of=$SWAPFILE bs=1024 count=$SWAPFILESIZE 2>&1 > /dev/null
      mkswap $SWAPFILE 2>&1 > /dev/null
    fi

    progress "enable swap"
      [ -z "$SWAP" -a -f "$SWAPFILE" ] && SWAP=$SWAPFILE
      for i in $SWAP; do
        swapon -p 10000 $SWAP 2>&1 > /dev/null
      done
      sysctl -w vm.swappiness=1 2>&1 > /dev/null
  fi
)&
