#!/bin/sh

export KSROOT=/koolshare
source $KSROOT/scripts/base.sh
source /koolshare/scripts/clash_base.sh
eval $(dbus export merlinclash_)
alias echo_date='echo 【$(date +%Y年%m月%d日\ %X)】:'
#
LOG_FILE=/tmp/upload/merlinclash_log.txt
mkdir -p /koolshare/merlinclash/rule_custom

yamlname=${merlinclash_set_yamlsel_start}

yamlselchange=${merlinclash_set_yamlsel_startchange}
savefile(){
    rm -rf /koolshare/merlinclash/rule_custom/${yamlname}_custom_rule.yaml
    acl_nu=$(get_list merlinclash_acl_type 1 4)
	num=0
	if [ -n "$acl_nu" ]; then
		for acl in $acl_nu; do
			type=$(eval echo \$merlinclash_acl_type_$acl)
			content=$(eval echo \$merlinclash_acl_content_$acl)
			lianjie=$(eval echo \$merlinclash_acl_lianjie_$acl)
			#protocol=$(eval echo \$merlinclash_acl_protocol_$acl)
			
            echo $type,$content,$lianjie >> /koolshare/merlinclash/rule_custom/${yamlname}_custom_rule.yaml
	    done
	else
	    echo "none"
	fi
}
usefile(){
    if [ -f "/koolshare/merlinclash/rule_custom/${yamlname}_custom_rule.yaml" ]; then
        acl_nu=$(get_list merlinclash_acl_type 1 4)
        echo_date "当配置文件变化且有自定义规则时，先清除自定义规则的值" >> $LOG_FILE
        if [ $yamlselchange == "1" ]; then
            if [ -n "$acl_nu" ]; then
                for acl in $acl_nu; do
                    dbus remove merlinclash_acl_type_$acl
                    dbus remove merlinclash_acl_content_$acl
                    dbus remove merlinclash_acl_lianjie_$acl
                    #dbus remove merlinclash_acl_protocol_$acl
                done
            fi
            #当配置文件变化，且存在自定义规则文件时，从rule_custom中的文件字典读取各值重新赋值还原
            rulecusfile="/koolshare/merlinclash/rule_custom/${yamlname}_custom_rule.yaml"
            lines=$(cat $rulecusfile | wc -l)
            echo_date "存在自定义规则：$lines条" >> $LOG_FILE
            if [ $lines -gt 0 ]; then
                i=1
                while [ "$i" -le "$lines" ]
                do
                    echo_date "开始取值赋值处理" >> $LOG_FILE
                    line=$(sed -n ''$i'p' "$rulecusfile")
                    type=$(echo $line | awk -F "," '{print $1}')
                    content=$(echo $line | awk -F "," '{print $2}')
                    lianjie=$(echo $line | awk -F "," '{print $3}')
                    #protocol=$(echo $line | awk -F "," '{print $4}')
                    dbus set merlinclash_acl_type_$i=$type
                    dbus set merlinclash_acl_content_$i=$content
                    dbus set merlinclash_acl_lianjie_$i=$lianjie
                    #dbus set merlinclash_acl_protocol_$i=$protocol
                    let i++
                done    
            fi
        fi
    else
        echo_date "当配置文件变化且无自定义规则文件时，先清除自定义规则的值" >> $LOG_FILE
        acl_nu=$(get_list merlinclash_acl_type 1 4)
        if [ -n "$acl_nu" ]; then
			for acl in $acl_nu; do
                dbus remove merlinclash_acl_type_$acl
                dbus remove merlinclash_acl_content_$acl
                dbus remove merlinclash_acl_lianjie_$acl
                #dbus remove merlinclash_acl_protocol_$acl
            done
        fi
    fi
}
case $2 in
save)
    savefile
    http_response $1
    ;;
    
del)
    savefile
    http_response $1
    ;;

use)
    usefile
    ;;
esac



