# Mount Magisk
if [ ! -d /sbin/.core/mirror/bin ]; then
    mkdir -p /sbin/.core/mirror/bin
    mount -o bind /data/adb/magisk /sbin/.core/mirror/bin
fi
# Create Energized Protection Directory if not available 
if [ ! -d /sdcard/EnergizedProtection ]; then
    mkdir -p /sdcard/EnergizedProtection;
fi
# Main Screen Starts
echo "-----------~------------"
echo "  \033[1;33mEnergized Protection\033[0m  "
echo "-----------~------------"
echo "ad.porn.malware blocking."
echo "Let's make an annoyance free better open internet, altogether!"
# Check if the Systemless Hosts is enabled or not
if [ ! -f /sbin/.core/img/.core/hosts ]; then
    echo "\n\033[33;5;7m[-] Systemless Hosts isn't enabled!\033[0m"
    echo "\033[37;5;7m[+] Enable it in 'Magisk Manager > Settings > Systemless Hosts'\033[0m"
    sleep 1
    echo "\033[36;5;7m[+] Re-run script after enabling Systemless Hosts!\033[0m"
    echo "\033[31;5;7m[-] Exiting!\033[0m"
    sleep 3
    exit 0
else
    echo "\033[32;1m\n[+] Systemless Hosts Enabled!\033[0m"
fi
# Check if Other Adblocker is installed Ex. Adaway
if [ -d /data/data/org.adaway/ ]; then
    echo "\n\033[33;5;7m[-] Other adblocker is detected!\033[0m"
    sleep 1
    echo "\033[36;5;7m[+] Make sure to disable them, to avoid any issue using Energized!\033[0m"
    sleep 1
elif [ -d /data/data/ru.dixl0f0s.unifiedhostsmanager ]; then
    echo "\n\033[33;5;7m[-] Other adblocker is detected!\033[0m"
    sleep 1
    echo "\033[36;5;7m[+] Make sure to disable them, to avoid any issue using Energized!\033[0m"
    sleep 1
fi
# Check if the Whitelist is there, else creates
if [ ! -f /sdcard/EnergizedProtection/blacklist ]; then
   echo "\n[+] Creating Blacklist.\n";
   touch /sdcard/EnergizedProtection/blacklist
fi
# Check if the Blacklist is there, else creates 
if [ ! -f /sdcard/EnergizedProtection/whitelist ]; then
   echo "[+] Creating Whitelist.";
   touch /sdcard/EnergizedProtection/whitelist
fi
# Aliases, Grep, Wget Variables
alias wget="/sbin/.core/busybox/wget"
alias grep="/sbin/.core/busybox/grep"
HOST=/sbin/.core/img/.core/hosts
TREADME=/sdcard/EnergizedProtection/readme
FILTER=/sdcard/EnergizedProtection/filter
WHITELIST=/sdcard/EnergizedProtection/whitelist
BLACKLIST=/sdcard/EnergizedProtection/blacklist
TEMP=/cache/energizedtemp
PTEMP=/cache/energizedporntemp
STEMP=/cache/energizedsocialtemp
COUNT=1
# Check if Root was granted or not
id="$(id)"; id="${id#*=}"; id="${id%%\(*}"; id="${id%% *}"
if [ "$id" != "0" ] && [ "$id" != "root" ]; then
    root="\e[1;31mDENIED\e[0m";
else
    root="\e[1;32mGRANTED\e[0m";
fi;
# Other Variables
update="\e[1;36m`busybox date -r /sbin/.core/img/.core/hosts`\e[0m"
busybox="\e[1;33m`busybox | head -1 | cut -f 2 -d ' '`\e[0m"
size="\e[1;35m`ls -lah /sbin/.core/img/.core/hosts | awk '{print $5}'`\e[0m"
# Check if System's Adblocker is enabled or not
   [ -f /sbin/.core/img/.core/hosts ];
if busybox grep -q adblocker /sbin/.core/img/.core/hosts; then
	adblocker="\e[1;32mENABLED\e[0m"
else
	adblocker="\e[1;31mDISABLED\e[0m"
fi
# Check if Energized is applied or not
	[ -f /sbin/.core/img/.core/hosts ];
if busybox grep -q adroitadorkhan.github.io /sbin/.core/img/.core/hosts; then
	eonoff="\e[1;32mENABLED\e[0m"
else
	eonoff="\e[1;31mDISABLED\e[0m"
fi
# If Energized is enabled, which pack it is
	[ -f /system/etc/hosts ];
if busybox grep -q "Energized Ad" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mAd Protection\e[0m"	
elif busybox grep -q "Energized Malware" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mMalware Protection\e[0m"
elif busybox grep -q "Energized Porn" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mPorn Protection\e[0m"
elif busybox grep -q "Energized Blu" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mBlu Protection\e[0m"
elif busybox grep -q "Energized go" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mBlu go Protection\e[0m"
elif busybox grep -q "EnergizedLite" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mLite Protection\e[0m"
elif busybox grep -q "EnergizedPornLite" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mPorn Lite Protection\e[0m"
elif busybox grep -q "Energized Ultimate" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mUltimate Protection\e[0m"
elif busybox grep -q "Energized Unified" /sbin/.core/img/.core/hosts; then
	echeck="\e[1;32mUnified Protection\e[0m"
else
	echeck="\e[1;31mNo Pack Detected!\e[0m"
fi
# Check if Hosts Update is available
echo "\n[+] Checking Update..."
echo "[+] NOTE: Data connection is required to download the hosts."
wget -q -O $TREADME http://ador.chorompotro.com/Magisk.txt
grep "Updated" $TREADME
if [ -f $FILTER ]; then
	CURRENT=$(grep "Updated" $TREADME)
	LAST_UPDATED=$(sed '1q;d' $FILTER)
	if [ "$LAST_UPDATED" != "$CURRENT" ]; then
	    echo "\033[33;1m---------------------\033[0m"
		echo -e "\033[33;5;7m[+] Update available!\033[0m"
		echo "\033[33;1m---------------------\033[0m"
	else
	
		echo "\033[33;1m------------------------\033[0m"
		echo "[+] No update available."
		echo "\033[33;1m------------------------\033[0m"
	fi
else
	touch $FILTER
	echo "[+] No hosts file from this module yet applied!"
fi
while true
do
	INPUT=$(eval "echo \$$COUNT")
	if [ ! "$INPUT" ]; then
		if [ "$1" ]; then
			rm -f $TREADME
			exit 0
		fi
# Starts Screen Echos
echo -e -n "\n[+] Energized All-In-One Magisk Module
[+] Version: 2.5
[+] Author: Energized Protection
 
[+] Checking Root & Busybox..
[+] ROOT: $root
[+] BUSYBOX: $busybox

[+] Checking Hosts...
[+] ENERGIZED: $eonoff - $echeck
[+] ADBLOCK STATUS: $adblocker
[+] SIZE: $size
[+] LAST UPDATED: $update \n"
		
        echo "\n\033[33;1m[+] Select an option below:\033[0m"
		echo "---------------~-----------------"
		echo "[1] Energized Ad Protection"
		echo "[2] Energized Malware Protection"
		echo "[3] Energized Porn Protection"
		echo "[4] Energized Blu Protection"
		echo "[5] Energized Blu go Protection"
		echo "[6] Energized Lite Protection"
		echo "[7] Energized Porn Lite Protection"
		echo "[8] Energized Ultimate Protection"
		echo "[9] Energized Unified Protection"
		
		echo "\n\033[33;1m[+] Extensions:\033[0m"
		echo "---------------~-----------------"
		echo "[p] Add Porn Blocking to current host."
		echo "[s] Add Social Blocking to current host."
		
		echo "\n\033[33;1m[+] Other Options:\033[0m"
		echo "---------------~-----------------"
		echo "[i] Info About The Packs (Opens in Browser)"
		echo "[c] Clear Hosts File"
		echo "[b] Apply Blacklist"
		echo "[w] Apply Whitelist"
		echo "[q] Quit."
		echo "[-] To disable, just toggle the enable systemless hosts in magisk manager." 
		echo -n "[+] Your Input (1-9, p, s, i, c, b, w & q) - "
		read -r INPUT
		if [ "$INPUT" != "m" ]; then
			INPUT="$(echo "$INPUT" | sed 's/m//g')"
		fi
		DIR=""
	fi
	case "$INPUT" in
		1) DIR="master/EnergizedAd/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Ad\033[0m"
		;;
		2) DIR="master/EnergizedMalware/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Malware\033[0m"
		;;
		3) DIR="master/EnergizedPorn/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Porn\033[0m"
		;;
		4) DIR="master/EnergizedBlu/energized/blu"
		echo "\033[1;33m[+] Applying Energized Blu\033[0m"
		;;
		5) DIR="master/EnergizedBlugo/energized/blu_go"
		echo "\033[1;33m[+] Applying Energized Blu go\033[0m"
		;;
		6) DIR="master/EnergizedLite/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Lite\033[0m"
		;;
		7) DIR="master/EnergizedPornLite/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Porn Lite\033[0m"
		;;
		8) DIR="master/EnergizedUltimate/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Ultimate\033[0m"
		;;
		9) DIR="master/EnergizedUnified/energized/hosts"
		echo "\033[1;33m[+] Applying Energized Unified\033[0m"
		;;
		w)  if [ ! -f $WHITELIST ]; then
				echo ""
				echo "[-] No Whitelist detected"
				sleep 1
				echo "[+] Copy your whilelist to /sdcard/EnergizedProtection"
				sleep 2
		    else
				if [ -f "$HOST" ]; then
					echo "\n\033[32;5;7m[+] Whitelist Found!\033[0m"
					echo "[+] Applying Whitelist"
					sleep 1
					grep -Fvxf $WHITELIST $HOST > $TEMP
					mv $TEMP $HOST
					sleep 1
					grep -Fvf $WHITELIST $HOST > $TEMP
					mv $TEMP $HOST
					sleep 1
					grep -Fvf $WHITELIST $HOST > $TEMP
					mv $TEMP $HOST
					sleep 1
					echo "\033[36;5;7m[-] Returning to the main screen.\033[0m\n"
					sleep 1
				else
					echo ""
					echo "[-] No hosts file detected"
					sleep 1
					echo "[+] Apply a hosts file first"
					sleep 1
				fi
		    fi
		;;
		d)  if [ ! -f $WHITELIST ]; then
				echo ""
				echo "[-] No Whitelist detected"
				sleep 1
				echo "[+] Copy your whilelist to /sdcard/EnergizedProtection"
				sleep 2
		    else
				if [ -f "$HOST" ]; then
					echo "\n\033[32;5;7m[+] Whitelist Found!\033[0m"
					echo "[+] Applying Whitelist"
					sleep 2
					grep -Fvf $WHITELIST $HOST > $TEMP
					mv $TEMP $HOST
					echo "\033[36;5;7m[-] Returning to the main screen.\033[0m\n"
					sleep 3 
				else
					echo ""
					echo "[-] No hosts file detected"
					sleep 1
					echo "[+] Apply a hosts file first"
					sleep 2
				fi
		    fi
		;;
		r)  if [ ! -f $WHITELIST ]; then
				echo ""
				echo "\033[31;5;7m[-] No Whitelist detected.\033[0m"
				echo "[+] Copy your whilelist to /sdcard/EnergizedProtection"
		    else
				if [ -f "$HOST" ]; then
					echo "\n\033[32;5;7m[+] Whitelist Found!\033[0m"
					echo "[+] Applying Whitelist (regex)"
					sleep 2
					grep -vxf $WHITELIST $HOST > $TEMP
					mv $TEMP $HOST
					echo "\033[36;5;7m[-] Returning to the main screen.\033[0m\n"
					sleep 3 
				else
					echo "\033[31;5;7m[-] No hosts file detected\033[0m"
					sleep 1
					echo "[+] Apply a hosts file first"
					sleep 2
				fi
		    fi
		;;
		b)	if [ ! -f $BLACKLIST ]; then
				echo ""
				echo "\033[31;5;7m[-] No Blacklist detected.\033[0m"
				echo "[+] Copy your blacklist to /sdcard/EnergizedProtection"
			else
				if [ -f "$HOST" ]; then
					if [ -s $BLACKLIST ]; then
						echo ""
						echo "[+] Applying Blacklist..."
						sleep 2
						NEWFILTERS="$(cat $BLACKLIST)"
						printf '%s\n' "$NEWFILTERS" | while IFS= read -r linenew
						do
						  if [ ! "$(grep -Fx "$linenew" $HOST)" ]; then
							sed -i -e "\$a0.0.0.0 $linenew" $HOST
							echo "\033[36;5;7m[-] Returning to the main screen.\033[0m\n"
						sleep 3 
						  fi
						done
					else
						echo "\033[31;5;7m[-] Blacklist file is empty!\033[0m"
						sleep 2
						echo "\n[-] NO NEW FILTER ADDED!\n"
						echo "\033[36;5;7m[-] Returning to the main screen\033[0m\n"
						sleep 3 
					fi
				else
					echo "\033[31;5;7m[-] No hosts file detected\033[0m"
					sleep 1
					echo "[+] Apply a hosts file first"
					sleep 2
				fi
		    fi
			;;
		q) break
		;;
		i) if [ "$INPUT" -eq i ]; then
		     am start -a android.intent.action.VIEW -d "https://github.com/EnergizedProtection/EnergizedHosts/blob/master/README.md"
		   fi
		;;
		c) rm -rf /sbin/.core/img/.core/hosts
		echo "[+] Clearing Hosts File..."
		sleep 3
		cat >> /sbin/.core/img/.core/hosts <<EOF
# Energized - ad.porn.malware blocking.
# A merged collection of hosts from reputable sources.
# https://ador.chorompotro.com/energized

127.0.0.1 localhost
0.0.0.0   0.0.0.0

EOF
        echo "[+] Done Clearing Hosts!"
        sleep 1
        ;;
        p)  wget -O $PTEMP https://raw.githubusercontent.com/EnergizedProtection/EnergizedHosts/master/EnergizedPornLite/energized/hosts
			echo "[+] Applying Porn Hosts to current hosts."
            mv $HOST $TEMP
			cat $TEMP $PTEMP > $HOST
			awk '!a[$0]++' $HOST > $PTEMP && mv -f $PTEMP $HOST
			rm -f $TEMP $PTEMP
			echo "[+] Done applying!"
		;;
		s)  wget -O $STEMP https://raw.githubusercontent.com/Sinfonietta/hostfiles/master/social-hosts
			echo "[+] Applying Social Hosts to current hosts."
            mv $HOST $TEMP
			cat $TEMP $STEMP > $HOST
			awk '!a[$0]++' $HOST > $STEMP && mv -f $STEMP $HOST
			rm -f $TEMP $STEMP
			echo "[+] Done applying!"
		;;
		*)  echo ""
			echo "Invalid input. Don't use any spaces between letters."
		;;
	esac
	if [ $DIR ]; then
		echo ""
		wget -O $HOST https://raw.githubusercontent.com/EnergizedProtection/EnergizedHosts/$DIR
		echo "\n\033[32;5;7m[+] Done Applying!\033[0m"
		sleep 3
		grep "Updated" $TREADME > $FILTER
		sleep 3
		echo "\033[37;5;7m[+] Returning to main screen in 3 seconds...\033[0m\n"
	fi
	DIR=""
	COUNT=$((COUNT+1))
done
rm -f $TREADME
echo "[+] Done!"
echo "\033[1;33m-~- Stay Energized Buddy! -~-\033[0m"