#!/bin/sh

#funciones script
VERDE='\033[0;32m'
ROJO='\033[0;31m'
AZUL='\033[0;34m'
NC='\033[0m'


function Mensaje() {
	echo -e "$2$1${NC}"
}

function logo(){
	clear
	echo -e "\e[32m${VERDE} ******************************************************************************\e[0m"
	echo -e "\e[32m${VERDE} *      _ _    _ _   _  _____ _      ______   _______ ______          __  __  *\e[0m"
    echo -e "\e[32m${VERDE} *     | | |  | | \ | |/ ____| |    |  ____| |__   __|  ____|   /\   |  \/  | *\e[0m"
    echo -e "\e[32m${VERDE} *     | | |  | |  \| | |  __| |    | |__ ______| |  | |__     /  \  | \  / | *\e[0m"
    echo -e "\e[32m${VERDE} *  _  | | |  | |     | | |_ | |    |  __|______| |  |  __|   / /\ \ | |\/| | *\e[0m"
    echo -e "\e[32m${VERDE} * | |_| | |__| | |\  | |__| | |____| |____     | |  | |____ / ____ \| |  | | *\e[0m"
    echo -e "\e[32m${VERDE} * \____/ \____/|_| \_|\_____|______|______|    |_|  |______/_/    \_\_|  |_| *\e[0m"        
	echo -e "\e[32m${VERDE} *        																	 *\e[0m"
	echo -e "\e[32m${VERDE} *                      Creacion Oscam auto update      					     *\e[0m"
	echo -e "\e[32m${VERDE} *      grupo telegram: https://t.me/joinchat/Bv0_2hZ8jH6dsUJFoYG-Rg          *\e[0m"
	echo -e "\e[32m${VERDE} *                                                                            *\e[0m"
	echo -e "\e[32m${VERDE} *                           jungle-team.com                                  *\e[0m"
	echo -e "\e[32m${VERDE} ******************************************************************************\e[0m"
}



function check_imagen() {
	if [ -f /etc/bhmachine ] || [ -f /etc/vtiversion.info ] || [ -f /etc/bpmachine ] || [ -f /tmp/infopanel.ver ] || [ -f /usr/lib/enigma2/python/Plugins/PLi/__init__.pyo ] || [ -f /etc/openSPAP ] || [ -f /usr/emu_scripts/EGcam_Ci.sh ] || [ -f /usr/script/ItalySat_log.sh ] || [ -f /usr/lib/enigma2/python/OPENDROID/OPD_panel.pyo ] || [ -f /usr/lib/enigma2/python/Plugins/SystemPlugins/PurE2PiconPathSet/plugin.pyo ];
	then
        if [ -f /etc/oscam.update ];
        then
           echo "======================================================================"
           Mensaje "Parece que existe una instalacion previa no se realiza instalacion" $ROJO
           echo "======================================================================"
        else
           echo "se procede a instalacion oscam update"
           instalacion
        fi
    else
       clear
       echo "============================================"
       Mensaje "No tiene imagen compatible" $ROJO
       echo "============================================"
     
    fi

}

function instalacion() {
	if [ -f /etc/bhmachine ];
	then
	   echo "se procede a instalacion script cam blackhole"
	   cat <<'EOF' > /usr/camscript/Ncam_oscam_conclave.sh
#!/bin/sh
########################################
###### Powered by Vu+ Blackhole   ######
##   http://www.jungle-team.com       ##
########################################

CAMNAME="Oscam_update"

remove_tmp () {
	rm -rf /tmp/*.info* /tmp/*.tmp*
}

case "$1" in
	start)
	echo "[SCRIPT] $1: $CAMNAME"
	remove_tmp
	/usr/bin/oscam-update -b -r 2 -c /etc/tuxbox/config/oscam-update
	sleep 2
	;;
	stop)
	echo "[SCRIPT] $1: $CAMNAME"
	killall -9 oscam-update 2>/dev/null
	sleep 1
	remove_tmp
	;;
	restart)
	$0 stop
	sleep 2
	$0 start
	exit
	;;
	*)
	$0 stop
	exit 0
	;;
esac

exit 0
EOF

	chmod +x /usr/camscript/Ncam_oscam_conclave.sh
	Mensaje "Instalado daemon" $VERDE
    fi
	if [ -f /etc/bpmachine ];
	then
	   echo "se procede a instalacion script cam blackhole"
	   cat <<'EOF' > /usr/camscript/Ncam_oscam_conclave.sh
#!/bin/sh
########################################
###### Powered by Vu+ Blackhole   ######
##   http://www.jungle-team.com       ##
########################################

CAMNAME="Oscam_update"

remove_tmp () {
	rm -rf /tmp/*.info* /tmp/*.tmp*
}

case "$1" in
	start)
	echo "[SCRIPT] $1: $CAMNAME"
	remove_tmp
	/usr/bin/oscam-update -b -r 2 -c /etc/tuxbox/config/oscam-update
	sleep 2
	;;
	stop)
	echo "[SCRIPT] $1: $CAMNAME"
	killall -9 oscam-update 2>/dev/null
	sleep 1
	remove_tmp
	;;
	restart)
	$0 stop
	sleep 2
	$0 start
	exit
	;;
	*)
	$0 stop
	exit 0
	;;
esac

exit 0
EOF

	chmod +x /usr/camscript/Ncam_oscam_conclave.sh
	Mensaje "Instalado daemon" $VERDE
    fi
	if [ -f /etc/vtiversion.info ];
    then
       echo "se procede a instalacion script cam vti"
	   cat <<'EOF' > /usr/script/Oscam_conclave_cam.sh
#!/bin/sh
########################################
###### Powered by Vu+ vti         ######
##   http://www.jungle-team.com       ##
########################################

CAMNAME="OScam update"


remove_tmp () {
	rm -rf /tmp/cainfo.* /tmp/camd.* /tmp/sc.* /tmp/*.info* /tmp/*.tmp* /tmp/oscam*
	[ -e /tmp/.oscam ] && rm -rf /tmp/.oscam
	[ -e /tmp/.emu.info ] && rm -rf /tmp/.emu.info
	[ -e /tmp/oscam.mem ] && rm -rf /tmp/oscam.mem
}

case "$1" in
	start)
		echo "[SCRIPT] $1: $CAMNAME"
		remove_tmp
		touch /tmp/.emu.info
		echo "oscam-update" > /tmp/.emu.info
		/usr/bin/oscam-update -b -r 2 -c /etc/tuxbox/config/oscam-update
	;;
	stop)
		echo "[SCRIPT] $1: $CAMNAME"
		kill `pidof oscam-update`
		remove_tmp
	;;
	restart)
		$0 stop
		sleep 2
		$0 start
		exit
	;;
	*)
		$0 stop
		exit 0
	;;
esac

exit 0
EOF

	chmod +x /usr/script/Oscam_conclave_cam.sh
	Mensaje "Instalado daemon" $VERDE

    fi
	if [ -f /tmp/infopanel.ver ];
    then
       echo "se procede a instalacion script cam openatv"
	   cat <<'EOF' > /etc/init.d/softcam.oscam_conclave
#!/bin/sh
########################################
###### Powered by openatv         ######
##   http://www.jungle-team.com       ##
########################################

case "$1" in
start)
    ulimit -s 1024
    /usr/bin/oscam-update --wait 0 --config-dir /etc/tuxbox/config/oscam-update --daemon --pidfile /tmp/oscam.pid --restart 2 
    ;;
stop)
    kill `cat /tmp/oscam.pid` 2> /dev/null
    ;;
restart|reload)
    $0 stop
    sleep 1
    $0 start
    ;;
version)
    echo "oscam-update"
    ;;
info)
    echo "oscam-update"
    ;;
*)
    echo "Usage: $0 start|stop|restart"
    exit 1
    ;;
esac
exit 0
EOF

	chmod +x /etc/init.d/softcam.oscam_conclave
	Mensaje "Instalado daemon" $VERDE
    fi
    
	if [ -f /usr/lib/enigma2/python/OPENDROID/OPD_panel.pyo ];
    then
       echo "se procede a instalacion script cam OPENDROID"
	   cat <<'EOF' > /etc/init.d/softcam.oscam_conclave
#!/bin/sh
########################################
###### Powered by OPENDROID       ######
##   http://www.jungle-team.com       ##
########################################

case "$1" in
start)
    ulimit -s 1024
    /usr/bin/oscam-update --wait 0 --config-dir /etc/tuxbox/config/oscam-update --daemon --pidfile /tmp/oscam.pid --restart 2 
    ;;
stop)
    kill `cat /tmp/oscam.pid` 2> /dev/null
    ;;
restart|reload)
    $0 stop
    sleep 1
    $0 start
    ;;
version)
    echo "oscam-update"
    ;;
info)
    echo "oscam-update"
    ;;
*)
    echo "Usage: $0 start|stop|restart"
    exit 1
    ;;
esac
exit 0
EOF

	chmod +x /etc/init.d/softcam.oscam_conclave
	Mensaje "Instalado daemon" $VERDE
    fi
	if [ -f /etc/openSPAP ];
    then
       echo "se procede a instalacion script cam openspa"
	   cat <<'EOF' > /usr/script/oscam_conclave_cam.sh
#!/bin/sh
########################################
###### Powered by openspa         ######
##   http://www.jungle-team.com       ##
########################################

#!/bin/sh

CAMD_ID=1796
CAMD_NAME="Oscam conclave"

INFOFILE_A=ecm.info
INFOFILE_B=ecm1.info
INFOFILE_C=ecm2.info
INFOFILE_D=ecm3.info
INFOFILE_E=ecm4.info
INFOFILE_F=ecm5.info
#Expert window
INFOFILE_LINES=1111111111000000
#Zapp after start
REZAPP=0

########################################

logger $0 $1
echo $0 $1

remove_tmp () {
  rm -rf /tmp/*.info* /tmp/*.tmp* /tmp/*mbox* /tmp/*share* /tmp/*.pid* /tmp/*sbox* /tmp/*oscam*
}

case "$1" in
  start)
  remove_tmp
  /usr/bin/oscam-update -b -r 2 -c /etc/tuxbox/config/oscam-update &
  ;;
  stop)
  killall -9 oscam-update 2>/dev/null
  sleep 2
  remove_tmp
  ;;
  *)
  $0 stop
  exit 0
  ;;
esac

exit 0
EOF

	chmod +x /usr/script/oscam_conclave_cam.sh
	Mensaje "Instalado daemon" $VERDE
    fi
	if [ -f /usr/lib/enigma2/python/Plugins/PLi/__init__.pyo ];
    then
       echo "se procede a instalacion script cam openpli"
	   cat <<'EOF' > /etc/init.d/softcam.oscam_conclave
#!/bin/sh
########################################
###### Powered by openpli         ######
##   http://www.jungle-team.com       ##
########################################

case "$1" in
start)
	ulimit -s 1024
	/usr/bin/oscam-update --wait 0 --config-dir /etc/tuxbox/config/oscam-update --daemon --pidfile /tmp/oscam.pid --restart 2 
	;;
stop)
	kill `cat /tmp/oscam.pid` 2> /dev/null
	;;
restart|reload)
	$0 stop
	sleep 1
	$0 start
	;;
version)
	echo "oscam-update"
	;;
info)
	echo "oscam-update"
	;;
*)
	echo "Usage: $0 start|stop|restart"
	exit 1
	;;
esac
exit 0
EOF

	chmod +x /etc/init.d/softcam.oscam_conclave
	Mensaje "Instalado daemon" $VERDE
    fi
    
	if [ -f /usr/script/ItalySat_log.sh ];
    then
       echo "se procede a instalacion script cam italysat"
	   cat <<'EOF' > /usr/emuscript/Oscam_conclave_em.sh
#!/bin/sh
#emuname=oscam conclave
#ecminfofile=ecm.info

remove_tmp () {
	rm -rf /tmp/*.info* /tmp/*.tmp*
}

case "$1" in
	start)
	echo "[SCRIPT] $1: $CAMNAME"
	remove_tmp
	/usr/bin/oscam-update -b -c /etc/tuxbox/config/oscam-update & 2>/dev/null
	sleep 2
	;;
	stop)
	echo "[SCRIPT] $1: $CAMNAME"
	killall -9  oscam-update 2>/dev/null
	sleep 1
	remove_tmp
	;;
	*)
	$0 stop
	exit 0
	;;
esac

exit 0
EOF

	chmod +x /usr/emuscript/Oscam_conclave_em.sh
	Mensaje "Instalado daemon" $VERDE
    fi
    
	if [ -f /usr/emu_scripts/EGcam_Ci.sh ];
    then
       echo "se procede a instalacion script cam Egami"
	   cat <<'EOF' > /usr/emu_scripts/EGcam_oscamConclave.sh
#!/bin/sh

CAMNAME="Oscam Conclave"

remove_tmp () {
	rm -rf /tmp/*.info* /tmp/*.tmp*
}

case "$1" in
	start)
	echo "[SCRIPT] $1: $CAMNAME"
	remove_tmp
	/usr/bin/oscam-update -v -b -c /etc/tuxbox/config/oscam-update &
	sleep 2
	;;
	stop)
	echo "[SCRIPT] $1: $CAMNAME"
	killall -9 oscam-update 2>/dev/null
	sleep 1
	remove_tmp
	;;
	*)
	$0 stop
	exit 0
	;;
esac

exit 0
EOF

	chmod +x /usr/emu_scripts/EGcam_oscamConclave.sh
	Mensaje "Instalado daemon" $VERDE
    fi


	if [ -f /usr/lib/enigma2/python/Plugins/SystemPlugins/PurE2PiconPathSet/plugin.pyo ];
    then
       mkdir /usr/script/cam
       echo "se procede a instalacion script cam Pure2"
	   cat <<'EOF' > /usr/script/cam/oscam_conclave.sh
#!/bin/sh

OSD="Oscam-conclave"
PID=`pidof oscam-conclave`
Action=$1

cam_clean () {
	rm -rf /tmp/.oscam  /tmp/oscam*
}

cam_handle () {
	if test	-z "${PID}"	; then
		cam_up;
	else
		cam_down;
	fi;
}

cam_down ()	{
	killall	-9 oscam-update
	sleep 2
	cam_clean
}

cam_up () {
	/usr/bin/oscam-update -S -c /etc/tuxbox/config/oscam-update &
}

if test	"$Action" =	"cam_res" ;	then
		cam_down
		cam_up
elif test "$Action"	= "cam_down" ; then
		cam_down
elif test "$Action"	= "cam_up" ; then
		cam_up
else
		cam_handle
fi

exit 0
EOF

	chmod +x /usr/script/cam/oscam_conclave.sh
	Mensaje "Instalado daemon" $VERDE
    fi

}


function auto_oscam() {
	if [ ! -f /etc/tuxbox/config/oscam-update ];
	then
       mkdir -p /etc/tuxbox/config/oscam-update
    fi
	if [ -f /usr/bin/autooscam.sh ];
	then
       echo "Ya se tiene instalado script auto"
    else
       echo "Se realiza instalacion de script auto"
	   wget -q http://tropical.jungle-team.online/oscam/autooscam.sh -O /usr/bin/autooscam.sh
	   chmod +x /usr/bin/autooscam.sh
	   /usr/bin/autooscam.sh
    fi
}

function check_cron_openpli() {
	if [ -f /usr/lib/enigma2/python/Plugins/PLi/__init__.pyo ];
	then
	   if [ ! -f /etc/init/busybox-cron ];
       then
           echo "se instala cron"  
           opkg update
           opkg install busybox-cron
       fi
    fi
}

cronjob_editor () {

if [[ -z "$1" ]] ;then printf " no interval specified\n" ;fi
if [[ -z "$2" ]] ;then printf " no command specified\n" ;fi
if [[ -z "$3" ]] ;then printf " no action specified\n" ;fi

if [[ "$3" == add ]] ;then
    # add cronjob, no duplication:
    ( crontab -l | grep -v -F -w "$2" ; echo "$1 $2" ) | crontab -
elif [[ "$3" == remove ]] ;then
    # remove cronjob:
    ( crontab -l | grep -v -F -w "$2" ) | crontab -
fi
}

function add_to_crontab() {

	cronjob_editor "*/15 * * * *" "/usr/bin/autooscam.sh" "add"
	Mensaje "Añadido un cron para actualizar oscam" $VERDE
}

function add_to_crontab_blackhole() {

    if [ -f /etc/bhmachine ];
    then
    cp /etc/cron/crontabs/root /etc/bhcron/bh.cron
    crontab /etc/bhcron/bh.cron -c /etc/bhcron/
    fi
}

logo
check_imagen
auto_oscam
check_cron_openpli
add_to_crontab
add_to_crontab_blackhole


