#!/bin/sh

################################################################################
#      Copyright (C) 2009-2010 OpenELEC.tv
#      http://www.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, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

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

. /etc/profile

INSTALLED_MEMORY=`cat /proc/meminfo | grep 'MemTotal:' | awk '{print $2}'`
SWAP=`blkid -t TYPE="swap" -o device`
SWAPFILE="$HOME/.cache/swapfile"

if [ "$INSTALLED_MEMORY" -le "500000" ]; then
  if [ -z "$SWAP" -a ! -f "$SWAPFILE" ]; then
    progress "creating swapfile"
    mkdir -p $HOME/.cache
    dd if=/dev/zero of=$SWAPFILE bs=1024 count=262144 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

  if [ "$INSTALLED_MEMORY" -le "260000" ]; then
    sysctl -w vm.swappiness=100 2>&1 > /dev/null
  else
    sysctl -w vm.swappiness=60 2>&1 > /dev/null
  fi
else
  sysctl -w vm.swappiness=0 2>&1 > /dev/null
fi

