#!/bin/bash
#
#  Copyright (c) 2017, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
# Purpose:
#  This script creates a swap file, some platforms do not have enough
#  physical RAM to compile the C++/Boost features of the border router.
#

SWAP_REQUIRED=${SWAP_REQUIRED:-false}
SWAP_FILENAME=${SWAP_FILENAME:-/swapfile}
# 1M * 1024 = 1G swapfile
SWAP_BLOCK_SIZE=${SWAP_BLOCK_SIZE:-1M}
SWAP_BLOCK_CNT=${SWAP_BLOCK_CNT:-1024}

delete_old_swapfile()
{
    # Delete the old
    if [ -f "${SWAP_FILENAME}" ]; then
        echo "Disable & remove old swapfile"
        sudo swapoff -a
        sudo rm -f "${SWAP_FILENAME}"
    fi
}

create_new_swapfile()
{
    echo "Create new (zeroed) swapfile"
    SWAP_STEPSIZE=64
    for x in $(seq "$SWAP_STEPSIZE" "$SWAP_STEPSIZE" "$SWAP_BLOCK_CNT"); do
        echo "Block: ${x} of ${SWAP_BLOCK_CNT}"
        sudo dd oflag=append conv=notrunc if=/dev/zero of="${SWAP_FILENAME}" bs="$SWAP_BLOCK_SIZE" count="$SWAP_STEPSIZE"
        # this actually creates 1 extra but we don't care
    done

    echo "Setting protections"
    sudo chmod 0600 "${SWAP_FILENAME}"

    echo "Format swapfile"
    sudo mkswap "${SWAP_FILENAME}"
}

add_to_fstab()
{
    echo "Adding ${SWAP_FILENAME} to /etc/fstab"
    rm -f /tmp/fstab.tmp
    grep -v "^${SWAP_FILENAME}" /etc/fstab >>/tmp/fstab.tmp
    echo "${SWAP_FILENAME} none swap defaults 0 0" >>/tmp/fstab.tmp
    sudo cp /tmp/fstab.tmp /etc/fstab
}

enable_swapfile()
{
    echo "Enabling swap space..."
    sudo swapon -a
}

setup_swapfile()
{
    if ${SWAP_REQUIRED}; then
        echo "Swapfile: Creating..."
        delete_old_swapfile
        create_new_swapfile
        add_to_fstab
        enable_swapfile
        echo "Swapfile: Complete"
    else
        echo "Swapfile: not required"
    fi
}
