#!/bin/bash

installSwapfile()
{
    if [[ "$CFG_REQUIREMENT_SWAPFILE" == "true" ]]; then
        if [ ! -f "$swap_file" ]; then
            isHeader "Increasing Swapfile"
            ISSWAP=$( (runAsManager swapoff /swapfile) 2>&1 )
            if [[ "$ISSWAP" != *"No such file or directory"* ]]; then
                local result=$(runAsManager swapoff /swapfile)
                isSuccessful "Turning off /swapfile (if needed)"
            fi

            local result=$(runAsManager fallocate -l $CFG_SWAPFILE_SIZE /swapfile)
            checkSuccess "Allocating $CFG_SWAPFILE_SIZE to the /swapfile"
            
            local result=$(sudo chmod 0600 /swapfile)
            checkSuccess "Adding permissions to the /swapfile"

            local result=$(runAsManager mkswap /swapfile)
            checkSuccess "Swapping to the new /swapfile"

            local result=$(runAsManager swapon /swapfile)
            checkSuccess "Enabling the new /swapfile"
        fi
    fi
}
