#!/bin/sh

. /usr/local/bin/telem/functions

Title(){
    echo "============ $1 ============"
}

logg "Starting"

telem_logs=/var/local/telem/log

_hostname=`hostname`

packlogs_dir=/tmp/packlogs
tmp_dir=$packlogs_dir/$_hostname

# Clean-up old
! rm -r $tmp_dir
! rm /var/log/telem_gw.tar
! rm /var/log/telem_gw.tar.gz
! rm /var/local/telem/done_pack_logs

! rm -r $tmp_dir


mkdir -p $tmp_dir

# version info
Title `cat /usr/local/etc/telem/product` > $tmp_dir/version-info
[ -f /usr/local/etc/telem/product_id ] && {
    Title "Device ID" >> $tmp_dir/version-info
    cat /usr/local/etc/telem/product_id >> $tmp_dir/version-info
}

Title "/VERSION" >> $tmp_dir/version-info
cat /VERSION >> $tmp_dir/version-info

Title "/PACKED" >> $tmp_dir/version-info
cat /PACKED >> $tmp_dir/version-info

Title "uname -a" >> $tmp_dir/version-info
uname -a >> $tmp_dir/version-info

Title "cat /proc/cpuinfo" >> $tmp_dir/version-info
cat /proc/cpuinfo >> $tmp_dir/version-info

! cp -v /usr/local/etc/telem/order_code $tmp_dir/

# filesystems
Title "Filesystem usage (df -h)" > $tmp_dir/disk-info
df -h >> $tmp_dir/disk-info

# memory info
Title "Memory usage (cat /proc/meminfo)" > $tmp_dir/mem-info
cat /proc/meminfo >> $tmp_dir/mem-info

uptime > $tmp_dir/uptime

# telem applicaiton logs
# Packing is not guaranteed, because file names may be changed from /usr/local/etc/telem/log-conf.xml
mkdir $tmp_dir/gw
cp -v $telem_logs/console.log $tmp_dir/gw/console.log
[ -f $telem_logs/console.1.log ] && cp -v $telem_logs/console.1.log $tmp_dir/gw/console.1.log

ver=$(awk '{print $2}' /VERSION)
[ "x${ver}" == "xVMX25" ] || {
    [ -f $telem_logs/console.2.log ] && cp -v $telem_logs/console.2.log $tmp_dir/gw/console.2.log
    [ -f $telem_logs/console.3.log ] && cp -v $telem_logs/console.3.log $tmp_dir/gw/console.3.log
}

cp -v $telem_logs/error.log $tmp_dir/gw/error.log
[ -f $telem_logs/error.1.log ] && cp -v $telem_logs/error.1.log $tmp_dir/gw/error.1.log
cp -v $telem_logs/events.csv $tmp_dir/gw/events.csv
[ -f $telem_logs/events.1.csv ] && cp -v $telem_logs/events.1.csv $tmp_dir/gw/events.1.csv
cp -v $telem_logs/gps.log $tmp_dir/gw/gps.log
[ -f $telem_logs/gps.1.log ] && cp -v $telem_logs/gps.1.log $tmp_dir/gw/gps.1.log
cp -v $telem_logs/check_usb.log $tmp_dir/gw/check_usb.log

[ -f $telem_logs/eeprom ] && cp -v $telem_logs/eeprom $tmp_dir/gw/eeprom

# system logs
cp -r /var/log/ $tmp_dir/system

cp $telem_logs/messages* $tmp_dir/system

[ -f $telem_logs/modem_st.csv   ] && cp -v $telem_logs/modem_st.csv   $tmp_dir/system/modem_st.csv
[ -f $telem_logs/modem_st.csv.0 ] && cp -v $telem_logs/modem_st.csv.0 $tmp_dir/system/modem_st.csv.0

! cp -f /var/local/telem/stats/watchdog-boot-message $tmp_dir/system

! cp -f $telem_logs/telem-gw-sigsegv $tmp_dir/telem-gw-sigsegv
! cp -f $telem_logs/telem-gw-stderr $tmp_dir/telem-gw-stderr

# networking
Title "iptables raw" > $tmp_dir/system/iptables.txt
/usr/sbin/iptables -L -n -v -t raw >> $tmp_dir/system/iptables.txt
Title "iptables filter" >> $tmp_dir/system/iptables.txt
/usr/sbin/iptables -L -n -v -t filter >> $tmp_dir/system/iptables.txt
Title "iptables nat" >> $tmp_dir/system/iptables.txt
/usr/sbin/iptables -L -n -v -t nat >> $tmp_dir/system/iptables.txt
Title "iptables mangle" >> $tmp_dir/system/iptables.txt
/usr/sbin/iptables -L -n -v -t mangle >> $tmp_dir/system/iptables.txt

Title "conntrack -L" > $tmp_dir/system/conntrack.txt
/usr/sbin/conntrack -L >> $tmp_dir/system/conntrack.txt

Title "netstat -r" > $tmp_dir/system/netstat.txt
/bin/netstat -r >> $tmp_dir/system/netstat.txt
Title "netstat" >> $tmp_dir/system/netstat.txt
/bin/netstat -lnap >> $tmp_dir/system/netstat.txt

Title "netstat-nat" > $tmp_dir/system/netstat-nat.txt
/usr/bin/netstat-nat -nNxS >> $tmp_dir/system/netstat-nat.txt
/usr/bin/netstat-nat -nNxD >> $tmp_dir/system/netstat-nat.txt
/usr/bin/netstat-nat -nNxL >> $tmp_dir/system/netstat-nat.txt

Title "ifconfig" > $tmp_dir/system/interfaces.txt
/sbin/ifconfig >> $tmp_dir/system/interfaces.txt

Title "ip addr" > $tmp_dir/system/ip.txt
/sbin/ip addr >> $tmp_dir/system/ip.txt

Title "bridge" > $tmp_dir/system/bridges.txt
/usr/sbin/brctl show >> $tmp_dir/system/bridges.txt
# STP info for bridges with STP enabled
/usr/sbin/brctl show | awk {' if($3 == "yes") system("/usr/sbin/brctl showstp " $1) }' >> $tmp_dir/system/bridges.txt
# MAC address for all
/usr/sbin/brctl show | awk {' if($3 == "yes") system("/bin/echo " $1 "; /usr/sbin/brctl showmacs " $1) }' >> $tmp_dir/system/bridges.txt
/usr/sbin/brctl show | awk {' if($3 == "no") system("/bin/echo " $1 "; /usr/sbin/brctl showmacs " $1) }' >> $tmp_dir/system/bridges.txt

# Time

Title "ntpq -p -n ..." > $tmp_dir/system/ntp.txt
/usr/bin/ntpq -n -p -c associations -c rv >> $tmp_dir/system/ntp.txt
Title "ntpdc -n -c monlist 127.0.0.1" >> $tmp_dir/system/ntp.txt
/usr/bin/ntpdc -n -c monlist 127.0.0.1 >> $tmp_dir/system/ntp.txt

Title "ntp.drift" >> $tmp_dir/system/ntp.txt
cat /etc/ntp.drift >> $tmp_dir/system/ntp.txt

# Processes
Title "ps aux" > $tmp_dir/system/ps.txt
/bin/ps aux >> $tmp_dir/system/ps.txt

# crypto/VPN
Title "ISAKMP(Phase 1)" > $tmp_dir/system/ipsec.txt
/usr/sbin/racoonctl -l show-sa isakmp >> $tmp_dir/system/ipsec.txt
Title "IPSec(Phase 2)" >> $tmp_dir/system/ipsec.txt
/usr/sbin/racoonctl -l show-sa ipsec >> $tmp_dir/system/ipsec.txt


Title "last users to login" > $tmp_dir/system/last.txt
last >> $tmp_dir/system/last.txt

# Remove stuff we dont want
rm $tmp_dir/system/utmp
rm $tmp_dir/system/wtmp
rm $tmp_dir/system/sshd
rm -r $tmp_dir/system/*.pid
rm -r $tmp_dir/system/*.tdb

rm -f $tmp_dir/system/telem

# Start backing and compressing
/usr/bin/tar -C $packlogs_dir -czf /var/log/telem_gw.tar.gz $_hostname

if [ $? = "0" ]; then
  # Ok, signal for gws.exe
  logg "Finished OK"
  
  touch /var/local/telem/done_pack_logs
  chmod a+rw /var/local/telem/done_pack_logs
else
  # On error remove *.tar and *.tar.gz
  # gws.exe needs timeout, as done_* file nor the *.gz is not generated
  rm /var/log/telem_gw.tar.gz
  
  logg "Finished ERROR"
fi

# Clean-up
rm -r $tmp_dir
