#!/bin/sh

. /usr/html/axis-cgi/lib/functions.sh

trap 'rm -rf $tmpdir' EXIT
PRG=$(__whoami)

croak() {
	logger -t$PRG -pcrit "$*"
	__cgi_errhd 500 "Error while compiling Server Report: $*!"
	exit 1
}

generate_report() {

	[ $# -gt 0 ] && [ "$1" ] ||
		croak "Bad destination file '$1' argument!"

	/usr/sbin/gen_serverreport.sh >$1
}

construct_and_send_package() {
	local p mac filename fullname type

	[ $# -gt 2 ] || croak "Bad number of arguments!"

	[ "$1" ] && [ -d "$1" ] || croak "Bad directory '$1' argument!"

	p=Properties.System.SerialNumber
	mac=$(parhandclient --nocgi get $p - RAW) ||
		croak "Failed to get parameter $p!"

	filename=Axis_SR_$(date '+%Y%m%d_%H%M%S')_$mac

	cd $1

	case $2 in
		zip)
			fullname=$filename".zip"
			type="zip"
			shift 2
			zip -jq $fullname $* 1>&2 || croak "Failed to perform zip operation!"
			;
		tar)
			fullname=$filename".tar"
			type="x-tar"
			shift 2
			tar -cf $fullname $* || croak "Failed to perform tar operation!"
			;
		*)
			croak "$2 is not supported"
			;
	esac

	printf "Content-Type: application/$type\r
Content-Disposition: attachment; filename=%s\r
Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n\r\n" $fullname
	cat $fullname
}

generate_radar_files() {
	[ $# -eq 3 ] || croak "Bad number of arguments!"
	[ "$1" ] && [ "$2" ] && [ "$3" ] || croak "Bad arguments: blank!"

	local result= f dest

	for f in /tmp/radar-diagnostics/*; do
		[ -f $f ] || continue
		dest=$1/${f##*/}
		cp $f $dest || {
			echo "$PRG: Error copying radar diagnostic file $f!" >>$2
			continue
		}
		result="${result:+$result
}$dest"
	done

	eval $3=\$result
}

generate_files() {
	[ $# -eq 2 ] || croak "Bad number of arguments!"

	[ "$1" ] && [ "$2" ] || croak "Bad destination file argument(s)!"

	generate_report $1
	local dadlog=/usr/local/packages/dad/localdata/log.db
	[ -r $dadlog ] || dadlog=

	eval $2=\$dadlog
}

prepare_files() {
	[ $# -eq 1 ] && [ "$1" ] && [ -d "$1" ] ||
		croak "Bad directory '$1' argument!"

	for f in "/usr/local/syslog.complete" 	"/usr/local/syslog.startup" 	"/usr/local/messages" 	"/usr/local/dmesg.startup" 	"/usr/local/syslog.log*" 	"/mnt/flash/messages" 	"/var/log/*.log*" 	; do
		cp $f $1 2>/dev/null
	done
}

if [ $# -gt 0 ] && [ "$1" ]; then
	mode=$1
else
	mode=$(__qs_getparam mode) || :
fi

tmpcgidir=/var/cache/httpd/cgi
[ -d $tmpcgidir ] || mkdir -p $tmpcgidir || croak "Error creating '$tmpcgidir'!"

name=${PRG%.cgi}

tmpdir=$(mktemp -d "$tmpcgidir/${name}XXXXXX") ||
	croak "Error creating temporary directory!"

tmpfile=$tmpdir/${name}_cgi.txt
tmpimage=$tmpdir/${name}_image.jpg

case $mode in
	zip)
		generate_files $tmpfile tmpdadlog
		construct_and_send_package $tmpdir "zip" $tmpfile $tmpdadlog
		;
	zip_with_image)
		generate_files $tmpfile tmpdadlog
		/usr/sbin/jpeg_snapshot $tmpimage || {
			echo "$PRG: Error generating snapshot image for server report." >>$tmpfile
			tmpimage=
		}
		generate_radar_files $tmpdir $tmpfile radarfiles
		construct_and_send_package $tmpdir "zip" $tmpfile $tmpimage $tmpdadlog $radarfiles
		;
	tar_all)
		prepare_files $tmpdir
		construct_and_send_package $tmpdir "tar" "*"
		;
	text|'')
		__cgi_hdgen yes text/plain
		generate_report $tmpfile
		cat $tmpfile
		;
	*)
		croak "Invalid mode '$mode'!"
		;
esac