#!/bin/sh
#author:Wos
#a script to simplify clash configuration file in public
killall -e clash 2>/dev/null
WORK_DIR=$(dirname $0)
SCRIPT_NAME=$(basename $0)
CFG_PATH=${WORK_DIR}/clash.yml
CFG_TEMP=/tmp/clash.yml
CFG_TEMP_2=/tmp/clash2.yml
CFG_TEMP_3=/tmp/clash3.yml
CFG_P2C_PATH=${WORK_DIR}/p2c.yml
NODE_NAME=/tmp/nodename
CLASH_PATH=/usr/bin/clash

function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA4WOwQ3AIAwD/0zhX/uoxEJIZpAMXzsBiU9VRyQHOAEgRfyKaM5kIFzMyGD1L/aV
LDJ3pFkFQ2nq+BK6P4onat+0vWuyerp4mxQnr8nD32BkUXqYpmIs9qLNXwr6qVPtBYezLN0OAQAA"|gunzip
    echo -e "\033[0m"

}
function usage()
{
    echo "${SCRIPT_NAME} [clash_config_link]"
    echo "${SCRIPT_NAME} [local_clash_config_file_path]"
    echo "${SCRIPT_NAME} -f [proxy_list_file] #convert proxy list to clash configuration file and connect"
    echo "${SCRIPT_NAME} -f #direct connect previous p2c configuration file"
    exit -1
}
####################################################################
#convert normal proxy list to clash configuration file
#proxy list foramt,you can combine http and socks5 in one file,one server one line
#recommend socks5 type 
# http ip port
# socks5 ip port
function p2c()
{
cat ${CFG_TEMP}|sed '/^$/d'|awk '{print "      - "$1"_"NR"_"}' > ${NODE_NAME}
cat ${CFG_TEMP}|sed '/^$/d'|awk '{print "  - {name: "$1"_"NR"_, type: "$1", skip-cert-verify: false, server: "$2", port: "$3"}"}' > ${CFG_TEMP_2}
cat <<BOF  > ${CFG_P2C_PATH}
mixed-port: 7891
allow-lan: false
mode: Rule
log-level: info
dns:
  enable: true 
  ipv6: false
  nameserver:
    - 127.0.0.1 
  fallback: 
    - 127.0.0.1 
proxies:
BOF
cat ${CFG_TEMP_2} >> ${CFG_P2C_PATH}
cat <<BOF  >> ${CFG_P2C_PATH}
proxy-groups:
  - name: auto
    type: url-test 
    url: 'https://www.gstatic.com/generate_204'
    interval: 300
    proxies:
BOF
cat ${NODE_NAME} >> ${CFG_P2C_PATH}
cat <<BOF  >> ${CFG_P2C_PATH}
rules:
 - MATCH,auto
BOF
}
###################################################################
#speicial binary configuration file cleaner

function clean_bin_yml()
{
cat ${CFG_TEMP}|grep -n "\- name:" > /tmp/1.txt
cat /tmp/1.txt|grep -n "自动选择"|awk -F:  '{print "sed -n",$1","$1+1"p /tmp/1.txt"}'|/bin/sh|awk -F: '{print $1}'|xargs|awk '{print "sed -n",$1","$2-1"p /tmp/clash.yml"}'|/bin/sh > ${CFG_TEMP_3}
ST=$(($(sed -n "/proxies:/=" ${CFG_TEMP}|head -1)+1))
ED=$(($(sed -n "/proxy-groups:/=" ${CFG_TEMP}|head -1)-1))
sed -n ${ST},${ED}p ${CFG_TEMP} > ${CFG_TEMP_2}


cat <<BOF  > ${CFG_PATH}
mixed-port: 7891
allow-lan: false
mode: Rule
log-level: info
dns:
  enable: true 
  ipv6: false
  nameserver:
    - 127.0.0.1 
  fallback: 
    - 127.0.0.1 
proxies:
BOF
cat ${CFG_TEMP_2} >> ${CFG_PATH}
cat <<BOF  >> ${CFG_PATH}
proxy-groups:
BOF
cat ${CFG_TEMP_3} >> ${CFG_PATH}
cat <<BOF  >> ${CFG_PATH}
rules:
  - MATCH,auto
BOF
sed -i 's/:.*自动选择"/: auto/g' ${CFG_PATH}
sed -i 's/:.*自动选择/: auto/g' ${CFG_PATH}
sed -i 's/-.*自动选择"/- auto/g' ${CFG_PATH}
sed -i 's/-.*自动选择/- auto/g' ${CFG_PATH}
cat ${CFG_PATH} |grep -n -E "中国|英国|俄罗斯"|grep -v "name"|awk -F: '{print "-e "$1"d"}'|xargs|awk '{print "sed -i -e "$0,"'${CFG_PATH}'"}'|/bin/sh
}
###################################################################
clear
logo
if [ $# == 0 ]; then
    usage
elif [ $# == 1 ]; then
    if [ ${1} == '-f' ] && [ -f "${CFG_P2C_PATH}" ]; then
        echo "connect p2c config file..."
        ${CLASH_PATH} -f ${CFG_P2C_PATH} 
        exit -1
    elif [[ "${1}" =~ ^(http(s)?:\/\/).* ]]; then
        echo "connect clash config link..."
        rm /tmp/clash.yml 2>/dev/null
        aria2c --connect-timeout=3 ${1} --dir=/tmp --out=clash.yml -q
        clean_bin_yml && ${CLASH_PATH} -f ${CFG_PATH} 
        exit -1
    elif [ -n "${1}" ] && [ -f "${1}" ]; then
        echo "connect local clash file..."
        cp ${1}  ${CFG_TEMP} && clean_bin_yml && ${CLASH_PATH} -f ${CFG_PATH} 
        exit -1
    else
        usage
    fi
elif [ $# == 2 ] && [ ${1} == '-f' ]; then
    if [ -n "${2}" ] && [ -f "${2}" ]; then
        echo "convert proxy list to clash config..."
        CFG_TEMP="${2}"
        p2c
        echo "connect p2c config file..."
        ${CLASH_PATH} -f ${CFG_P2C_PATH} 
        exit -1
    else
        usage
    fi
else
    usage
fi