#!/bin/bash
# -*- mode: sh; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# vim: et sts=4 sw=4

#  SPDX-License-Identifier: LGPL-2.1+
#
#  Copyright © 2019-2021 Collabora Ltd.
#  Copyright © 2019-2021  Valve Corporation.
#
#  This file is part of steamos-customizations.
#
#  steamos-customizations is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of the License,
#  or (at your option) any later version.

set -euo pipefail

SWAPFILE=${1:-}
SWAPSIZE=${2:-}

fail()  { echo >&2 "$@"; exit 1; }
usage() { echo >&2 "Usage: $(basename $0) FILE SIZE"; exit 1; }

[ "$SWAPFILE" ] || usage
[ "$SWAPSIZE" ] || usage

[ -e "$SWAPFILE" ] && fail "File '$SWAPFILE' already exists"

BASE_DIR=$(dirname "$SWAPFILE")
BUFFER_MB=10000
AVAIL_DISK_MB=$(df -BM "$BASE_DIR" | tail -1 | tr -s ' ' | cut -d' ' -f4 | sed 's/M//')
REQUIRED_BUFFER_MB=$(($BUFFER_MB + $SWAPSIZE))
[ "$AVAIL_DISK_MB" -lt "$REQUIRED_BUFFER_MB" ] && fail "Not enough free disk space"

touch "$SWAPFILE"
chattr +C "$SWAPFILE"
btrfs property set "$SWAPFILE" compression none && true # prevent script failure when using nodatacow
dd if=/dev/zero of="$SWAPFILE" bs=1M count="$SWAPSIZE"
chmod 600 "$SWAPFILE"
mkswap "$SWAPFILE"
