#!/bin/sh

mkdir /tmp/report

cp /etc/openwrt_release /tmp/report/openwrt_release.txt

if [[ "$(uci -q get report.@report[0].kernel)" -eq "1" ]]; then
	dmesg &> /tmp/report/dmesg.txt
fi

if [[ "$(uci -q get report.@report[0].system)" -eq "1" ]]; then
	logread &> /tmp/report/logread.txt
	log_file=$(uci -q get system.@system[0].log_file)
	if [[ -n "$log_file" ]]; then
		cp ${log_file} /tmp/report/logfile.txt
		cp ${log_file}.old /tmp/report/logfile.old.txt
	fi
fi

if [[ "$(uci -q get report.@report[0].network)" -eq "1" ]]; then
	cp /etc/config/network /tmp/report/network.txt
	cp /etc/config/firewall /tmp/report/firewall.txt
	cp /etc/config/wireless /tmp/report/wireless.txt
	route -n &> /tmp/report/route.txt
	ifconfig &> /tmp/report/ifconfig.txt
	iptables -S &> /tmp/report/iptables.txt
fi

if [[ "$(uci -q get report.@report[0].simman)" -eq "1" ]]; then
	cp /etc/config/simman2 /tmp/report/simman2.txt
	simman2_cli -a core &> /tmp/report/simman2_cli.txt
fi

if [[ "$(uci -q get report.@report[0].openvpn)" -eq "1" ]]; then
	cp /etc/config/openvpn /tmp/report/openvpn.txt
fi

if [[ "$(uci -q get report.@report[0].mwan)" -eq "1" ]]; then
	cp /etc/config/mwan3 /tmp/report/mwan.txt
fi

if [[ "$(uci -q get report.@report[0].pollmydevice)" -eq "1" ]]; then
	cp /etc/config/pollmydevice /tmp/report/pollmydevice.txt
fi

if [[ "$(uci -q get report.@report[0].ntp)" -eq "1" ]]; then
	ntpq -p &> /tmp/report/ntpqp.txt
	cp /etc/ntp.conf /tmp/report/ntp.txt
fi

if [[ "$(uci -q get report.@report[0].smsd)" -eq "1" ]]; then
	cp /etc/config/smscontrol /tmp/report/smscontrol.txt
	cp /etc/smsd.conf /tmp/report/smsd.txt
	cp /var/log/smsd.log /tmp/report/smsdlog.txt
fi

if [[ "$(uci -q get report.@report[0].snmp)" -eq "1" ]]; then
	cp /etc/config/snmpd /tmp/report/snmpd.txt
fi

cd /tmp/ && tar -czf /tmp/report.tar.gz report 2>/dev/null

rm -rf /tmp/report/

exit 0