#!/bin/bash

function downfilez(){
mkdir -p /etc/scripts
link='https://darix.ooguy.com/drowkid01/dpbt/raw/branch/main/scripts'
scpconf='/etc/scripts/scripts.json'
wget -O $scpconf $link/scripts.json >/dev/null 2>&1
for i in `seq 0 3`; do
	name[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.nombre"|tr -d '"')
	ruta[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.ruta"|tr -d '"')
	inst[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.instal"|tr -d '"')
	arqs[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.files"|tr -d '"')
	linK[$i]="$link/${name[$i]}.tar"
	scp="${name[$i]}|"
	echo "${name[$i]}|${ruta[$i]}|${inst[$i]}|${arqs[$i]}" >> /usr/local/lib/drowkid/scripts.conf
done
mkdir -p /etc/scripts
[[ ! -e /etc/scripts/scripts.conf ]] && ln -s /usr/local/lib/drowkid/scripts.conf /etc/scripts/scripts.conf
cd /etc/scripts
clear&&clear
msg -b
print_center -m 'INSTALANDO SCRIPTS'
for i in `seq 0 3`; do
	mkdir -p "/etc/scripts/${name[$i]}"
	msg -b&&unset z
	wget -O "/etc/scripts/${name[$i]}/${name[$i]}.tar" "${linK[$i]}" >/dev/null 2>&1
	cd "${ruta[$i]}"&&tar xpf "${name[$i]}.tar" >/dev/null 2>&1
	rm -rf "${ruta[$i]}/${name[$i]}.tar" userDIR 0 *.txt
	echo -e "		\e[1;30m[#\e[1;34mSCRIPT: \e[1;36m${name[$i]}\e[1;30m]\n"&&msg -b
	#[[ $(ls "${ruta[$i]}") == $(echo "${arqs[$i]}") ]] && continue || echo -e "\e[1;31m[x] error al descargar el script ${name[$i]} [x]"&&return $?
	for x in `ls "/etc/scripts/${name[$i]}"`;do
		echo -e "		\e[1;30m[#\e[1;34mfile: ${z:=1} \e[1;36m${x}\e[1;30m]"
		sleep 0.4&&z=$((${z:=1} + 1 ))
		[[ $z == "${#arqs[$i]}" ]] && break
	done
done
}
list-fix(){
scpconf='/etc/scripts/scripts.json'
for i in `seq 0 3`; do
	name[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.nombre"|tr -d '"')
	ruta[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.ruta"|tr -d '"')
	inst[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.instal"|tr -d '"')
	arqs[$i]=$(cat "$scpconf"|jq ".scripts[$i].data.files"|tr -d '"')
done
valuekey="$(date | md5sum | head -c11)"
valuekey+="$(echo $(($RANDOM * 10)) | head -c 6)"
ip=$(wget -qO- ipv4.icanhazip.com)
DIR='/etc/http-shell'&&keY="$2"&&ress="$3"
case $1 in
 --scriptdk1 | --scriptcgh)
	LIST='-SPVweN'&&BASICINST="${arqs[0]}"
		fress='menu_credito'
	instal="${inst[0]}"&&SCPT_DIR="${ruta[0]}"
	keyfnl=$(ofus "$ip:8888/$valuekey/$LIST")
	;;
 --vpsmx)
	LIST='lista-arq'&&BASICINST="${arqs[1]}"
		fress='message.txt'
	instal="${inst[1]}"&&SCPT_DIR="${ruta[1]}"
	keyfnl=$(ofusdk "$ip:8888/$valuekey/$LIST")
	;;
 --latam)
	LIST='lista-arq'&&BASICINST="${arqs[2]}"
		fress='menu_credito'
	instal="${inst[2]}"&&SCPT_DIR="${ruta[2]}"
	keyfnl=$(ofusdk "$ip:8888/$valuekey/$LIST")
	;;
 --darnix)
	LIST='lista-arq'&&BASICINST="${arqs[3]}"
		fress='menu_credito'
	instal="${inst[3]}"&&SCPT_DIR="${ruta[3]}"
	keyfnl=$(ofusdk "$ip:8888/$valuekey/$LIST")
	;;
esac
[[ ! -d $DIR/$keY ]] && mkdir -p $DIR/$keY
echo $ress > "$DIR/${keY}.name"
msg -b&&echo -e "ARQS DE ${1//-/}"
for i in `echo "$BASICINST"`;do
	[[ $(ls ${SCPT_DIR}|grep -w "$i") ]] && continue
	echo "[${w:=1}] <<====>> [$i]"
	cp ${SCPT_DIR}/$i $DIR/$keY/
	echo $i >> $DIR/$keY/$LIST
done
msg -b
echo -e "\e[1;30m[•] \e[1;34mSCRIPT: \e[1;36m${1//-/}"
echo -e "\e[1;30m[•] \e[1;34mKEY: \e[1;37m${keyfnl}"
echo -e "\e[1;30m[•] \e[1;34mINSTALADOR: \e[1;37m${instal}"
enter

}
list_fix() {
    rm ${SCPT_DIR}/*.x.c &>/dev/null
    unset KEY
    KEY="$1"
    name="$2"
    #CRIA DIR
    [[ ! -e ${DIR} ]] && mkdir ${DIR}
    #ENVIA ARQS
    i=0
    VALUE+="gerar.sh http-server.py $BASICINST"
    for arqx in $(ls ${SCPT_DIR}); do
        [[ $(echo $VALUE | grep -w "${arqx}") ]] && continue
        echo -e "[$i] -> ${arqx}"
        arq_list[$i]="${arqx}"
        let i++
    done
    msg -bar
    #read -p " OPCION : " readvalue
    #CRIA KEY
    [[ ! -e ${DIR}/${KEY} ]] && mkdir ${DIR}/${KEY}
    #PASSA ARQS
    [[ -z $readvalue ]] && readvalue="1"
    [[ -z $nombrevalue ]] && nombrevalue="$nomkey$name"
    if [[ $readvalue = @(cgh|1) ]]; then
        #ADM BASIC
        echo -e "  -  KEY SCRIPT BASE ADM  -  "
        arqslist="$BASICINST"
        for arqx in $(echo "${arqslist}"); do
            [[ -e ${DIR}/${KEY}/$arqx ]] && continue #ANULA ARQUIVO CASO EXISTA
            cp ${SCPT_DIR}/$arqx ${DIR}/${KEY}/
            echo "$arqx" >>${DIR}/${KEY}/${LIST}
        done
    else
        for arqx in $(echo "${readvalue}"); do
            #UNE ARQ
            [[ -e ${DIR}/${KEY}/${arq_list[$arqx]} ]] && continue #ANULA ARQUIVO CASO EXISTA
            rm ${SCPT_DIR}/*.x.c &>/dev/null
            cp ${SCPT_DIR}/${arq_list[$arqx]} ${DIR}/${KEY}/
            echo "${arq_list[$arqx]}" >>${DIR}/${KEY}/${LIST}
        done
        echo "TRUE" >>${DIR}/${KEY}/FERRAMENTA
    fi
    rm ${SCPT_DIR}/*.x.c &>/dev/null
    echo "$nombrevalue" >${DIR}/${KEY}.name
    [[ ! -z $IPFIX ]] && echo "$IPFIX" >${DIR}/${KEY}/keyfixa
    echo -e "-------------------------------------------------"
}

ofus() {
    unset txtofus
    number=$(expr length $1)
    for ((i = 1; i < $number + 1; i++)); do
        txt[$i]=$(echo "$1" | cut -b $i)
        case ${txt[$i]} in
        ".") txt[$i]="x" ;;
        "x") txt[$i]="." ;;
        "5") txt[$i]="s" ;;
        "s") txt[$i]="5" ;;
        "1") txt[$i]="@" ;;
        "@") txt[$i]="1" ;;
        "2") txt[$i]="?" ;;
        "?") txt[$i]="2" ;;
        "4") txt[$i]="0" ;;
        "0") txt[$i]="4" ;;
        "/") txt[$i]="K" ;;
        "K") txt[$i]="/" ;;
        esac
        txtofus+="${txt[$i]}"
    done
    echo "$txtofus" | rev
}
fix-key(){
unset nomkey
msg -b
read -p $'\e[1;30m[•] \e[1;34mIngrese el reseller: ' nomkey
[[ ! -n $nomkey ]] && nomkey='@drowkid01'
clear&&clear
figlet 'KEYGEN'|lolcat
msg -b
print_center -m 'ELIJA EL SCRIPT'
menu_func "GENERAR KEY \e[1;30m[#\e[1;36mSCRIPTDK1\e[1;30m]" "GENERAR KEY \e[1;30m[#\e[1;36mVPSMX/CASITA\e[1;30m]" "GENERAR KEY \e[1;30m[#\e[1;36mSCRIPT-LATAM\e[1;30m]" "GENERAR KEY \e[1;30m[#\e[1;36mSCRIPT-DARNIX\e[1;30m]"
back
ops=$(selection_fun 4)
case $ops in
  1)list-fix --scriptdk1;;
  2)list-fix --vpsmx;;
  3)list-fix --latam;;
  4)list-fix --darnix;;
esac
}
fix_key() {
    unset nomkey
    echo " Bienvenido, Porfavor dijita el Nombre del DUE�O de la KEYs"
    echo "             DIJITA  0  PARA REGRESAR "
    read -p " RESELLER deL Keys : " nomkey
    [[ $nomkey = 0 ]] && return
    [[ -z $nomkey ]] && {
        [[ -e /etc/menu_ito ]] && nomkey="$(cat /etc/menu_ito)" || nomkey="$(curl -sSL "https://www.dropbox.com/s/z38hj1mz3q9krso/menu_credito")"
    }
    msg -bar
    echo " Bienvenido, Porfavor ingresa el numero de keys a generar"
    read -p "Numero de Keys : " numk
    [[ -z ${numk} ]] && numk=0
    msg -bar
    read -p "INGRESA NOMBRE DE VENDEDOR : " vkey
    clear
    msg -bar
    echo -e "$numk Keys activas,de $nomkey y esperando instalaci�n!"
    echo "$nomkey" >${SCPT_DIR}/menu_credito
    for ((w = 0; w < $numk; w++)); do
        xyz+="$(echo $(($RANDOM * 3)) | head -c 4)"
        #valuekey+="$(echo $(($RANDOM*10))|head -c 5)"
        list_fix "$valuekey" "$(($w + 1))"
        keyfinal=$(ofus "$(wget -qO- ipv4.icanhazip.com):8888/$valuekey/$LIST")
        echo -e " ${CHeko} Key $(($w + 1)) Exitosa! $(printf '%(%D-%H:%M:%S)T')  \n"
        echo -e " ${TTcent}  ${keyfinal}  ${TTcent}" | pv -qL 80
    done
    [[ -e /etc/menu_ito ]] && cat /etc/menu_ito >${SCPT_DIR}/menu_credito || echo -e "\nCreditos Aplicados Exitosamente"
    #$(cat < /etc/CAT-BOT/resell)
    msg -bar
    echo -e " ${ScT}  *INSTALADOR UNIVERSAL*   ${ScT}"
    msg -bar && echo -ne "$(msg -verd "apt update -y &&") $(msg -aqua "apt upgrade -y &&\n") " && msg -bra "\033[7;49;35m wget -q https://raw.githubusercontent.com/ChumoGH/ScriptCGH/main/setup && chmod 777 setup && ./setup --install"
    msg -bar
    [[ -e /etc/menu_numito ]] && menumito="$(cat /etc/menu_numito)" || menumito="https://t.me/ChumoGH_bot"
    [[ -z $vkey ]] && vkey="@drowkid01"
    echo -e " ${ceLL} Soporte : Ubuntu 14.04 - 16.04 - 18.04 - 20.04 - 21.04 - 22.04\n         S.O PREFERENTE 18.04"
    echo -e " Verificada: $vkey ${ScT} RESELLER ACTUAL : $nomkey" | pv -qL 80
    msg -bar
    read -p "Enter para finalizar"
}

databot(){
clear&&clear
figlet 'CONFBOT'|lolcat
msg -b
print_center -m 'CONFIGURACIÓN DE DATOS DEL BOT'
msg -b
case $1 in
 --id)
	while read -p $'\e[1;30m[•] \e[1;34mIngresa tu id: ' id; do
		if [[ -z $id ]]; then
			id='7570680285'
		fi
		break
	done
	echo "$id" > /etc/CAT-BOT/Admin-ID
	tput cuu1&&tput dl1;;
 --token)
	while read -p $'\e[1;30m[•] \e[1;34mIngresa tu token: ' token; do
		if [[ -z $token ]]; then
			tput cuu1&&tput dl1
			sleep 0.4&&msg -r "[x] INGRESA UN TOKEN VÁLIDO [x]"&&sleep 0.4
			tput cuu1&&tput dl1
			unset token&&read -p $'\e[1;30m[•] \e[1;34mIngresa tu token: \e[1;36m' token
		else
			break
		fi
	done
	;;
esac
}

downbot(){
unset -v owner link
owner=$(if [[ "$(wget -qO- ipv4.icanhazip.com)" == '45.56.112.117' ]]; then
	echo -e "drowkid01"
else
	echo -e "darnix"
fi)
link='https://darix.ooguy.com/drowkid01/dpbt/raw/branch/main/exec'

files=( "BotGen.sh|/etc/CAT-BOT" "http-server.sh|/bin/http-server.sh" )
for i in "${files[@]}"; do
	ab=$(echo "$i"|awk -F '|' '{print $1}')
	ba=$(echo "$i"|awk -F '|' '{print $2}')
	wget -O ${ba}/${ab} ${link}/${owner}/${ab} >/dev/null 2>&1
	chmod +rwx ${ba}/${ab}
done
msg -b
echo -e "\e[1;32m[+] BOTGEN DESCARGADO CORRECTAMENTE [+]"
}


