#!/bin/bash

########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn is free software: you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# MuGLIn is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

. /muglin/functions
file=$1
##############################

output 2 -n ">>Verifying hardware dependency..."

#disk size
mysize=$(fdisk -l 2>/dev/null | grep "/dev/sda:" | cut -d" " -f5)
imgsize=$(grep "Size=" $file | cut -d"=" -f2)
if [[ "$imgsize" -gt "$mysize" ]]; then
  die "failed: hard disk not big enough" "verifying"
fi
#disk size done

# other hw
HWDEP=$(grep "HwDep=" $file | cut -d"=" -f2)
if [[ "$HWDEP" = "1" ]]; then
	MYHWLIST=$(lspci -n)
	begin=`grep -n "\[Hardware]" $file | cut -d":" -f1`
	end=`grep -n "\[EndHardware]" $file | cut -d":" -f1`
	cat $file | head -n $(($end-1)) | tail -n $(($end-$begin-1)) > /tmp/hwinfo
	while read line; do
		if [[ "$(echo $MYHWLIST | grep $line)" == "" ]]; then
			# TODO: what hw is missing? 
			die "missing hw"
		fi
	done < /tmp/hwinfo
fi

output 2 "done"
