#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;
module_dir="/root/amh/modules/AMProxy-2.0/";

#info
function amh_module_info()
{
    echo 'AMH-ModuleName: AMProxy-2.0';
    echo 'AMH-ModuleDescription: AMProxy-2.0 反向代理模块修改版(用于API兼容HYProxy)';
    echo 'AMH-ModuleButton: 安装/卸载';
    echo 'AMH-ModuleDate: 2020-01-17';
    echo 'AMH-ModuleAdmin: ./index.php?c=hyproxy';
    echo 'AMH-ModuleWebSite: http://www.huayizhiyun.com';
    echo 'AMH-ModuleScriptBy: HITSword.';
}

#install
function amh_module_install()
{
    if amh_module_status ; then
        exit;
    else
        if [ ! -f "/root/amh/modules/HYProxy-1.0/InstallComplete" ]; then
            echo '[Error] HYProxy 未安装.';
            exit;
        fi;
        cd /root/amh/modules/AMProxy-2.0;
        touch ./InstallComplete;
        amh_module_status;
    fi;
}

#uninstall
function amh_module_uninstall()
{
    if amh_module_status ; then
        rm -f /root/amh/modules/AMProxy-2.0/InstallComplete;
        echo '[OK] AMProxy-2.0 Uninstall successful.';
    else
        exit;
    fi;
}

#admin
function amh_module_admin()
{
    if amh_module_status ; then
        param_list=${1//,/ };
        set -- $param_list;
        action=$1;
        domain=$2;
        
        if [ "$action" == '' ]; then
            echo "[Notice] AMProxy management, please select: (1~4)"
            select action in 'list' 'add' 'start' 'stop' 'edit' 'del' 'start-cache' 'stop-cache' 'cache' 'cache-index' 'cache-delete'; do
                break;
            done;
        fi;
				
        case $action in
            list)
                php ${module_dir}cli.php --action='list';
            ;;
            start)
                [ -f "/usr/local/nginx/conf/hyproxy/$domain.conf" ] && echo "[OK] $domain is ok" && exit;
                RES=`php ${module_dir}cli.php --action='edit' --server_name=${domain} --field_name='status' --value='start'`;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    kill -HUP `cat /usr/local/nginx/logs/nginx.pid` && echo "[OK] successfully start $domain " && exit;
                fi;
                echo "[Error] $domain not exist!" && exit 1;
            ;;
            stop)
                [ ! -f "/usr/local/nginx/conf/hyproxy/$domain.conf" ] && echo "[OK] $domain already stopped" && exit;
                RES=`php ${module_dir}cli.php --action='edit' --server_name=${domain} --field_name='status' --value='stop'`;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    kill -HUP `cat /usr/local/nginx/logs/nginx.pid` && echo "[OK] successfully stop $domain" && exit;
                fi;
                echo "[Error] $domain not exist!" && exit 1;
            ;;
            add)
                proxy_pass=$3;
                RES=`php ${module_dir}cli.php --action='add' --server_name=${domain} --proxy_pass="${proxy_pass}"`;
                echo $RES;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    amh nginx reload;
                fi;
            ;;
            edit)
                #参数名
                ParamName=$3
                #参数值
                i=0;
                for line in $*; do
                    i=$[$i+1];
                    [ "$i" -gt 3 ] && ParamVal="$ParamVal $line";
                done;
                RES=`php ${module_dir}cli.php --action='edit' --server_name=${domain} --field_name=${ParamName} --value="${ParamVal}"`;
                echo $RES;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    amh nginx reload;
                fi;
            ;;
            del)
                php ${module_dir}cli.php --action='del' --server_name=${domain}
                amh nginx reload;
            ;;
            start-cache)
                RES=`php ${module_dir}cli.php --action='edit' --server_name=${domain} --field_name='proxy_cache' --value='1'`;
                echo $RES;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    kill -HUP `cat /usr/local/nginx/logs/nginx.pid` && echo "[OK] successfully start-cache $domain " && exit;
                fi;
            ;;
            stop-cache)
                RES=`php ${module_dir}cli.php --action='edit' --server_name=${domain} --field_name='proxy_cache' --value='0'`;
                echo $RES;
                if ! echo "$RES" | grep 'Error' > /dev/null; then
                    kill -HUP `cat /usr/local/nginx/logs/nginx.pid` && echo "[OK] successfully stop-cache $domain " && exit;
                fi;
            ;;
            cache)
                echo "[Notice] 暂不支持缓存配置";
            ;;
            cache-index)
                echo "[Notice] 暂不支持缓存索引";
            ;;
            cache-delete)
                echo "[Notice] 暂不支持缓存删除";
            ;;
            *)
                echo "[Notice] 无效操作";
            ;;
        esac
    else
        exit;
    fi;
}


#status
function amh_module_status()
{
    if [ -f "/root/amh/modules/AMProxy-2.0/InstallComplete" ]; then
        echo '[OK] AMProxy-2.0 is already installed.';
        return 0;
    else
        echo '[Notice] AMProxy-2.0 is not installed.';
        return 1;
    fi;
}