#!/bin/bash
{
	#////////////////////////////////////
	# DietPi-logclear Script
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	# Info:
	# Clears logs in $FILEPATH_LOGFOLDER, with backup option.
	#
	# usage:
	# - dietpi-logclear 0		| Update current log files data to "$FILEPATH_BACKUPFOLDER/*. Then clear contents.
	# - dietpi-logclear 1		| Clear contents of all logs in $FILEPATH_LOGFOLDER.
	# - dietpi-logclear 2		| Delete all logs in $FILEPATH_LOGFOLDER and backups.
	#////////////////////////////////////

	INPUT=-1
	if [[ $1 =~ ^-?[0-9]+$ ]]; then

		INPUT=$1

	fi

	#Import DietPi-Globals ---------------------------------------------------------------
	. /DietPi/dietpi/func/dietpi-globals
	G_CHECK_ROOT_USER
	export G_PROGRAM_NAME='DietPi-Logclear'
	#Import DietPi-Globals ---------------------------------------------------------------

	#////////////////////////////////////////////////////////////////
	# Global
	#////////////////////////////////////////////////////////////////
	TEMP_FILE="/tmp/dietpi-logclear"

	FILEPATH_LOGFOLDER="/var/log"
	FILEPATH_BACKUPFOLDER="$HOME/logfile_storage"

	FILE_NAME=""
	#0=text log, 1=compressed
	FILE_TYPE=0
	FILESIZE_BYTES=0
	PROCESS_FILE=0

	#////////////////////////////////////////////////////////////////
	#Excluded log files (user created)
	#////////////////////////////////////////////////////////////////
	FP_EXCLUDED_LOGFILES="/DietPi/dietpi/.dietpi-logclear_exclude"
	INFO_EXCLUDED_LOGFILES=0

	#////////////////////////////////////////////////////////////////
	# Info Printouts
	#////////////////////////////////////////////////////////////////
	INFO_SIZE_CLEARED=0
	INFO_FILES_PROCESSED=0
	INFO_LOGS_DELETED=0
	INFO_LOGS_CLEARED=0
	INFO_LOGS_NOTSUPPORTED=0
	INFO_BACKUPS_MADE=0

	#////////////////////////////////////////////////////////////////
	# Process log files.
	#////////////////////////////////////////////////////////////////
	Process_Logfiles(){

		#-----------------------------------------------------------------------------------
		#Find existing logs and generate a filepath list.
		find "$FILEPATH_LOGFOLDER" -type f > "$TEMP_FILE"

		#Read logfile filepath list into array.
		readarray -t ARRAY_LOG_FILEPATH < "$TEMP_FILE"

		#-----------------------------------------------------------------------------------
		#Process Logfiles
		for ((i=0; i<${#ARRAY_LOG_FILEPATH[@]}; i++))
		do

			#File details
			FILE_NAME=$(echo -e ${ARRAY_LOG_FILEPATH[$i]} | sed 's/\/var\/log\///g')
			FILESIZE_BYTES=$(stat -c%s "${ARRAY_LOG_FILEPATH[$i]}")
			PROCESS_FILE=1
			FILE_TYPE=0

			#Special Filetypes
			#PiHole logs (contains dns stats) | FILE_TYPE 2
			if [ "${ARRAY_LOG_FILEPATH[$i]}" = "/var/log/pihole.log" ]; then

				FILE_TYPE=2

			#Compessed files (zip etc) | FILE_TYPE 1
			elif [[ ${FILE_NAME: -4} == ".zip" ]] ||
				[[ ${FILE_NAME: -3} == ".gz" ]]; then

				FILE_TYPE=1

			#Normal Log Files. Do we have any data to clear? Check size of file.
			elif (( $FILESIZE_BYTES <= 10 )); then

				PROCESS_FILE=0

			fi

			#Process File
			if (( $PROCESS_FILE == 1 )); then

				#PiHole
				if (( $FILE_TYPE == 2 )); then

					local pihole_restart_required=0

					#Find all non-matching lines with todays date
					#	 NB: day is space padded without trailling 0 (eg: Sep  1, Sep 11)
					#	 NB: Force english dates to match english dates that dnsmasq always logs with: https://github.com/Fourdee/DietPi/issues/233#issuecomment-196407135
					local month_day_today=$(LC_ALL=en_GB.UTF-8 date +'%b %e')

					# - Check for at least 1 non-matching line before applying. Remove all lines in logfile for all other days, excluding today.
					if (( $(grep -v -ci -m1 "$month_day_today" /var/log/pihole.log) == 1 )); then

						#	RAMlog mode 2:
						if (( $INPUT == 0 )); then

							cat "${ARRAY_LOG_FILEPATH[$i]}" | sed '/'"$month_day_today"'/d' >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME"
							((INFO_BACKUPS_MADE++))

						fi

						G_DIETPI-NOTIFY 2 'Clearing log entries for PiHole, that are not from today, please wait...'
						sed -i '/'"$month_day_today"'/!d' /var/log/pihole.log
						pihole_restart_required=1

					fi

					#	recheck size again
					FILESIZE_BYTES=$(stat -c%s "${ARRAY_LOG_FILEPATH[$i]}")

					#Clear all entries if over 5MB (we have to limit and prevent /var/log hitting the 20mb tmpfs limit)
					# - 5mb should be more than enough for 1+ days of pihole logs
					if (( $FILESIZE_BYTES >= 5242880 )); then

						G_DIETPI-NOTIFY 2 "${ARRAY_LOG_FILEPATH[$i]} has exceeded 5MB, clearing, please wait..."

						#	RAMlog mode 2:
						if (( $INPUT == 0 )); then

							cat "${ARRAY_LOG_FILEPATH[$i]}" >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME"
							((INFO_BACKUPS_MADE++))

						fi

						echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}"
						((INFO_LOGS_CLEARED++))

						pihole_restart_required=1

					#Prevent size effecting results
					else

						FILESIZE_BYTES=0

					fi

					#restart PiHole if we modified the log file
					if (( $pihole_restart_required )); then

						G_DIETPI-NOTIFY 2 'PiHole logs have been changed, restarting, please wait...'
						systemctl stop pihole-FTL
						systemctl stop dnsmasq
						sleep 1
						systemctl start dnsmasq
						systemctl start pihole-FTL

						#	Reapply DietPi-Process_Tool
						/DietPi/dietpi/dietpi-process_tool 1

					fi

				#Delete all compressed filetypes.
				elif (( $FILE_TYPE == 1 )); then

					rm "${ARRAY_LOG_FILEPATH[$i]}"
					((INFO_LOGS_DELETED++))
					((INFO_LOGS_NOTSUPPORTED++))

				#Update current log files data to $FILEPATH_BACKUPFOLDER/*. Then clear.
				elif (( $INPUT == 0 )); then

					#Generate filepaths
					if [ ! -f "$FILEPATH_BACKUPFOLDER/$FILE_NAME" ]; then

						#This is a little "hack" to automatically generate the required subdirectories.
						# EG: /this/is/my/logfile.txt | will create the folders /this/is/my
						mkdir -p "$FILEPATH_BACKUPFOLDER/$FILE_NAME"
						rm -R "$FILEPATH_BACKUPFOLDER/$FILE_NAME"

					fi

					#Write current logfile contents to existing.
					cat "${ARRAY_LOG_FILEPATH[$i]}" >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME"
					((INFO_BACKUPS_MADE++))

					#Clear logfile contents
					echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}"
					((INFO_LOGS_CLEARED++))

				#Clear logfile contents
				elif (( $INPUT == 1 )); then

					echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}"
					((INFO_LOGS_CLEARED++))

				#Hard delete log files
				elif (( $INPUT == 2 )); then

					rm "${ARRAY_LOG_FILEPATH[$i]}"
					((INFO_LOGS_DELETED++))

				fi

				#Update Size cleared.
				INFO_SIZE_CLEARED=$(($INFO_SIZE_CLEARED + $FILESIZE_BYTES))

			fi

			((INFO_FILES_PROCESSED++))

		done

		#Remove temp files
		rm "$TEMP_FILE" &> /dev/null

		#delete[] array
		unset ARRAY_LOG_FILEPATH

	}

	#////////////////////////////////////////////////////////////////
	# Main Loop
	#////////////////////////////////////////////////////////////////

	#----------------------------------------------------------------
	G_DIETPI-NOTIFY 3 DietPi-Logclear "Welcome"
	#----------------------------------------------------------------
	#How to use print.
	if (( $INPUT == -1 )); then

		G_DIETPI-NOTIFY 2 'Available commands:'
		echo -e ""
		echo -e "\e[1m dietpi-logclear 0\e[0m"
		echo -e "\e[38;5;244m Backup contents of all log files from $FILEPATH_LOGFOLDER to $FILEPATH_BACKUPFOLDER/*.\n Also clears the contents of all logs files in $FILEPATH_LOGFOLDER.\e[0m"
		echo -e ""
		echo -e "\e[1m dietpi-logclear 1\e[0m"
		echo -e "\e[38;5;244m Clear contents of all logs files in $FILEPATH_LOGFOLDER.\e[0m"
		echo -e ""
		echo -e "\e[1m dietpi-logclear 2\e[0m"
		echo -e "\e[38;5;244m Physically delete all files in $FILEPATH_LOGFOLDER and backups in $FILEPATH_BACKUPFOLDER/*.\n May prevent log files from being updated, restart services or reboot. \e[0m"
		echo -e ""

	#----------------------------------------------------------------
	#Process log files
	elif (( $INPUT >= 0 )); then

		Process_Logfiles

		#Reset permissions for log files/dir
		chmod -R 775 /var/log

		#Delete logfile backups
		if (( $INPUT == 2 )); then

			rm -R "$FILEPATH_BACKUPFOLDER" &> /dev/null

		fi

		#Print Info
		G_DIETPI-NOTIFY 2
		echo -e " - Log file directory      \e[90m|\e[0m $FILEPATH_LOGFOLDER"
		echo -e " - Processed files         \e[90m|\e[0m $INFO_FILES_PROCESSED"

		#Excluded
		echo -e " - Excluded files          \e[90m|\e[0m $INFO_EXCLUDED_LOGFILES"

		#Cleared
		echo -e " - Cleared log files       \e[90m|\e[0m $INFO_LOGS_CLEARED"

		#Deleted
		echo -e " - Unsupported files       \e[90m|\e[0m $INFO_LOGS_NOTSUPPORTED"
		echo -e " - Deleted files           \e[90m|\e[0m $INFO_LOGS_DELETED"

		#convert size to kb
		#/= (i miss it)
		INFO_SIZE_CLEARED=$(($INFO_SIZE_CLEARED / 1024))
		echo -e " - Space cleared           \e[90m|\e[0m $INFO_SIZE_CLEARED KB"

		#Backups
		if (( $INPUT == 0 )); then

			echo -e ""
			echo -e " \e[38;5;244mBackup Info:\e[0m"
			echo -e " - Backup directory        \e[90m|\e[0m $FILEPATH_BACKUPFOLDER"
			echo -e " - Updated log files       \e[90m|\e[0m $INFO_BACKUPS_MADE"

		fi

		echo -e ""

	fi

	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}