#!/bin/bash

neko_dir="/etc/neko"
neko_tmp_dir="$neko_dir/tmp"
neko_core="$neko_dir/core"
neko_bin="/usr/bin/mihomo"
neko_www="/www/nekobox"
neko_config=`cat $neko_www/lib/selected_config.txt`

php_bin="/usr/bin/php"
php_address="2.0.9.0"
php_port="8080"

tun_bin="$neko_dir/core/tun"
reload_bin="$neko_dir/core/reload"
firewall="/etc/init.d/firewall"

neko_log="$neko_tmp_dir/neko_log.txt"
neko_pid_path="$neko_tmp_dir/neko_pid.txt"
neko_status=`uci -q get neko.cfg.enabled`
php_pid_path="$neko_tmp_dir/php_pid.txt"
log="$neko_dir/tmp/log.txt"

neko_ver(){
    neko_version="2.0.9"
}

logs() {
    echo "[ `date +%T` ] $1"
    if [ -z $2 ]; then
        echo "[ `date +%T` ] $1" >> $log
    elif [ $2 -eq 2 ]; then
        echo "[ `date +%T` ] $1" > $log
    elif [ $2 -eq 3 ]; then
        echo "[ `date +%T` ] $1" >> $log
        exit 1
    fi
}

check_depedency() {
    opw_version=`cat /etc/os-release | grep VERSION_ID | cut -d\" -f 2`
    logs "Check Depedencies : $opw_version"
    if [[ $opw_version == 18.* ]]; then
        req=(curl php7 php7-cgi iptables kmod-tun)
        list=`opkg list-installed | grep -E "curl|php7|iptables|kmod-tun" | awk '{ print $1 }'`
        for req_pkg in ${req[@]}; do
            for lst_pkg in $list; do
                stat=0
                if [ ${req_pkg} == $lst_pkg ] ; then
                    logs "- $req_pkg Installed" 1
                    stat=0
                    break;
                else
                    stat=1
                fi
            done
            if [ $stat = 1 ] ; then
                logs "- Package $req_pkg " 1
                logs "- Installing $req_pkg "
                opkg install $req_pkg
            fi
        done
    elif [[ $opw_version == 21.* ]]; then
        req=(curl php7 php7-cgi iptables kmod-tun)
        list=`opkg list-installed | grep -E "curl|php7|iptables|kmod-tun" | awk '{ print $1 }'`
        for req_pkg in ${req[@]}; do
            for lst_pkg in $list; do
                stat=0
                if [ ${req_pkg} == $lst_pkg ] ; then
                    logs "- $req_pkg Installed" 1
                    stat=0
                    break;
                else
                    stat=1
                fi
            done
            if [ $stat = 1 ] ; then
                logs "- Package $req_pkg " 1
                logs "- Installing $req_pkg "
                opkg install $req_pkg
            fi
        done
    elif [[ $opw_version == 22.* ]]; then
        req=(curl php8 php8-cgi firewall4 xtables-nft kmod-tun)
        list=`opkg list-installed | grep -E "curl|php8|firewall4|kmod-tun|xtables-nft" | awk '{ print $1 }'`
        for req_pkg in ${req[@]}; do
            for lst_pkg in $list; do
                stat=0
                if [ ${req_pkg} == $lst_pkg ] ; then
                    logs "- $req_pkg Installed" 1
                    stat=0
                    break;
                else
                    stat=1
                fi
            done
            if [ $stat = 1 ] ; then
                logs "- Package $req_pkg " 1
                logs "- Installing $req_pkg "
                opkg install $req_pkg
            fi
        done
    elif [[ $opw_version == 23.* ]]; then
        req=(curl php8 php8-cgi firewall4 xtables-nft kmod-tun)
        list=`opkg list-installed | grep -E "curl|php8|firewall4|kmod-tun|xtables-nft" | awk '{ print $1 }'`
        for req_pkg in ${req[@]}; do
            for lst_pkg in $list; do
                stat=0
                if [ ${req_pkg} == $lst_pkg ] ; then
                    logs "- $req_pkg Installed" 1
                    stat=0
                    break;
                else
                    stat=1
                fi
            done
            if [ $stat = 1 ] ; then
                logs "- Package $req_pkg " 1
                logs "- Installing $req_pkg "
                opkg install $req_pkg
            fi
        done
    else
        echo "Zonk."
    fi
}

neko_start(){
    neko_ver
    logs "Starting Nekobox v.$neko_version" 2
    if [ -f "/tmp/neko/neko.bak" ]; then
        rm -r /tmp/neko/neko.bak
        rm -r $neko_tmp_dir/first_install.txt
    elif [ -f "$neko_tmp_dir/first_install.txt" ]; then
        logs "- First Install Detected."
        rm -r $neko_tmp_dir/first_install.txt
        logs "Mihomo Not Running" 3
    else
        logs "Checking Package..."
        check_depedency
        files_check
        logs "Checking Package Done."
    fi
    if [ -f $neko_pid_path ] ; then
        logs "Mihomo is Running. Killing Mihomo PID : `cat $neko_pid_path`"
        $tun_bin -k >> $log
        kill `cat $neko_pid_path`
        uci set neko.cfg.enabled='0'
        uci commit neko
        rm $neko_pid_path
        logs "Retry Starting Mihomo..."
    fi
    if [ -f $neko_bin ] ; then
        rpid=`pgrep "mihomo|reload"`
        if [[ -n $rpid ]] ; then
            kill $rpid 
        fi
        rm $neko_log
        logs "Restarting Firewall"
        $firewall restart
        $neko_bin -f $neko_config -d $neko_dir >> $neko_log &

        sleep 1
        neko_pid=`pgrep mihomo`
        if [[ -z $neko_pid ]]; then
            for file in "$neko_core"/*; do
                if [ ! -x "$file" ]; then
                    logs "- `basename $file` Binary Permission is not executable, Changing..."
                    chmod +x "$file"
                fi
            done
            logs "Retry Starting Mihomo"
            $neko_bin -f $neko_config -d $neko_dir >> $neko_log &
            sleep 1
            neko_pid=`pgrep mihomo`
        fi
        logs "Mihomo Started. PID : $neko_pid"
        logs "Configs : $neko_config"
        echo $neko_pid > $neko_pid_path
        $tun_bin -s >> $log
        uci set neko.cfg.enabled='1'
        uci commit neko
        $reload_bin "$neko_version" > /dev/null 2>&1 &
    else
        logs "Core not Detected!!!"
    fi
    #logs "Done"
}

php_start(){
    logs "Checking PHP Server Status..."
    if [ -f $php_pid_path ] ; then
        logs "- PHP Server is Running..."
        logs "- Destroy PHP Server. PID : `cat $php_pid_path`"
        kill `cat $php_pid_path`
        rm $php_pid_path
    else  
        logs "- PHP Server not Running..."
    fi
    nohup $php_bin -S $php_address:$php_port -t ${neko_www} > /dev/null 2>&1 &
    php_pid=`pgrep php`
    logs "- PHP Server Started. PID:$php_pid"
    logs "- PHP Running at $php_address:$php_port"
    echo $php_pid > $php_pid_path
    logs "Done"
}

neko_stop(){
    logs "Disable Mihomo" 2
    $tun_bin -k >> $log
    if [ -f $neko_pid_path ] ; then
        logs "Killing Mihomo PID : `cat $neko_pid_path`"
        logs "Restarting Firewall"
        $firewall restart
        kill `cat $neko_pid_path`
        rm $neko_pid_path
        rpid=`pgrep "mihomo|reload"`
        if [[ -n $rpid ]] ; then
            kill $rpid 
        fi
        logs "Mihomo has Disabled"
    else
        logs "Mihomo is not Running"
    fi
    uci set neko.cfg.enabled='0'
    uci commit neko
}

neko_restart(){
    logs "Restarting Mihomo" 2
    neko_start
    logs "Restarting Mihomo - Done"
}

php_stop(){
    if [ -f $php_pid_path ] ; then
        logs "- Killing PHP PID : `cat $php_pid_path`"
        kill `cat $php_pid_path`
        rm $php_pid_path
        logs "- Done..."
    else
        logs "- PHP Server Not Running"
    fi
}

cleanup(){
    logs "Cleanup Temporary..." 2
    neko_stop
    php_stop
    logs "Cleanup Done..."
}

while getopts ":skrpcvh" signal ; do
    case $signal in
        s)
            neko_start
            ;;
        k)
            neko_stop
            ;;
        r)
            neko_restart
            ;;
        p)
            php_start
            ;;
        c)
            cleanup
            ;;
        v)
            neko_ver
            neko_checknewver
            echo "v.$neko_version"
            ;;
        h)
            echo "Neko"
            echo "      -s : Start Mihomo Proxy"
            echo "      -p : Start PHP Server"
            echo "      -k : Kill/Stop Mihomo Proxy"
            echo "      -r : Restart Mihomo Proxy"
            echo "      -c : cleanup (kill mihomo and php server)"
            echo "      -v : Version"
            echo "      -h : help (this text)"
            echo "Please Use ROOT User"
            ;;
    esac
done
