#!/bin/bash
####################################################################
# Prey System Module Linux Functions - by Carlos Yaconi H.
# URL: http://preyproject.com
# License: GPLv3
####################################################################

# system_udi=$(hal-find-by-property --key info.product --string Computer)

#get_from_hal(){
#	/usr/bin/hal-get-property --udi $system_udi --key $1
#}

get_system_full_info(){
	system__full_info=$(lshw 2> /dev/null)
}

get_system_ids(){
	[ -z "$system__full_info" ] && get_system_full_info
	system__uuid=$(echo "$system__full_info" | head -10 | grep "uuid" | sed "s/.*uuid=\(.*\)/\1/")
	system__serial_number=$(echo "$system__full_info" | head -10 | grep serial | head -1 | awk -F ': ' '{print $2}')
}

get_mb_info(){
	system__mb_vendor=$(echo "$system__full_info" | grep Motherboard -A5 | grep vendor | awk -F ': ' '{print $2}')
	system__mb_serial=$(echo "$system__full_info" | grep Motherboard -A5 | grep serial | awk -F ': ' '{print $2}')
	system__mb_model=$(echo "$system__full_info" | grep Motherboard -A5 | grep product | awk -F ': ' '{print $2}')
	system__mb_version=$(echo "$system__full_info" | grep Motherboard -A5 | grep version | awk -F ': ' '{print $2}')
}

get_bios_info(){
	system__bios_vendor=$(echo "$system__full_info" | grep -i "\-firmware" -A4 | grep vendor | awk -F ': ' '{print $2}')
	system__bios_version=$(echo "$system__full_info" | grep -i "\-firmware" -A4 | grep version | awk -F ': ' '{print $2}')
}

get_cpu_info(){
	local cpu_info=$(cat /proc/cpuinfo)

	system__cpu_model=$(echo -e "$cpu_info" | grep "model name" | tail -1 | sed 's/.*: //')
	system__cpu_speed=$(echo -e "$cpu_info" | grep "cpu MHz" | tail -1 | sed 's/.*: //')
	system__cpu_cores=$(echo -e "$cpu_info" | grep "processor" | wc -l)
}

get_memory_info(){
	local mem_in_kb=$(cat /proc/meminfo | grep "MemTotal" | sed 's/.*:[[:space:]]*\(.*\) .*/\1/')

	system__ram_size=$(($mem_in_kb/1024))
	system__ram_modules=$(dmidecode --type 17 | grep Size | grep -v "No Module" | wc -l)
}

get_storage_devices(){

#	hwinfo --disk --cdrom 2> /dev/null | grep Class -A8 | while read line; do

#		key=$(echo "$line" | cut -d":" -f1)
#		val=$(echo "$line" | cut -d":" -f2)

#		echo "key: $key"
#		echo "val: $val"

#	done

	local disks_path="/dev/disk/by-id"
	local last_path=""

	for disk in $(stat -c "%n" ${disks_path}/* | grep -v "\-part[1-9]"); do

		local path=$(readlink -f "$disk")
		[ "$path" == "$last_path" ] && continue

		local name=$(basename "$path")
		store_storage_device_value "$name" 'name' "$name"

		local full_name=$(echo "$disk" | sed "s/.*\/\([^\/]*\)$/\1/g")
		local interface=$(echo "$full_name" | cut -d"-" -f1)
		store_storage_device_value "$name" 'interface' "$interface"

		# cdroms are owned by the group 'cdrom'
		if [ "$(stat -c "%G" "$path")" == "cdrom" ]; then
			store_storage_device_value "$name" 'storage_type' 'CDROM'
		elif [[ "$interface" =~ "usb" ]]; then
			store_storage_device_value "$name" 'storage_type' 'Removable'
		else
			store_storage_device_value "$name" 'storage_type' 'Fixed'
		fi

		local model=$(echo "$full_name" | sed "s/.*-\(.*\)_.*/\1/")
		store_storage_device_value "$name" 'model' "$model"

		local serial_number=$(echo "$full_name" | sed "s/.*_\(.*\)/\1/")
		store_storage_device_value "$name" 'serial_number' "$serial_number"

		last_path="$path"
	done

}


get_network_interfaces(){
	for device_name in /sys/class/net/* ; do
		[ -e $device_name/device ] && {

			local name=$(echo "$device_name" | sed "s/.*\/\(.*\)$/\1/")
			store_network_interface_value $name 'name' "$name"

			local interface_type="Wired"
			[ -n "$(cat /proc/net/wireless | grep "$name" 2> /dev/null)" ] && interface_type="Wireless"
			store_network_interface_value $name 'interface_type' "$interface_type"

			local pci_slot=$(cat $device_name/device/uevent | grep PCI_SLOT | cut -f2- -d":")
			local vendor=$(lspci -v | grep $pci_slot -A1 | grep 'Subsystem' | cut -f2- -d" ")

			store_network_interface_value $name 'vendor' "$vendor"

			local mac_address=$(cat $device_name/address)
			store_network_interface_value $name 'mac_address' "$mac_address"

			local ip_address=$(get_ip_address $name)
			store_network_interface_value $name 'ip_address' "$ip_address"

			local gateway_ip=$(get_gateway_ip $name)
			store_network_interface_value $name 'gateway_ip' "$gateway_ip"

			local netmask=$(get_netmask $name)
			store_network_interface_value $name 'netmask' "$netmask"

		}
	done
}
