#!/bin/bash
#
# __copy1__
# __copy2__
#
#
CMD=$(basename $0)
CMDVER="0.2"
CMDSIG="$CMD v.$CMDVER (2016/11)"

tmp=$(mktemp /tmp/$CMD-XXXXXXXX)

set -e -u

cleanup()
{
	rm -f $tmp
}



# (MAIN)

trap 'cleanup' EXIT
trap 'echo -e "\n*INTR*\n"; cleanup; exit 255"' 1 2 3

systemid=$(uname -n | sed -e 's/\..*//')



# lsb infos
#
DISTRIB_ID=
DISTRIB_RELEASE=
DISTRIB_CODENAME=
DISTRIB_DESCRIPTION=
if [ -f /etc/lsb-release ]
then
	. /etc/lsb-release
else
	if [ "$(which kusa-setdistro)" != "" ]
	then
		eval $(kusa-setdistro)
	fi
fi

# kusa infos
#
ku_updated=
ku_svclist=
[ "$(which kusa-conf)" != "" ] && {
	ku_updated=$(grep 'system.*updated' /etc/motd.tail | sed -e 's/.* on //')
	ku_svclist=$(kusa-conf common.svclist 2>/dev/null)
}

# hardware infos
#
hw_ram=$(head -1 /proc/meminfo | sed -e 's/ kB//' -e 's/.* //')

hw_disks=
hw_dsizes=
sfdisk -s >$tmp
exec 9<&0 <$tmp
while read disk size
do
	case "$disk" in
	  /dev/*)
	  	disk=$(echo $disk | sed -e 's#/dev/##' -e 's/://')
	  	hw_disks="$hw_disks $disk"
		hw_dsizes="$hw_dsizes $size"
	esac
done
exec 0<&9 9<&-

hw_parts=
hw_psize=
hw_pfree=
hw_pmount=

df >$tmp
exec 9<&0 <$tmp
while read fs size used free cap mount
do
	case $fs in
	 /dev/*)
	 	hw_parts="$hw_parts $fs"
	 	hw_psize="$hw_psize $size"
	 	hw_pfree="$hw_pfree $free"
	 	hw_pmount="$hw_pmount $mount"
	esac
done
exec 0<&9 9<&-


# network
#
set +e

trush=$(ifconfig eth0 | fgrep 'inet addr:')
eth0_ip=$(echo "$trush" | sed -e 's/.*inet addr://' -e 's/ .*//')
eth0_mask=$(echo "$trush" | sed -e 's/.*Mask://' -e 's/ .*//')
eth0_hwaddr=$(ifconfig eth0 | fgrep ' HWaddr ' | sed -e 's/.* HWaddr //' -e 's/ .*//')
eth1_ip= eth1_mask= eth1_hwaddr=
eth2_ip= eth2_mask= eth2_hwaddr=
eth3_ip= eth3_mask= eth3_hwaddr=

trush=$(ifconfig eth1 2>/dev/null| fgrep 'inet addr:')
[ "$trush" != "" ] && {
	eth1_ip=$(echo "$trush" | sed -e 's/.*inet addr://' -e 's/ .*//')
	eth1_mask=$(echo "$trush" | sed -e 's/.*Mask://' -e 's/ .*//')
	eth1_hwaddr=$(ifconfig eth1 | fgrep ' HWaddr ' | sed -e 's/.* HWaddr //' -e 's/ .*//')
}

trush=$(ifconfig eth2 2>/dev/null| fgrep 'inet addr:')
[ "$trush" != "" ] && {
	eth2_ip=$(echo "$trush" | sed -e 's/.*inet addr://' -e 's/ .*//')
	eth2_mask=$(echo "$trush" | sed -e 's/.*Mask://' -e 's/ .*//')
	eth2_hwaddr=$(ifconfig eth2 | fgrep ' HWaddr ' | sed -e 's/.* HWaddr //' -e 's/ .*//')
}

trush=$(ifconfig eth3 2>/dev/null| fgrep 'inet addr:')
[ "$trush" != "" ] && {
	eth3_ip=$(echo "$trush" | sed -e 's/.*inet addr://' -e 's/ .*//')
	eth3_mask=$(echo "$trush" | sed -e 's/.*Mask://' -e 's/ .*//')
	eth3_hwaddr=$(ifconfig eth3 | fgrep ' HWaddr ' | sed -e 's/.* HWaddr //' -e 's/ .*//')
}


# xen host infos
#
xen_running=
xen_config=
[ "$(which xm)" != "" ] && {
	xen_running=$(xm list | sed -e '1,/Domain-0/d' -e 's/ .*//')
	xen_config=$((cd /etc/xen ; ls *.cfg | sed -e 's/\.cfg//'))
}


echo "
[$systemid]
  name		$(uname -n)

  distro_id	$DISTRIB_ID
  distro_rel	$DISTRIB_RELEASE
  distro_code	$DISTRIB_CODENAME
  distro_desc	$DISTRIB_DESCRIPTION

  kernel_rel	$(uname -r)
  kernel_arch	$(uname -m)

  hw_ram	$hw_ram

  disks		$hw_disks
  disks_size	$hw_dsizes

  parts		$hw_parts
  parts_size	$hw_psize
  parts_free	$hw_pfree
  parts_mount	$hw_pmount

  ku_updated	$ku_updated

  xen_running	"$xen_running"
  xen_config	"$xen_config"

  eth0		$eth0_hwaddr $eth0_ip $eth0_mask
  eth1		$eth1_hwaddr $eth1_ip $eth1_mask
  eth2		$eth2_hwaddr $eth2_ip $eth2_mask
  eth3		$eth3_hwaddr $eth3_ip $eth3_mask
"

exit 0
