# SliTaz package receipt.

PACKAGE="linux-libre"
VERSION="2.6.37-libre"
CATEGORY="base-system"
SHORT_DESC="The Linux Libre kernel and modules."
MAINTAINER="gokhlayeh@slitaz.org"
PROVIDE="linux"
SOURCE="linux"
TARBALL="$SOURCE-$VERSION.tar.bz2"
WEB_SITE="http://www.fsfla.org/svnwiki/selibre/linux-libre/"
WGET_URL="http://www.fsfla.org/svnwiki/selibre/linux-libre/download/releases/$VERSION/$TARBALL"
CONFIG_FILES="/lib/modules/$VERSION-slitaz/modules.dep"

DEPENDS="depmod"
BUILD_DEPENDS="slitaz-toolchain perl git lzma patch"

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	# this is code to help update the kernel version faster
	#for i in $(grep -l 'WANTED="linux"' $WOK/*/receipt)
	#do
	#	sed -i 's|VERSION="OLDVERSION"|VERSION="NEWVERSION"|g' $i
	#done

	[ ! -x /usr/bin/cook ] && report open-bloc

	# Check for Aufs and cook it if unbuilt.
	echo "Checking for Aufs packages..."
	_AUFSVER=`grep  ^VERSION= $WOK/aufs/receipt | cut -d "=" -f2 | sed -e 's/"//g'`
	if [ ! -d "$AUFSDIR" ] && [ -x /usr/bin/cook ]; then
		# Give info an redirect to /dev/null since aufs got it own log.
		AUFSDIR=$WOK/aufs/source/aufs-${_AUFSVER}
		echo "Cook: aufs $_AUFSVER"
		cook aufs > /dev/null
	elif [ ! -x /usr/bin/cook ]; then
		# We only need aufs source. Also cooking now is a bad idea since it
		# needs linux-module-headers.
		AUFSDIR=$WOK/aufs/aufs-${_AUFSVER}
		[ -d "$AUFSDIR" ] && rm -rf $AUFSDIR
		tazwok get-src aufs --target=$AUFSDIR || { report close-bloc; return 1; }
	fi
	
	echo "Copying Aufs files and patches..."
	cp -a $AUFSDIR/Documentation $AUFSDIR/fs $AUFSDIR/include $src
	cp -a $AUFSDIR/*.patch $stuff
	
	# SliTaz db
	rm -rf $WOK/$PACKAGE/slitaz && mkdir $WOK/$PACKAGE/slitaz
	echo "$WGET_URL" > $WOK/$PACKAGE/slitaz/url
	cp $stuff/gztazmod.sh $stuff/list_modules.sh $WOK/$PACKAGE/slitaz
	cp $stuff/bootloader.sh $WOK/$PACKAGE/slitaz
	
	# Apply patches
	echo "Applying patches..."
	while read patch_file; do
		echo "$patch_file" >> $WOK/$PACKAGE/slitaz/patches
		cp $stuff/$patch_file $WOK/$PACKAGE/slitaz
		if [ -f done.$patch_file ]; then
			echo "Skipping $patch_file"
			continue
		fi
		echo "Apply $patch_file"
		patch -p1 < $WOK/$PACKAGE/slitaz/$patch_file || { report close-bloc; return 1; }
		touch done.$patch_file
	done <<EOT
$PACKAGE-diff-$VERSION.u
$PACKAGE-header-$VERSION.u
$PACKAGE-freeinitrd-$VERSION.u
aufs2-base.patch
aufs2-standalone.patch
001-squashfs-decompressors-add-xz-decompressor-module.patch
002-squashfs-decompressors-add-boot-time-xz-support.patch
003-squashfs-x86-support-xz-compressed-kernel.patch
004-squashfs-add-xz-compression-support.patch
005-squashfs-add-xz-compression-configuration-option.patch
EOT

	[ ! -x /usr/bin/cook ] && report step "Make kernel proper and then build lguest"
	
	make mrproper
	cd Documentation/lguest
	make $MAKEFLAGS lguest || { report close-bloc; return 1; }
	cd $src

	[ ! -x /usr/bin/cook ] && report step "Make bzImage without modules first"

	# Build bzImage without modules first
	cp -f $stuff/$PACKAGE-$VERSION-slitaz.config .config
	sed -i 's/CONFIG_MODULES=y/# CONFIG_MODULES is not set/' .config
	
	# We can't keep every driver in staging 
	sed -i -e 's/^CONFIG_RTL8192/#&/' \
	       -e 's/^CONFIG_R8187SE/#&/' \
	       -e 's/^CONFIG_RT2870/#&/' .config
	yes '' | make oldconfig
	make $MAKEFLAGS bzImage || { report close-bloc; return 1; }
	[ -d $PWD/_pkg ] || mkdir -p $PWD/_pkg
	mv arch/x86/boot/bzImage $PWD/_pkg
	mv System.map System.map-without-modules

	[ ! -x /usr/bin/cook ] && report step "Now build bzImage with modules"
	
	# Build bzImage with modules
	cp -f $stuff/$PACKAGE-$VERSION-slitaz.config .config
	make oldconfig
	ln .config $WOK/$PACKAGE/slitaz/config
	make $MAKEFLAGS bzImage &&
	make $MAKEFLAGS modules &&
	make INSTALL_MOD_PATH=$PWD/_pkg modules_install &&
	make INSTALL_HDR_PATH=$PWD/_pkg/usr headers_install &&
	[ -s arch/x86/boot/bzImage ] || { report close-bloc; return 1; }
	mkdir -p $PWD/_pkg/boot 2> /dev/null
	mv arch/x86/boot/bzImage $PWD/_pkg/boot/vmlinuz-$VERSION-slitaz
	
	[ ! -x /usr/bin/cook ] && report step "Compressing all modules"
	
	# Compress all modules.
	$stuff/gztazmod.sh $PWD/_pkg/lib/modules/$VERSION-slitaz
	ln System.map System.map-modules
	ln Module.symvers Module.symvers-modules
	
	if [ ! -x /usr/bin/cook ]; then
		report close-bloc
	fi
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
    local path
    cp -a $_pkg/boot $fs
    # Compress all modules.
    #$stuff/gztazmod.sh $_pkg/lib/modules/$VERSION-slitaz
    path=$fs/lib/modules/$VERSION-slitaz/kernel
    mkdir -p $path
    cp -a $_pkg/lib/modules/$VERSION-slitaz/mo* \
		$fs/lib/modules/$VERSION-slitaz
    # Get the base modules
    export src
    export _pkg
    mkdir $WOK/$PACKAGE/tmp
    $stuff/list_modules.sh \
	$(cat stuff/modules-$VERSION.list) > $WOK/$PACKAGE/tmp/modules.list
    while read module; do
    	dir=$(dirname $module)
    	[ -d $path/$dir ] || mkdir -p $path/$dir
        cp -a $_pkg/lib/modules/$VERSION-slitaz/kernel/$module $path/$dir
    done < $WOK/$PACKAGE/tmp/modules.list
    # Remove unresolved links
    rm -f $fs/lib/modules/$VERSION-slitaz/build
    rm -f $fs/lib/modules/$VERSION-slitaz/source
    # Check and echo any module in kernel .config that's not added to 
    # one of linux-* pkgs
    export PACKAGE
	$stuff/check_modules.sh 
}

# Pre and post install commands for Tazpkg.
post_install()
{
    echo "Processing post-install commands..."
    chroot "$1/" depmod -a $VERSION-slitaz
    # GRUB stuff.
    if [ -f "$1/boot/grub/menu.lst" ]; then
    	root_dev=`cat $1/boot/grub/menu.lst | grep root= | sed 's/.*root=\([^ ]*\).*/\1/' | head -n 1`
		grub_dev=`cat $1/boot/grub/menu.lst | grep "root (" | head -n 1`
		# Add new kernel entry in case of upgrade for installed system.
		if ! grep -q $PACKAGE-$VERSION-slitaz $1/boot/grub/menu.lst; then
    		cat >> $1/boot/grub/menu.lst << EOT

title SliTaz GNU/Linux (Kernel $VERSION-slitaz)
$grub_dev
kernel /boot/vmlinuz-$VERSION-slitaz root=$root_dev
EOT
		fi
		# Display information message.
    	cat <<EOT
----
GRUB is installed, these tree lines must be in your /boot/grub/menu.lst:

title  SliTaz GNU/Linux (Kernel $VERSION-slitaz)
$grub_dev
kernel /boot/vmlinuz-$VERSION-slitaz root=$root_dev
----
EOT
	fi
}