#!/bin/bash

fspacing="        "
findent="  - "
for ftgz in linux/cacti-1*.tar.gz; do

	ftar=${ftgz/.gz/}
	fzip=${ftgz/.tar.gz/.zip}
	if [ -z "$fzip" ]; then
		fzip=${ftgz/.tar/.zip}
	fi
	
	fversion=${ftgz/.tar.gz/}
	fversion=${fversion/.tar/}
	flst="${fversion}"

	fversion=${fversion/linux\/cacti-/}
	
	fdir=cacti-$fversion
	fcfg=$fdir/include/config.php
	fdst=$fcfg.dist
	fversion="v${fversion}"
	
	echo "Version $fversion - GZ  - $ftgz"
	echo "        $fversion - TAR - $ftar"
	echo "        $fversion - ZIP - $fzip"
	echo "        $fversion - DIR - $fdir"
	echo "        $fversion - CFG - $fcfg"
	echo "        $fversion - DST - $fdst"
	echo
	
	if [ ! -f "$fzip" ]; then
		echo "${fspacing}ERROR: $fversion - Command failed -  to find valid zip file - $fzip"
		continue
	fi

	if [ ! -f "$ftgz" ]; then
		echo "${fspacing}ERROR: $fversion - Command failed -  to find valid gzip file - $ftgz"
		echo Failed to find valid zip file for $ftgz
		continue
	fi

#################################### TAR/GZIP FILE ######################################
	
	fpadding="${fspacing}[ GZ] "
	echo "${fpadding}Unpacking gzip file"
	gzip -f -d $ftgz | sed "s/^/$fpadding$findex/"
	
	if [ -f $ftar ]; then
		echo "${fpadding}Checking for config.php"
		output=$(tar tvf $ftar $fcfg 2>&1);
		result=$?
	
		if [[ $result -eq 0 ]]; then
			if [ ! -f $fcfg ]; then
				echo "${fpadding}${findent}Extracting config.php files"
				output=$(tar -xf $ftar $fcfg 2>&1)
				result=$?
				if [[ $result -ne 0 ]]; then
					echo $output | sed "s/^/${fpadding}${findent}/"
					echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
					exit $result;
				fi
			fi
	
			if [ -f $fcfg ]; then
				echo "${fpadding}${findent}Moving config.php to config.php.dist"
				output=$(mv $fcfg $fdst 2>&1)
				result=$?
				if [[ $result -ne 0 ]]; then
					echo $output | sed "s/^/${fpadding}${findent}/"
					echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
					exit $result;
				fi
			fi
	
			if [ ! -f $fdst ]; then
				echo "${fpadding}${findent}ERROR: $fversion - Config.php.dist is missing"
				exit 1;
			fi
		fi
	
		echo "${fpadding}Checking for config.php.dist"
		output=$(tar tvf $ftar $fdst 1>/dev/null 2>&1)
		result=$?
	
		if [[ $result -gt 0 ]]; then
			echo "${fpadding}${findent}Adding missing config.php.dist"
			output=$(tar -f $ftar -r $fdst 2>&1)
			result=$?
			if [[ $result -ne 0 ]]; then
				echo $output | sed "s/^/${fpadding}${findent}/"
				echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
				exit $result;
			fi
		fi
	
	
		echo "${fpadding}Verifying config.php does not exist"
		output=$(tar tvf $ftar $fcfg 1>/dev/null 2>&1)
		result=$?
	
		if [[ $result -eq 0 ]]; then
			echo "${fpadding}${findent}Removing config.php"
			output=$(tar -f $ftar --delete $fcfg 2>&1)
			result=$?
			if [[ $result -ne 0 ]]; then
				echo $output | sed "s/^/${fpadding}${findent}/"
				echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
				exit $result;
			fi
		fi
	
		echo "${fpadding}Verifying config.php.dist exists"
		output=$(tar tvf $ftar $fdst 2>&1)
		result=$?
		if [[ $result -ne 0 ]]; then
			echo $output | sed "s/^/$fpadding/"
			echo "${fpadding}ERROR: $fversion - Command failed - $result"
			exit $result;
		fi

		echo "${fpadding}Repacking archive"
		echo "${fpadding}${findent}Compressing archive"
		output=$(gzip -9 $ftar 2>&1)
		result=$?
		if [[ $result -ne 0 ]]; then
			echo $output | sed "s/^/${fpadding}${findent}/"
			echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
			exit $result;
		fi
	
		echo "${fpadding}${findent}Ensuring original date"
		output=$(touch -r $fzip $ftgz)
		result=$?
		if [[ $result -ne 0 ]]; then
			echo $output | sed "s/^/${fpadding}${findent}/"
			echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
			exit $result;
		fi
	
		if [ -n "$fdir" ]; then
			if [ -d "$fdir" ]; then
				echo "${fpadding}${findent}Removing temporary directory"
				output=$(rm -r $fdir/ 2>&1)
				result=$?
				if [[ $result -ne 0 ]]; then
					echo $output | sed "s/^/${fpadding}${findent}/"
					echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
					exit $result;
				fi
			fi
		fi

		echo "${fpadding}Listing versions"
		echo
		ls -al $flst.* | sed "s/^/$fpadding/"
	fi
		
	echo

#################################### ZIP FILE ######################################

	fpadding="${fspacing}[ZIP] "
	echo "${fpadding}Checking for config.php"
	output=$(unzip -l $fzip $fcfg 2>&1 | grep -q $fcfg)
	result=$?

	if [[ $result -eq 0 ]]; then
		if [ ! -f $fcfg ]; then
			echo "${fpadding}${findent}Extracting config.php files"
			output=$(unzip $fzip $fcfg 2>&1)
			result=$?
			if [[ $result -ne 0 ]]; then
				echo $output | sed "s/^/${fpadding}${findent}/"
				echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
				exit $result;
			fi
		fi
	
		if [ -f $fcfg ]; then
			echo "${fpadding}${findent}Moving config.php to config.php.dist"
			output=$(mv $fcfg $fdst 2>&1)
			result=$?
			if [[ $result -ne 0 ]]; then
				echo $output | sed "s/^/${fpadding}${findent}/"
				echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
				exit $result;
			fi
		fi
	
		if [ ! -f $fdst ]; then
			echo "${fpadding}${findent}ERROR: $fversion - Config.php.dist is missing"
			exit 1;
		fi
	fi
	
	echo "${fpadding}Checking for config.php.dist"
	output=$(unzip -l $fzip $fdst 2>&1 | grep -q $fdst)
	result=$?
	
	if [[ $result -gt 0 ]]; then
		echo "${fpadding}${findent}Adding missing config.php.dist"
		output=$(zip -u $fzip $fdst 2>&1)
		result=$?
		if [[ $result -ne 0 ]]; then
			echo $output | sed "s/^/${fpadding}${findent}/"
			echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
			exit $result;
		fi
	fi
		
	echo "${fpadding}Verifying config.php does not exist"
	output=$(unzip -l $fzip $fcfg 2>&1 | grep -q $fcfg)
	result=$?
	
	if [[ $result -eq 0 ]]; then
		echo "${fpadding}${findent}Removing config.php"
		output=$(zip -d $fzip $fcfg 2>&1)
		result=$?
		if [[ $result -ne 0 ]]; then
			echo $output | sed "s/^/${fpadding}${findent}/"
			echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
			exit $result;
		fi
	fi
	
	echo "${fpadding}Verifying config.php.dist exists"
	output=$(unzip -l $fzip $fdst 2>&1)
	result=$?
	if [[ $result -ne 0 ]]; then
		echo $output | sed "s/^/$fpadding/"
		echo "${fpadding}ERROR: $fversion - Command failed - $result"
		exit $result;
	fi

	echo "${fpadding}Repacking archive"
	
	echo "${fpadding}${findent}Ensuring original date"
	output=$(touch -r $ftgz $fzip 2>&1)
	result=$?
	if [[ $result -ne 0 ]]; then
		echo $output | sed "s/^/${fpadding}${findent}/"
		echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
		exit $result;
	fi

	if [ -n "$fdir" ]; then
		if [ -d $fdir ]; then
			echo "${fpadding}${findent}Removing temporary directory"
			output=$(rm -r $fdir/)
			result=$?
			if [[ $result -ne 0 ]]; then
				echo $output | sed "s/^/${fpadding}${findent}/"
				echo "${fpadding}${findent}ERROR: $fversion - Command failed - $result"
				exit $result;
			fi
		fi
	fi

	echo "${fpadding}${findent}Listing versions"
	echo
	ls -al $flst.* | sed "s/^/$fpadding/"
		
	echo


done;
