#!/bin/bash

set -e

is_automatic=$(get_value automatic-installation)
if [ x${is_swapfile} = x"false" ] || [ x${is_990_9a0} = x"false" ]; then
    return 0
fi

ram=$(cat < /proc/meminfo  | grep ^MemTotal: | awk '{print $2}')


### swapfile size
#swapfile=$(( ram * 12 / 1024 / 10 ))
#if [ $swapfile -gt 64000 ]; then
#    swapfile=64000
#fi
swapfile=8192

do_setup_swapfile() {
    dd if=/dev/zero of=/target/${swap_file} bs=1M count=${swapfile:-1}
    chmod 0600 /target/${swap_file}
    mkswap /target/${swap_file}
    swapon /target/${swap_file}

    offset=$(filefrag -v /target/${swap_file} | awk '{if($1=="0:"){print $4}}')
    root=$(findmnt -rno SOURCE /target)
    if egrep -qi 'kirin.?900' /proc/cpuinfo; then
      root="PARTUUID=$(lsblk -rno partuuid $(findmnt -rno SOURCE /target))"
      if [[ "${isluks_lvm}" == "true" ]] || [[ "${tpm}" == "true" ]]; then
	      root=$(findmnt -rno SOURCE /target)
      fi
    else
      root=$(findmnt -rno SOURCE /target)
    fi

    if [[ -f /target/etc/default/grub ]]; then
        sed -ri "s@quiet splash@resume=${root} systemd.kylin_force_hibernate=true resume_offset=${offset:0:-2} quiet splash@" /target/etc/default/grub
    fi
}

do_setup_swapfile



