# Created by Tobias Powalowski <tpowa@archlinux.org>

run_hook ()
{
     if grep -qw arch-addons /proc/cmdline; then
     	ARCH_ADDON="optical storage floppy disk"
     	### exclude kernel ntfs it seems broken!
     	msg "Disabling kernel NTFS module for mounting, due to known issues!"
     	mv /lib/modules/$(uname -r)/kernel/fs/ntfs/ntfs.ko /lib/modules/$(uname -r)/kernel/fs/ntfs/ntfs.ko.old
     	msg " Waiting 10 seconds for usb/fw devices to come ready..."
     	sleep 10
     	for i in $ARCH_ADDON; do
		msg "Checking $i devices for addons..."
			for k in $(find /dev -maxdepth 1 -group "$i"); do
     				if mount $k /addons > /dev/null 2>&1; then
					msg "Looking for new config files on $k, checking /config directory..."
					if [ -d "/addons/config" ]; then
						msg "Copying new config files to /etc install environment..."
						cp -r /addons/config/* /etc/
						msg "Finished."
						RETRIGGER_UDEV="1"
					else
						msg "No files found to copy in /config directory on media $i."
					fi
					msg "Looking for new packages to install on $k, checking /packages directory..."
					if [ -d "/addons/packages" ]; then
						mkdir /tmp/packages/
						msg "Copying new packages to /tmp/packages/ install environment..."
						cp /addons/packages/*.pkg.tar.gz /tmp/packages/
						msg "Installing new packages to install environment..."
						pacman -U /tmp/packages/* || msg "Dependency resolution failed!"
						RETRIGGER_UDEV="1"
					else
						msg "No new files found to copy and install in /packages directory on media $k."
					fi
					umount /addons
					sleep 2
				fi
			done
     	done
    	mv /lib/modules/$(uname -r)/kernel/fs/ntfs/ntfs.ko.old /lib/modules/$(uname -r)/kernel/fs/ntfs/ntfs.ko
    	if [ "$RETRIGGER_UDEV" = "1" ]; then
     		msg "Retrigger udev uevents..."
     		udevadm trigger
		udevadm settle
    	fi
    fi
}
