#!/bin/bash
#Script version .3alpha
#2015/02/18
#FOG version 1.2.0
#Copies drivers to a C:\drivers on a universal image based on the hardware
#detected on PCI, USB & PNP.  Full documentation & How-To's on the FOG Wiki.
#http://www.fogproject.org/wiki/index.php/Auto_driver_Install
#Andrew Single - Frontier School Division (Frontier loves FOG!)

oIFS=$IFS;
getDeviceName()
{
        local devnamefull=`lspci -mm -d $1`;
        devnamefull=${devnamefull//\" \"/¤};
        devnamefull=${devnamefull//\" /¤};
        IFS=¤;
        pciDescr=($devnamefull);
        IFS=$oIFS;
}

#Read PCI Device strings
pci=`lspci -n |cut -c 15-23`;
#Copy detected devices to C:\Drivers\HWDetect.txt
echo "FOG Driver Install Detected PCI Devices:" >/ntfs/Drivers/HWDetect.txt
last="";
for device in $pci
do
	#filter for unique devices
	if [ "$device" != "$last" ]; then
		last=$device;
		#Split up vendor & Device IDs
		dMfg=${device:0:4};
		dDev=${device:5:4};
		drvLoc="/images/drivers/$dMfg/$dDev";
		#Does the driver folder exist?
		if [ -d $drvLoc ];then
			#Copy files
			getDeviceName $device;
			echo -n " * Copying PCI Driver ${pciDescr[4]} ........";
			echo "Device: $device (${pciDescr[4]}) using  $drvLoc." >>/ntfs/Drivers/HWDetect.txt
			#Does the vendor folder exist?
			if [ -d /ntfs/Drivers/$dMfg ]; then 
				cp -r $drvLoc /ntfs/Drivers/$dMfg
			else
				 mkdir /ntfs/Drivers/$dMfg
				cp -r $drvLoc /ntfs/Drivers/$dMfg
			fi
			if [ -f $drvLoc/drvinstall.bat ]; then
				cat $drvLoc/drvinstall.bat >>/ntfs/Windows/Setup/Scripts/SetupComplete.cmd
				unix2dos /ntfs/Windows/Setup/Scripts/SetupComplete.cmd
			fi
			echo "Done.";
		else
			echo "Device: $device(${pciDescr[4]}) no driver present." >>/ntfs/Drivers/HWDetect.txt
		fi
	fi
done

IFS='
';

#enumerate PNP devices
pnpdevices=`dmesg |grep IDs`;
echo "FOG Driver Install Detected PNP Devices:" >>/ntfs/Drivers/HWDetect.txt
for pnp in $pnpdevices
do
    pnp=${pnp#*IDs };
    pnp=${pnp:0:7};
	#filter for unique devices
    if [ "$pnp" != "$last" ]; then
        last=$pnp;
        drvLoc="/images/drivers/pnp/$pnp";
        #Does the driver folder exist?
        if [ -d $drvLoc ];then
            #Copy files
            echo -n " * Copying PnP Device $pnp ........";
			echo "Device: $pnp found driver from $drvLoc." >>/ntfs/Drivers/HWDetect.txt
            #Does the  folder exist?
            if [ -d /ntfs/Drivers/pnp ]; then
                cp -r $drvLoc /ntfs/Drivers/pnp
            else
                 mkdir /ntfs/Drivers/pnp
                cp -r $drvLoc /ntfs/Drivers/pnp
            fi
            if [ -f $drvLoc/drvinstall.bat ]; then
                cat $drvLoc/drvinstall.bat >>/ntfs/Windows/Setup/Scripts/SetupComplete.cmd
                unix2dos /ntfs/Windows/Setup/Scripts/SetupComplete.cmd
            fi
            echo "Done.";
        else
			echo "Device: $pnp no driver present." >>/ntfs/Drivers/HWDetect.txt
		fi
    fi


done

IFS=$oIFS;

usb=`lsusb |cut -c 23-32`;

echo "FOG Driver Install Detected USB Devices:" >>/ntfs/Drivers/HWDetect.txt
#cat usb >>/ntfs/Drivers/HWDetect.txt

for device in $usb
do
    #filter for unique devices
    if [ "$device" != "$last" ]; then
        last=$device;
        #Split up vendor & Device IDs
        dMfg=${device:0:4};
        dDev=${device:5:4};
        drvLoc="/images/drivers/$dMfg/$dDev";
        #Does the driver folder exist?
        if [ -d $drvLoc ];then
            #Copy files
            echo -n " * Copying USB Driver ${pciDescr[4]} ........";
			echo "Device: $device (${pciDescr[4]}) using $drvLoc." >>/ntfs/Drivers/HWDetect.txt
            #Does the vendor folder exist?
            if [ -d /ntfs/Drivers/$dMfg ]; then
                cp -r $drvLoc /ntfs/Drivers/$dMfg
            else
                 mkdir /ntfs/Drivers/$dMfg
                cp -r $drvLoc /ntfs/Drivers/$dMfg
            fi
            if [ -f $drvLoc/drvinstall.bat ]; then
                cat $drvLoc/drvinstall.bat >>/ntfs/Windows/Setup/Scripts/SetupComplete.cmd
                unix2dos /ntfs/Windows/Setup/Scripts/SetupComplete.cmd
            fi
            echo "Done.";
        else
			echo "Device: $device (${pciDescr[4]}) no driver present." >>/ntfs/Drivers/HWDetect.txt
		fi
    fi
done
unix2dos /ntfs/Drivers/HWDetect.txt