#!/bin/bash

# Get the total or free amount of RAM+swap on the system
#
# [packagingv1]
#
# usage: ynh_get_ram [--free|--total] [--ignore_swap|--only_swap]
# | arg: -f, --free         - Count free RAM+swap
# | arg: -t, --total        - Count total RAM+swap
# | arg: -s, --ignore_swap  - Ignore swap, consider only real RAM
# | arg: -o, --only_swap    - Ignore real RAM, consider only swap
# | ret: the amount of free ram, in MB (MegaBytes)
#
# Requires YunoHost version 3.8.1 or higher.
ynh_get_ram() {
    # Declare an array to define the options of this helper.
    local legacy_args=ftso
    local -A args_array=([f]=free [t]=total [s]=ignore_swap [o]=only_swap)
    local free
    local total
    local ignore_swap
    local only_swap
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"
    ignore_swap=${ignore_swap:-0}
    only_swap=${only_swap:-0}
    free=${free:-0}
    total=${total:-0}

    if [ $free -eq $total ]; then
        ynh_print_warn --message="You have to choose --free or --total when using ynh_get_ram"
        ram=0
    # Use the total amount of ram
    elif [ $free -eq 1 ]; then
        local free_ram=$(LC_ALL=C vmstat --stats --unit M | grep "free memory" | awk '{print $1}')
        local free_swap=$(LC_ALL=C vmstat --stats --unit M | grep "free swap" | awk '{print $1}')
        local free_ram_swap=$((free_ram + free_swap))

        # Use the total amount of free ram
        local ram=$free_ram_swap
        if [ $ignore_swap -eq 1 ]; then
            # Use only the amount of free ram
            ram=$free_ram
        elif [ $only_swap -eq 1 ]; then
            # Use only the amount of free swap
            ram=$free_swap
        fi
    elif [ $total -eq 1 ]; then
        local total_ram=$(LC_ALL=C vmstat --stats --unit M | grep "total memory" | awk '{print $1}')
        local total_swap=$(LC_ALL=C vmstat --stats --unit M | grep "total swap" | awk '{print $1}')
        local total_ram_swap=$((total_ram + total_swap))

        local ram=$total_ram_swap
        if [ $ignore_swap -eq 1 ]; then
            # Use only the amount of free ram
            ram=$total_ram
        elif [ $only_swap -eq 1 ]; then
            # Use only the amount of free swap
            ram=$total_swap
        fi
    fi

    echo $ram
}

# Return 0 or 1 depending if the system has a given amount of RAM+swap free or total
#
# [packagingv1]
#
# usage: ynh_require_ram --required=RAM [--free|--total] [--ignore_swap|--only_swap]
# | arg: -r, --required=    - The amount to require, in MB
# | arg: -f, --free         - Count free RAM+swap
# | arg: -t, --total        - Count total RAM+swap
# | arg: -s, --ignore_swap  - Ignore swap, consider only real RAM
# | arg: -o, --only_swap    - Ignore real RAM, consider only swap
# | ret: 1 if the ram is under the requirement, 0 otherwise.
#
# Requires YunoHost version 3.8.1 or higher.
ynh_require_ram() {
    # Declare an array to define the options of this helper.
    local legacy_args=rftso
    local -A args_array=([r]=required= [f]=free [t]=total [s]=ignore_swap [o]=only_swap)
    local required
    local free
    local total
    local ignore_swap
    local only_swap
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"
    # Dunno if that's the right way to do, but that's some black magic to be able to
    # forward the bool args to ynh_get_ram easily?
    # If the variable $free is not empty, set it to '--free'
    free=${free:+--free}
    total=${total:+--total}
    ignore_swap=${ignore_swap:+--ignore_swap}
    only_swap=${only_swap:+--only_swap}

    local ram=$(ynh_get_ram $free $total $ignore_swap $only_swap)

    if [ $ram -lt $required ]; then
        return 1
    else
        return 0
    fi
}


# Ask the creation of a swap file for the application.
# The helper might or might not create the swap file, considering the machine's configuration.
#
# [packagingv1]
#
# usage: ynh_add_swap --size=SWAP in Mib
# | arg: -s, --size= - Amount of SWAP to add in Mib.
ynh_add_swap () {
    # Declare an array to define the options of this helper.
    local -A args_array=( [s]=size= )
    local size
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    # Could be moved to an argument
    swapfile_dir="$(realpath /)"
    swapfile_path="$(realpath "$swapfile_dir/swap_file")"

    # Early warning exit: Can't swap inside LXD
    if [[ "$(systemd-detect-virt)" == "lxc" ]]; then
        ynh_print_warn --message="You are inside a LXC container, swap will not be added."
        ynh_print_warn --message="That can cause troubles for $app. Please make sure you have more than $((size/1024))G of RAM/swap available."
        return
    fi

    # Early warning exit: Preserve SD cards and do not swap on them
    if ynh_is_on_sd_card --dir="$swapfile_dir" && [[ "${SD_CARD_CAN_SWAP:-0}" == "0" ]]; then
        ynh_print_warn --message="Swap files on an SD card is not recommended, swap will not be added."
        ynh_print_warn --message="That can cause troubles for $app. Please make sure you have more than $((size/1024))G of RAM/swap available."
        ynh_print_warn --message="If you still want activate the swap, you can relaunch the command preceded by 'SD_CARD_CAN_SWAP=1'"
        return
    fi

    # Early warning exit: swapfile already exists
    if [ -f "$swapfile_path" ]; then
        ynh_print_warn --message="Swap file $swapfile_path already exists!"
        return
    fi

    local swap_max_size_kb=$(( size * 1024 ))

    local free_space_kb
    free_space_kb=$(df --block-size=K --output=avail "$swapfile_dir" | sed 1d | sed -e 's/K$//')
    # Because we don't want to fill the disk with a swap file, divide by 2 the available space.
    local usable_space_kb=$(( free_space_kb / 2 ))

    # Compare the available space with the size of the swap.
    # And set a acceptable size from the request
    size_space_ratio=$(( swap_max_size_kb / usable_space_kb + 1 ))
    if (( size_space_ratio > 4)); then
        ynh_print_warn --message="Not enough space for a swap file at $swapfile_path."
        return
    fi
    swap_size_kb=$(( swap_max_size_kb / size_space_ratio ))

    # Configure swappiness
    if [ ! -e /etc/sysctl.d/999-ynhswap.conf ]
    then
        echo "vm.swappiness=10" > /etc/sysctl.d/999-ynhswap.conf
        sysctl --quiet --system
    fi

    # If there's enough space for a swap, and no existing swap here
    if [ $swap_size -ne 0 ] && [ ! -e /swap_file ]
    then
        # Create file
        truncate -s 0 /swap_file

        # set the No_COW attribute on the swapfile with chattr
        # let's not fail here, as some filesystems like ext4 do not support COW
        chattr +C /swap_file || true

        # Preallocate space for the swap file, fallocate may sometime not be used, use dd instead in this case
        if ! fallocate -l ${swap_size}K /swap_file
        then
            dd if=/dev/zero of=/swap_file bs=1024 count=${swap_size}
        fi
        chmod 0600 /swap_file
        # Create the swap
        mkswap /swap_file
        # And activate it
        swapon /swap_file
        # Then add an entry in fstab to load this swap at each boot.
        echo -e "/swap_file swap swap defaults 0 0 #Swap added by YunoHost config panel" >> /etc/fstab
    fi
}

# Delete the swap file created for the app by ynh_add_swap.
#
# [packagingv1]
#
ynh_del_swap () {
    # Could be moved to an argument
    swapfile_dir="$(realpath /)"
    swapfile_path="$(realpath "$swapfile_dir/swap_file")"

    if [ ! -f "$swapfile_path" ]; then
        return
    fi

    # Clean the fstab...
    sed -i "/#Swap added by $app/d" /etc/fstab
    # Desactive the swap file...
    swapoff "$swapfile_path"
    # And remove it.
    rm "$swapfile_path"
}

# Check if the device of the provided directory is an SD card
#
# [internal]
#
# usage: ynh_is_on_sd_card --dir=/
# | arg: -d, --dir= - Directory to check
ynh_is_on_sd_card () {
    # Declare an array to define the options of this helper.
    local -A args_array=( [d]=dir= )
    local dir
    # Manage arguments with getopts
    ynh_handle_getopts_args "$@"

    device_dev=$(findmnt --nofsroot --uniq --output source --noheadings --first-only --target "$dir")
    device_pkname=$(lsblk --output PKNAME --noheadings "$device_dev")
    device_rotational=$(lsblk --output ROTA --noheadings "$device_dev")

    if [[ "$device_pkname" == *"mmc"* ]] && [[ "$device_rotational" == "0" ]]; then
        return 0
    else
        return 1
    fi
}