#!/bin/sh

SZ_CMD="ps dmesg df free ifconfig"
SZF_MSG="/var/log/messages* /var/log/*.result /var/log/synoupdate.log /var/log/memtester.log /var/run/dmesg.boot /etc.defaults/VERSION /usr/syno/etc/amule/amule.conf /usr/syno/etc/dms/menu_custom*.xml"

PROCFILES="/proc/usb/devices /proc/scsi/scsi /proc/partitions /proc/mdstat /proc/mtd /sys/class/scsi_host/host*/syno_pm_info /proc/net/dev /proc/net/route /proc/mounts /proc/meminfo /proc/swaps /proc/vmstat /proc/cpuinfo /proc/interrupts /proc/uptime /proc/cmdline"

DIRLIST="/etc /usr/syno/etc /tmp/space"
RemoveDirs="/usr/syno/etc/amule /usr/syno/etc/dms"
RemoveFiles="/usr/syno/etc/usb.map"

if [ -z "$1" ]; then
	TAR_FILE="/tmp/msg.tar.gz"
else
	TAR_FILE="$1"
fi
SZD_TMP="`dirname ${TAR_FILE}`/@`date +%s`"

IsMvSocDriver()
{
	if [ -z "$1" ]; then
		return 255
	fi
	KernelVersion=`/bin/uname -r | cut -d'.' -f-2`
	if [ "x$KernelVersion" = "x2.4" ]; then
		return 0
	fi
	Chip=`/bin/cat /sys/block/$1/device/../../scsi*/proc_name`
	if [ "x$Chip" = "xmvSata" ]; then
		return 1
	fi
	return 0
}

/bin/rm -f /var/log/*.result

# Runtime commands
for cmd in $SZ_CMD; do
	`/usr/bin/which $cmd` > /var/log/$cmd.result
done

mv /etc/mtab /etc/mtab.$$
ln -s /proc/mounts /etc/mtab
/bin/df > /var/log/df2.result
rm /etc/mtab
mv /etc/mtab.$$ /etc/mtab

/bin/echo q | /usr/bin/top  > /var/log/top.result

if [ -x /usr/syno/bin/spacetool ]; then
	/usr/syno/bin/spacetool --synoblock-enum > /var/log/synoblock_enum.result
fi

SZF_RAID_RESULT="/var/log/raid_superblock_enum.result"
SZF_SFDISK_RESULT="/var/log/sfdisk_enum.result"
for i in `cat /proc/partitions | awk '{print $4}'`;
do 
	if [ ! -e /dev/$i ]; then 
		continue;
	fi
	if [ -x /sbin/mdadm ]; then
		echo "$i:" >> $SZF_RAID_RESULT
		/sbin/mdadm -E -b /dev/$i >> $SZF_RAID_RESULT
		echo "" >> $SZF_RAID_RESULT
	fi
	# seperate sdXXyy to D=sdXX N=yy
	D=""
	N=""
	substr=`echo $i | cut -c1-2`
	if [ "$substr" == "hd" ]; then
	    `echo $i | sed -n 's/hd\([a-z]*\)\([0-9]*\)/eval D=hd\1; eval N=\2;/p'`
	elif [ "$substr" == "sd" ]; then
	    `echo $i | sed -n 's/sd\([a-z]*\)\([0-9]*\)/eval D=sd\1; eval N=\2;/p'`
	fi 

	if [ ! -z $N ]; then
		# Partition Info for sdXXyy
		/sbin/sfdisk -l -uS -N$N /dev/$D >> $SZF_SFDISK_RESULT
		if [ $? -ne 0 ]; then
			echo "/dev/$D$N error" >> $SZF_SFDISK_RESULT
		fi
	elif [ ! -z $D ]; then
		# SMART test for sdXX
		IsMvSocDriver $D
		ret=$?
		if [ $ret -eq 0 ]; then
			/usr/syno/bin/smartctl -a -d ata /dev/$D > /var/log/smart_$D.result
		elif [ $ret -eq 1 ]; then
			/usr/syno/bin/smartctl -a -d marvell /dev/$D > /var/log/smart_$D.result
		fi
	fi
done

/bin/mkdir -p $SZD_TMP

# Copy and Remove Dirs and Files in $SZD_TMP
for d in $DIRLIST; do
	/bin/mkdir -p ${SZD_TMP}/$d
	/bin/cp -a $d/* ${SZD_TMP}/$d/
done
for r in $RemoveDirs $RemoveFiles; do
	/bin/rm -rf ${SZD_TMP}/$r
done
# Copy Proc files into $SZD_TMP
for f in $PROCFILES; do
	/bin/mkdir -p ${SZD_TMP}/`dirname $f`
	/bin/cp -f $f ${SZD_TMP}/$f
done

cd $SZD_TMP
/bin/rm -f $TAR_FILE
/bin/tar czf $TAR_FILE $SZF_MSG *
cd -
/bin/rm -rf $SZD_TMP /var/log/*.result

