#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Enables or disables zram

# License
# =======
#
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-zram
export TEXTDOMAINDIR="/usr/share/locale"

DAEMON_FILENAME=/etc/systemd/system/zram.service

function zram_daemon {
  echo '[Unit]' > $DAEMON_FILENAME
  echo 'Description=Zeronet Server' >> $DAEMON_FILENAME
  echo 'After=syslog.target' >> $DAEMON_FILENAME
  echo 'After=network.target' >> $DAEMON_FILENAME
  echo '[Service]' >> $DAEMON_FILENAME
  echo 'Type=simple' >> $DAEMON_FILENAME
  echo 'User=zram' >> $DAEMON_FILENAME
  echo 'Group=zram' >> $DAEMON_FILENAME
  echo 'WorkingDirectory=' >> $DAEMON_FILENAME
  echo "ExecStart=${PROJECT_NAME}-zram on" >> $DAEMON_FILENAME
  echo '' >> $DAEMON_FILENAME
  echo '[Install]' >> $DAEMON_FILENAME
  echo 'WantedBy=multi-user.target' >> $DAEMON_FILENAME

}

function zram_on {
    if [ ! -f $DAEMON_FILENAME ]; then
        if ! grep -q "options zram num_devices=1" /etc/modprobe.d/zram.conf; then
            echo 'options zram num_devices=1' >> /etc/modprobe.d/zram.conf
        fi

        # get the number of CPUs
        num_cpus=$(grep -c processor /proc/cpuinfo)

        # if something goes wrong, assume we have 1
        [ "$num_cpus" != 0 ] || num_cpus=1

        # set decremented number of CPUs
        decr_num_cpus=$((num_cpus - 1))

        # get the amount of memory in the machine
        mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching "[[:digit:]]+")
        mem_total=$((mem_total_kb * 1024))

        # load dependency modules
        modprobe zram num_devices=$num_cpus

        # initialize the devices
        for i in $(seq 0 $decr_num_cpus); do
            echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
        done

        # Creating swap filesystems
        for i in $(seq 0 $decr_num_cpus); do
            mkswap /dev/zram$i
        done

        # Switch the swaps on
        for i in $(seq 0 $decr_num_cpus); do
            swapon -p 100 /dev/zram$i
        done

        zram_daemon
    fi
}

function zram_off {
    if [ -f $DAEMON_FILENAME ]; then
        # get the number of CPUs
        num_cpus=$(grep -c processor /proc/cpuinfo)

        # set decremented number of CPUs
        decr_num_cpus=$((num_cpus - 1))

        # Switching off swap
        for i in $(seq 0 $decr_num_cpus); do
            if [ "$(grep /dev/zram$i /proc/swaps)" != "" ]; then
                swapoff /dev/zram$i
                sleep 1
            fi
        done

        sleep 1
        rmmod zram

        rm $DAEMON_FILENAME
    fi
}

function show_help {
    echo ''
    echo $"${PROJECT_NAME}-zram [on|off]"
    echo ''
    exit 0
}

if [ ! $1 ]; then
    show_help
else
    if [[ "$1" == "on" || "$1" == "enable" || "$1" == "yes" ]]; then
        zram_on
    else
        zram_off
    fi
fi

exit 0
