#!/bin/sh

# get target path of new system
TARGET="${1}"
FOLDER_PATH="${2}"
SIZE="${3}"

# create folder if not exist
if [ ! -d ${TARGET}/${FOLDER_PATH} ] 
then
    chroot ${TARGET} mkdir -p ${FOLDER_PATH}
fi

# create new swap-file with size 1G
chroot ${TARGET} fallocate -l ${SIZE}G ${FOLDER_PATH}/swapfile

# allow write and read root only
chroot ${TARGET} chmod 600 ${FOLDER_PATH}/swapfile

# mark new swap-file as swap space
chroot ${TARGET} mkswap ${FOLDER_PATH}/swapfile

# write changes to mounting conf-file /etc/fstab
echo >> ${TARGET}/etc/fstab
echo "${FOLDER_PATH}/swapfile swap swap defaults 0 0" >> ${TARGET}/etc/fstab
