#!/bin/sh

#create group hardinfo2 if not exists
groupadd hardinfo2 2>/dev/null
addgroup hardinfo2 2>/dev/null

#Create runtime data directory
mkdir /run/hardinfo2 2>/dev/null
chmod 750 /run/hardinfo2

#----Allow users in hardinfo2 group to get these hardware info----
#iomem and ioport address - no need to show system/kernel addresses.
cat /proc/iomem |grep -v Kernel|grep -v System >/run/hardinfo2/iomem 2>/dev/null
cat /proc/ioports >/run/hardinfo2/ioports 2>/dev/null

#DMI memory array and DIMM memory information
dmidecode -t 16 >/run/hardinfo2/dmi_memarray 2>/dev/null
dmidecode -t 17 >/run/hardinfo2/dmi_memory 2>/dev/null

#Allow for single user systems to use user group (To avoid reboot)
UID_MIN=1000
if [ -e /etc/login.defs ] ; then
    UID_MIN=$(awk 'match($1,"^UID_MIN") {print $2}' /etc/login.defs)
fi
#SYSTYPE 0:Root, 1:SingleUser 2:MultiUser
SYSTYPE=$(awk -F: -v UID_MIN=$UID_MIN '$3 >= UID_MIN && !match($7,"false") && !match($7,"nologin") && !match($1,"nobody") { C++; } END { if(C+0 == 0) print 0; else if(C+0 == 1) print 1; else print 2 }' /etc/passwd)
#Find User ID, User Name, Group Name
UIDNO=$(awk -F: -v UID_MIN=$UID_MIN '$3 >= UID_MIN && !match($7,"false") && !match($7,"nologin") && !match($1,"nobody") { print $3;exit; }' /etc/passwd)
GIDNO=$(awk -F: -v UID_MIN=$UID_MIN '$3 >= UID_MIN && !match($7,"false") && !match($7,"nologin") && !match($1,"nobody") { print $4;exit; }' /etc/passwd)
UIDNAME=$(awk -F: -v UIDNO=$UIDNO '$3 == UIDNO { print $1 }' /etc/passwd)
GIDNAME=$(awk -F: -v GIDNO=$GIDNO '$3 == GIDNO { print $1 }' /etc/group)
if [ $SYSTYPE = "1" ] && [ ! -z "${UIDNAME}" ] && [ $GIDNAME = $UIDNAME ]; then
    echo "Single User System" >/run/hardinfo2/systype
    #set owner to single user group
    chown -R root:$GIDNAME /run/hardinfo2
else
    if [ $SYSTYPE = "0" ]; then
	echo "Root Only System" >/run/hardinfo2/systype
    else
	echo "Multi User System" >/run/hardinfo2/systype
    fi
    #set owner to hardinfo2 (multiuser)
    chown -R root:hardinfo2 /run/hardinfo2
fi
#set rights
chmod 640 /run/hardinfo2/*


#loading SPD modules
modprobe -q spd5118
modprobe -q ee1004
modprobe -q at24 || true
