#!/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
}

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
}

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

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

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

    for port in $(seq $1 $2); do
        $sudoprefix iptables -D INPUT -p tcp --dport $port -j ACCEPT 1>/dev/null 2>/dev/null
        $sudoprefix iptables -D OUTPUT -p tcp --sport $port -m state --state ESTABLISHED -j ACCEPT 1>/dev/null 2>/dev/null
        $sudoprefix iptables -A INPUT -p tcp --dport $port -j ACCEPT 1>/dev/null 2>/dev/null
        $sudoprefix iptables -A OUTPUT -p tcp --sport $port -m state --state ESTABLISHED -j ACCEPT 1>/dev/null 2>/dev/null
    done
}

