#!/bin/bash

# Copyright Jean-Philippe Guillemin <jp.guillemin@free.fr>. This program 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 2 of the License, or (at your option)
# any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm

# Script modified by Ejis <ejis@mailoo.org>

version='2.6.33.7.2'
rt='rt30'
pkgversion='70.3'
softname='kernel-rt'

arch='i486'
cpu='i686'
Ktune=i486
Karch=x86
Kver=${version}

buildir=$(pwd)

srcdir="/usr/src/linux-${version}-${rt}"

newdir="${softname}-${version}.${rt}-${arch}-${pkgversion}"
PKG=/tmp/${newdir}

cd ${buildir}
rm -rf ${PKG}
rm -f ${PKG}.tgz

# Creating main directories
mkdir -p ${PKG}/boot
mkdir -p ${PKG}/etc/rc.d
mkdir -p ${PKG}/lib/modules
mkdir -p ${PKG}${srcdir}
mkdir -p ${PKG}/usr/{doc,src}/kernel-rt-${version}.${rt}
mkdir -p ${PKG}/usr/include

# Copying the kernel and modules
install -D -m644  ${srcdir}/arch/${Karch}/boot/bzImage ${PKG}/boot/vmlinuz-${version}-${rt}
install -D -m644  ${srcdir}/System.map ${PKG}/boot/System.map-${version}-${rt}
install -D -m644  ${srcdir}/.config ${PKG}/boot/config-${version}-${rt}

(
	cd ${PKG}/boot
	ln -s vmlinuz-${version}-${rt} vmlinuz-rt
	ln -s System.map-${version}-${rt} System.map-rt
	ln -s config-${version}-${rt} config-rt
)

## Install kernel modules
(
	cd ${srcdir}
	make INSTALL_MOD_PATH=${PKG} modules_install
)

(
	cd ${srcdir}
	## Install fake kernel source
	install -D -m644 Module.symvers  ${PKG}${srcdir}/Module.symvers
	install -D -m644 Makefile        ${PKG}${srcdir}/Makefile
	install -D -m644 kernel/Makefile ${PKG}${srcdir}/kernel/Makefile
	install -D -m644 .config         ${PKG}${srcdir}/.config
	install -D -m644 .config         ${PKG}/lib/modules/${version}-${rt}/.config

	## Install scripts directory
	cp -a scripts/ ${PKG}${srcdir}
	
	# cp -a Documentation/ ${PKG}${srcdir}
	## remove other architecture docs
	#for i in `/bin/ls arch/ -F1|grep "/"|grep -v "x86\|ia64"` DocBook/; 
	# do [ -d ${PKG}${srcdir}/Documentation/$i ] \
	#	&& rm -fr ${PKG}${srcdir}/Documentation/$i; 
	#done

	## Install userspace kernel headers
	#(
	#cd ${srcdir}
	#make headers_check
	#make headers_install ARCH=i386 INSTALL_HDR_PATH=${PKG}/usr
	#)
	#(
	#cd ${PKG}/usr/include/linux
	#	ln -sf autoconf.h config.h
	#)
	
	## Install kernel space kernel headers
	(
	cd ${srcdir}
	cp -rf include ${PKG}${srcdir}/include	
	)
	## Install headers for lirc
	mkdir -p ${PKG}${srcdir}/drivers/media/video
	cp drivers/media/video/*.h ${PKG}${srcdir}/drivers/media/video/

	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvideo zc0301; do
		mkdir -p ${PKG}${srcdir}/drivers/media/video/$i
		cp -a drivers/media/video/$i/*.h ${PKG}${srcdir}/drivers/media/video/$i
	done

	## Install dm headers
	mkdir -p ${PKG}${srcdir}/drivers/md
	cp drivers/md/*.h ${PKG}${srcdir}/drivers/md

	## Install inotify.h
	mkdir -p ${PKG}${srcdir}/include/linux
	cp include/linux/inotify.h ${PKG}${srcdir}/include/linux/

	## Install CLUSTERIP file for iptables
	mkdir -p ${PKG}${srcdir}/net/ipv4/netfilter/
	cp net/ipv4/netfilter/ipt_CLUSTERIP.c ${PKG}${srcdir}/net/ipv4/netfilter/

	## Install wireless headers
	mkdir -p ${PKG}${srcdir}/net/mac80211/
	cp net/mac80211/*.h ${PKG}${srcdir}/net/mac80211/

	## Install Kconfig files
	for i in `find . -name "Kconfig*"`; do
		mkdir -p ${PKG}${srcdir}/`echo $i | sed 's|/Kconfig.*||'`
		cp $i ${PKG}${srcdir}/$i
	done

	## Copy arch files
	mkdir -p ${PKG}${srcdir}/arch/${Karch}/kernel
	cp arch/${Karch}/kernel/asm-offsets.s ${PKG}${srcdir}/arch/${Karch}/kernel

	cp arch/${Karch}/Makefile* ${PKG}${srcdir}/arch/${Karch}
	cp -r arch/${Karch}/configs ${PKG}${srcdir}/arch/${Karch}

	## Copy arch includes for external modules and fix the nVidia issue
	mkdir -p ${PKG}${srcdir}/arch/${Karch}
	cp -a arch/${Karch}/include ${PKG}${srcdir}/arch/${Karch}/

	(cd ${PKG}${srcdir}/arch && ln -s ${Karch} i386)

	## Remove alien architectures
	cd ${PKG}${srcdir}/arch
	find . -type d -mindepth 1 -maxdepth 1 ! -iname x86 | xargs rm -fr

	## Create needed symlinks
	(cd ${PKG}${srcdir}/include
	 rm  -f asm asm-${Karch} 2>/dev/null
	 ln -s ../arch/${Karch}/include/asm asm-${Karch}
	 ln -s ../arch/${Karch}/include/asm asm
	)

	(cd ${PKG}/lib/modules/${version}-${rt}
	 rm -rf source build 2>/dev/null
	 ln -sf ../../..${srcdir} build
	)
	
)



# Creating rc.modules

cat << EOF > ${PKG}/etc/rc.d/rc.modules.new
#!/bin/sh
# rc.modules   $(date) <jp.guillemin@free.fr>
EOF


cat << "EOF" >> ${PKG}/etc/rc.d/rc.modules.new
#
# This file loads extra drivers into the Linux kernel.
# As kmod and udev handle module loading, this file will be mostly
# commented out, but is still useful for loading selected modules at boot time.
#

RELEASE=$(uname -r)

echo "Updating module dependencies for Linux $RELEASE"

/sbin/depmod -A


EOF

find ${PKG}/lib/modules -type f -iname *.ko | sed -e "s/^.*\/\(.*\)\.ko$/# \/sbin\/modprobe \1/m ; \
s/# \(.* ac$\)/\1/ ; \
s/# \(.* battery$\)/\1/ ; \
s/# \(.* button$\)/\1/ ; \
s/# \(.* fan$\)/\1/ ; \
s/# \(.* fuse$\)/\1/ ; \
s/# \(.* processor$\)/\1/ ; \
s/# \(.* thermal$\)/\1/ ; \
s/# \(.* lp$\)/\1/ ; \
s/# \(.* parport_pc$\)/\1/ ; \
s/# \(.* video$\)/\1/" | sort \
>> ${PKG}/etc/rc.d/rc.modules.new

chmod 755 ${PKG}/etc/rc.d/rc.modules.new


chown -R root:root ${PKG}

mkdir -p ${PKG}/install

cat << "EOF" >  ${PKG}/install/doinst.sh
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/rc.d/rc.modules.new

EOF

cat << "EOF" > ${PKG}/install/slack-desc
kernel: Linux kernel 
kernel:
kernel: The Linux kernel is compiled with support for most hardware.
kernel: 
kernel: Drivers (such as for CD-ROM drives on proprietary interfaces, or
kernel: or ethernet cards) may be loaded as modules.
kernel: See /etc/rc.d/rc.modules for examples
kernel:
kernel: This kernel is patched for Real Time.
kernel: 
kernel: 
EOF


mkdir -p ${PKG}/usr/src/${softname}-${version}.${rt}
cp ${buildir}/build-${softname}.sh ${PKG}/usr/src/${softname}-${version}.${rt}

cd ${PKG}
makepkg -p -l y -c n ${PKG}.txz

# cleanup
cd ${buildir}
rm -rf ${PKG}


