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

get(){
	a=$(echo $(dbus get $1))
	a=$(echo $(dbus get $1))
	echo $a
}


count=$(get merlinclash_acl_edit_content1_count)
yamlname=$(get merlinclash_set_yamlsel_start)
sleep 1
if [ -n "$count" ];then
	i=0
	while [ "$i" -lt "$count" ]
	do
		txt=$(get merlinclash_acl_edit_content1_$i)
		#开始拼接文件值，然后进行base64解码，写回文件
		content=${content}${txt}
		let i=i+1
	done
	echo $content| base64_decode > /tmp/cusrule.txt
	if [ -f /tmp/cusrule.txt ]; then
		echo_date "中间文件已经创建" >> $LOG_FILE
		echo_date "生成新文件" >> $LOG_FILE
		cat /tmp/cusrule.txt | urldecode > /koolshare/merlinclash/rule_use/${yamlname}_rules.yaml 2>&1
	#	rm -rf /tmp/cusrule.txt
	fi
	#dbus remove jdqd_jd_script_content_custom
	customs=`dbus list merlinclash_acl_edit_content1_ | cut -d "=" -f 1`
	for custom in $customs
	do
		dbus remove $custom
	done
fi


rm -rf /tmp/upload/${yamlname}_rules.txt

if [ -f "/koolshare/merlinclash/rule_use/${yamlname}_rules.yaml" ]; then
	ln -sf /koolshare/merlinclash/rule_use/${yamlname}_rules.yaml /tmp/upload/${yamlname}_rules.txt
else
	ln -sf /koolshare/merlinclash/rule_bak/${yamlname}_rules.yaml /tmp/upload/${yamlname}_rules.txt
fi



http_response "$1"