#!/bin/sh
#
# http://EasyToYou.eu

MKDIR="mkdir -p"

ENCODED_FOLDER=ENCODED
DECODED_FOLDER=DECODED
DELETE_RAR=DECODED.rar
BUG_OUTPUT_FILE="main.log"
LOG_FILE=log.txt
PHP_COMMAND="php\IC6\php.exe -c php\IC6\php.ini"
NUMBER_DECODED_FILES=0

if [ -d "${DECODED_FOLDER}" ]; then
	rm -rf ${DECODED_FOLDER}
	rm -rf ${DELETE_RAR}
fi

${MKDIR} "${DECODED_FOLDER}"

if [ -d "${ENCODED_FOLDER}" ] && [ -d "${DECODED_FOLDER}" ]; then
	echo -e "### EasyToYou.eu Log ###\r" > "${DECODED_FOLDER}/${LOG_FILE}"

	for FILE in `find "${ENCODED_FOLDER}"`; do {
		IS_DECODED=0
		FILENAME=`echo ${FILE} | awk -F '/' '{print $NF}'`
		DESTINATION=`echo ${FILE} | sed -e "s/^${ENCODED_FOLDER}/${DECODED_FOLDER}/;"`
		DESTINATION_FOLDER=`dirname "${DESTINATION}"`

		if [ ! -d "${DESTINATION_FOLDER}" ]; then
			${MKDIR} "${DESTINATION_FOLDER}"
		fi

		if [ -f "${FILE}" ]; then
			FILENAME_EXTENSION=`echo ${FILE} | awk -F '.' '{print $NF}'`

		#	if [ "${FILENAME_EXTENSION}" = "php" ]; then
				IS_COMPILED=`cat ${FILE} | grep "requires the ionCube PHP Loader\|extension_loaded('ionCube Loader'))\|function_exists('_il_exec'))\|<?php @Zend;\|^Zend\|!extension_loaded('Php Express')\|NuSphere PHP Express"`

				if [ "${IS_COMPILED}" ]; then
					echo -e "# Command ./${PHP_COMMAND} \"${FILE}\" > \"${DESTINATION}\""
					./${PHP_COMMAND} "${FILE}" /path:${DESTINATION_FOLDER} /ext:.${FILENAME_EXTENSION}

					if [ -f "${BUG_OUTPUT_FILE}" ]; then
						if [ -f "${DESTINATION}" ]; then
							rm -f "${DESTINATION}"
						fi

						mv "${BUG_OUTPUT_FILE}" "${DESTINATION}"
					fi

					echo -e "${DESTINATION}\r" >> "${DECODED_FOLDER}/${LOG_FILE}"
					IS_DECODED=1
					NUMBER_DECODED_FILES=$((${NUMBER_DECODED_FILES} + 1))
				fi
		#	fi
		fi

		if [ -f "${FILE}" ] && [ "${IS_DECODED}" = "0" ]; then
			cp -f "${FILE}" "${DESTINATION}"
		fi
	}
	done

	echo -e "\n" >> "${DECODED_FOLDER}/${LOG_FILE}"
	echo -e "Total Decoded: \"${NUMBER_DECODED_FILES}\"" >> "${DECODED_FOLDER}/${LOG_FILE}"
fi

exit 0;