#!/bin/bash
source $HOME/.gamerc

TARGET="/etc/security/limits.conf"
ACTION=$1

check () {
    CHECK=$(sudo cat ${TARGET} | egrep "$USER soft nofile 1048576|$USER hard nofile 1048576|$USER soft core unlimited|$USER core unlimited|$USER hard memlock ulimited|$USER soft memlock ulimited" | wc -l)
    echo ${CHECK}
}

get () {
    sudo cat ${TARGET} | egrep "$USER"
}

set () {
    sudo sed -i "/# End of file/i $USER soft nofile 1048576\n$USER hard nofile 1048576\n$USER soft core unlimited\n$USER core unlimited\n$USER hard memlock ulimited\n$USER soft memlock ulimited\n" ${TARGET}
    echo "Please restart $HOST_NAME!"
}

del () {
    sudo sed -i "/$USER/d" ${TARGET}
}

process () {
    ULIMIT_CHECK=$($0 check)
    if [ "${ULIMIT_CHECK}" -ge "6" ]; then
        echo 0
    else
        $0 del
        $0 set
        echo 1
    fi
}

${ACTION}
