#!/bin/sh

init_env()
{
    sudoprefix=
    IS_ANDROID=false
    TMP_DIR=/tmp
    if id -u 2>/dev/null 1>/dev/null; then
        if ! id | grep "uid=0(" 2>/dev/null 1>/dev/null; then
            if sudo -V >/dev/null 2>&1; then
                sudoprefix=sudo
            else
                echo "error: You need to run the script with root privileges"
                exit 1
            fi
        fi
    fi

    if uname -s 2>/dev/null 1>/dev/null; then
        TOS="`uname -s`"
        ARCH="`uname -m`"
    else
        if echo $OSTYPE | grep darwin  >/dev/null; then
            TOS="Darwin"
            ARCH="x86"
            if echo $MACHTYPE | grep x86_64 >/dev/null; then
                ARCH="x86_64"
            elif echo $CPUTYPE | grep x86_64 >/dev/null; then
                ARCH="x86_64"
            fi
        elif cat /proc/version | grep Linux 2>/dev/null 1>/dev/null; then
            TOS="Linux"
            IS_ANDROID=true
            TMP_DIR=/cache
            if cat /proc/cpuinfo | grep aarch64 2>/dev/null 1>/dev/null; then
               ARCH="aarch64"
            elif cat /proc/cpuinfo | grep arm 2>/dev/null 1>/dev/null; then
               ARCH="armv4tl"
            elif cat /proc/version | grep x86_64 2>/dev/null 1>/dev/null; then
               ARCH="x86_64"
            elif cat /proc/version | grep x86 2>/dev/null 1>/dev/null; then
               ARCH="x86"
            else
                cat /proc/cpuinfo
                echo "error: unsupported Android arch, Please tell us to fix"
            fi
        fi
    fi
}

set_sys_env()
{
    if [ $TOS = "Darwin" ]; then
        $sudoprefix pmset disksleep 0 2>/dev/null
        $sudoprefix pmset displaysleep 0 2>/dev/null
        $sudoprefix pmset sleep 0 2>/dev/null

        $sudoprefix chmod -R 755 MacPmem.kext/
        $sudoprefix chown -R root:wheel MacPmem.kext/
        $sudoprefix kextutil -t MacPmem.kext/ 2>/dev/null 1>/dev/null

    elif [ $TOS = "Linux" ]; then
        [ -e pmem/pmem.ko -a ! -e /dev/pmem ] && $sudoprefix insmod pmem/pmem.ko
    fi
}

os_pidof( )
{
    if [ x"$TOS" = x"Darwin" -o x"$TOS" = x"FreeBSD" ]; then
        ps axc|awk "{if (\$5==\"$1\") print \$1}";
    elif [ x"$IS_ANDROID" = x"true" ]; then
       # maybe error 
       ps | grep "/$1\$"
    else
        pidof $1
    fi
}

set_firewall_port()
{
    if [ $1 -eq 0 ]; then
        return;
    fi

    if [ x"$TOS" = x"Darwin" -o x"$TOS" = x"FreeBSD" ]; then
        $sudoprefix ipfw add $1 allow tcp from any to any dst-port $1 2>/dev/null
        return
    fi

    $sudoprefix firewall-cmd --zone=public --add-port=$1/tcp --permanent 1>/dev/null 2>/dev/null
    $sudoprefix firewall-cmd --reload 1>/dev/null 2>/dev/null

    $sudoprefix iptables -D INPUT -p tcp --dport $1 -j ACCEPT 1>/dev/null 2>/dev/null
    $sudoprefix iptables -D OUTPUT -p tcp --sport $1 -m state --state ESTABLISHED -j ACCEPT 1>/dev/null 2>/dev/null
    $sudoprefix iptables -A INPUT -p tcp --dport $1 -j ACCEPT 1>/dev/null 2>/dev/null
    $sudoprefix iptables -A OUTPUT -p tcp --sport $1 -m state --state ESTABLISHED -j ACCEPT 1>/dev/null 2>/dev/null
}

