# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: lucianm Exp $
#
# Author:
#   Lucian Muresan <lucianm AT users DOT sourceforge DOT net>
#   based upon code from the vdr-plugin.eclass by
#     Matthias Schwarzott <zzam@gentoo.org>
#     Joerg Bornkessel <hd_brummy@gentoo.org>

# orion5x-kernel.eclass
#

inherit kernel-2

IUSE_ORION_NAS_DEVICES="linkstation_pro_duo linkstation_pro_live linkstation_mini kurobox_pro qnap_ts_109_209 qnap_ts_409 dlink_dns_323 hp_mv2120 ts_78xx_sbc wrt350_v2 terastation_pro_v2 netronix_nh233 maxtor_shared_storage_2"

DEPEND="${DEPEND}
	sys-block/devio
	dev-embedded/u-boot-tools"

PROVIDE="virtual/linux-sources"

MY_MACHINE_TYPE=""
nasdevice_count=0

for nasdevice in ${IUSE_ORION_NAS_DEVICES}; do
	IUSE="${IUSE} orion_nas_devices_${nasdevice}"
done

orion5x-kernel_add_nasdevice() {
	#: ${nasdevice_count:=0}
	: $(( nasdevice_count++ ))

	if [[ ${nasdevice_count} -eq 2 ]]; then
		ewarn
		ewarn "Only one machine type can be patched after building the zImage!"
		ewarn "Please select just one of '${IUSE_ORION_NAS_DEVICES}'!"
		die "Multiple machine types selected"
	fi

	local dev="${1}"
	local desc="device ${dev}"
	if [[ -n "${2}" ]]; then
		desc="${2}"
	fi

	einfo "Will patch the zImage for ${desc}"
	MY_MACHINE_TYPE="${MY_MACHINE_TYPE} ${dev}"
}

orion5x-kernel_patch_help() {
	einfo ""
	einfo ""
	einfo ""
	einfo "You may add your own patches like this:"
	einfo ""
	einfo " - Add the following variable to your /etc/make.conf:"
	einfo "   ORION5X_KERNEL_PATCHES_DIR=\"/path/to/my/patches\""
	einfo ""
	einfo " - Create the following subdirectories inside your local patch directory:"
	einfo "   ${PN}/${PV}"
	einfo ""
	einfo " - Place your patches ending on *.diff or *.patch in the created"
	einfo "   \"${PV}\" directory, together with a file called \"0_list\" in which you"
	einfo "   list them in the order you want them applied."
	einfo ""
}

orion5x-kernel_pkg_setup() {
	kernel-2_pkg_setup

	local nasdevice

	for nasdevice in ${IUSE_ORION_NAS_DEVICES}; do
		if use orion_nas_devices_${nasdevice}; then
			orion5x-kernel_add_nasdevice ${nasdevice}
		fi
	done

	if [[ ${nasdevice_count} -eq 0 ]]; then
		ewarn
		ewarn "You did not select a machine type to be patched after building the zImage!"
		ewarn "Please set 'ORION_NAS_DEVICES' to just one of '${IUSE_ORION_NAS_DEVICES}'!"
		die "No Orion machine type selected"
	fi

	if [ -z "${ORION5X_KERNEL_PATCHES_DIR}" ] ; then
		orion5x-kernel_patch_help
	else
		if ! [ -d "${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}" ] ; then
			orion5x-kernel_patch_help
		fi
	fi
}

orion5x-kernel_apply_local_patches() {
	cd ${S}
	if test -d "${ORION5X_KERNEL_PATCHES_DIR}/${PN}"; then
		echo
		local PATCH_LIST=$(cat ${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/0_list) 
		einfo "Applying local patches found in"
		einfo "${ORION5X_KERNEL_PATCHES_DIR}:"
#		for LOCALPATCH in ${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/*.{diff,patch}; do
#			test -f "${LOCALPATCH}" && epatch "${LOCALPATCH}"
#		done
		for LOCALPATCH in ${PATCH_LIST}; do
			cd ${S}
			test -f "${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/${LOCALPATCH}" && patch -p 1 < "${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/${LOCALPATCH}"
			#test -f "${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/${LOCALPATCH}" && epatch "${ORION5X_KERNEL_PATCHES_DIR}/${PN}/${PV}/${LOCALPATCH}"
		done		
	fi
}

orion5x-kernel_generate_mach-type_patch(){
echo "diff --git a/arch/arm/boot/Makefile b/arch/arm/boot/Makefile
index 25f1230..789190e 100644
--- a/arch/arm/boot/Makefile
+++ b/arch/arm/boot/Makefile
@@ -55,6 +55,10 @@ \$(obj)/compressed/vmlinux: \$(obj)/Image FORCE
 
 \$(obj)/zImage:	\$(obj)/compressed/vmlinux FORCE
 	\$(call if_changed,objcopy)
+	devio > foo 'wl 0xe3a01c05,4' 'wl 0xe38110e5,4'
+	cat foo \$(obj)/zImage > zImage.new
+	mv zImage.new \$(obj)/zImage
+	rm foo
 	@echo '  Kernel: \$@ is ready'
 
 endif" > ${WORKDIR}/force_mach-type.patch
}

orion5x-kernel_src_unpack(){

	kernel-2_src_unpack
	orion5x-kernel_apply_local_patches

	# create mach-type patch in the working dir, to modify it according to the
	# set orion nas device
	orion5x-kernel_generate_mach-type_patch

	if use orion_nas_devices_linkstation_pro_live; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811031,4' # Linkstation Pro/Live
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811031,4:" ${WORKDIR}/force_mach-type.patch
	fi

	#if use orion_nas_devices_kurobox_pro; then
	#nothing to replace for the Kurobox Pro
	#fi

	if use orion_nas_devices_qnap_ts_109_209; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe381101d,4' # QNAP TS-109/TS-209
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe381101d,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_qnap_ts_409; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811041,4' # QNAP TS-409
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811041,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_dlink_dns_323; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811006,4' # D-Link DNS-323
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811006,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_hp_mv2120; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe381109d,4' # HP Media Vault MV2120
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe381109d,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_ts_78xx_sbc; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811074,4' # Technologic Systems TS-78xx SBC
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811074,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_maxtor_shared_storage_2; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe38110e6,4' # Maxtor Shared Storage II (2.6.27)
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe38110e6,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_wrt350_v2; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811061,4' # Linksys WRT350N v2
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811061,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_linkstation_pro_duo; then
		#devio > foo 'wl 0xe3a01c07,4' 'wl 0xe3811027,4' # Buffalo Linkstation Pro Duo (Is this right?)
		sed -i -e "s:0xe3a01c05,4:0xe3a01c07,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811027,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_linkstation_mini; then
		#devio > foo 'wl 0xe3a01c07,4' 'wl 0xe3811042,4' # Buffalo Linkstation Mini (Is this right?)
		sed -i -e "s:0xe3a01c05,4:0xe3a01c07,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811042,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_terastation_pro_v2; then
		#devio > foo 'wl 0xe3a01c06,4' 'wl 0xe3811030,4' # Buffalo TS Pro V2 (not yet supported in Vanilla 2.6.26 as of Aug. 10th, 2008)
		sed -i -e "s:0xe3a01c05,4:0xe3a01c06,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811030,4:" ${WORKDIR}/force_mach-type.patch
	fi

	if use orion_nas_devices_netronix_nh233; then
		#devio > foo 'wl 0xe3a01c07,4' 'wl 0xe3811065,4' # Netronix NH-233
		sed -i -e "s:0xe3a01c05,4:0xe3a01c07,4:" ${WORKDIR}/force_mach-type.patch
		sed -i -e "s:0xe38110e5,4:0xe3811065,4:" ${WORKDIR}/force_mach-type.patch
	fi

	# finally patch with the correct orion nas device
	epatch ${WORKDIR}/force_mach-type.patch
}

orion5x-kernel_pkg_postinst(){
	postinst_sources
}

EXPORT_FUNCTIONS pkg_setup src_unpack pkg_postinst
