#!/bin/sh
#
# Script to build the DVICO/DNTV Live DVB-T modules
# Chris Pascoe 2004/12/04
#

# Change this to where you unpacked the tree
BUILDDIR=/build
export BUILDDIR

#
# Getting "disagrees about version" messages when you try to load the modules?
# This is because you have the DVB core code in your kernel, and the video4linux
# build matches up against the API used in that version, rather than the one
# in the new dvb-kernel tree here.
#
# To fix this, run this script giving the "remove-symbols" argument, rebuild,
# then try again.
#

#
# Prerequisite kernel build options:
#   CONFIG_I2C=m or y
#   CONFIG_I2C_ALGOBIT=m or y
#   CONFIG_VIDEO_DEV=m or y
#   CONFIG_FW_LOADER=m or y
#   CONFIG_DVB=n
#

# Remove symbols from the kernel tree?
if [ "$1" = "remove-symbols" ]
then
	perl -pi -e 's/\tdvb_/\tcjp_dvb_/' /lib/modules/`uname -r`/build/Module.symvers
	$0 clean
	exit 0
fi

# Clean?
if [ "$1" = "clean" ]
then
	( cd video4linux ; make clean ; rm -rf .tmp_versions )
	( cd dvb-kernel ; make clean ; rm -f build-2.6/bttv.h build-2.6/bt848.h )
	exit 0
fi

# Help?
if [ "x$1" != "x" -a "x$1" != "xbuild" ]
then
	echo "Usage: $0 {remove-symbols,clean,build} (default is build)"
	exit 1
fi

# If the kernel tree already has DVB modules built which may have conflicting
# symbols, emit a warning.
if fgrep -q '	dvb_' /lib/modules/`uname -r`/build/Module.symvers > /dev/null 2>&1
then
	(
		echo ""
		echo "Warning: Your kernel appears to have DVB support already"
		echo "built as modules."
		echo ""
		echo "Depending on your distribution, you may need to do a:"
		echo "    ./DVB-Build remove-symbols"
		echo "    ./DVB-Build clean"
		echo "    ./DVB-Build"
		echo "before you will be able to use load your drivers successfully."
		echo ""
		echo "(This build will continue in 5 seconds)"
		echo ""
	) 1>&2
	sleep 5
fi

# Build Video4Linux
( 
	cd video4linux
	make DVB=1 EXTRA_CFLAGS="-I${BUILDDIR}/dvb-kernel/linux/drivers/media/dvb/dvb-core/ -I${BUILDDIR}/dvb-kernel/linux/drivers/media/dvb/frontends/" || exit 1
	exit 0
) || exit 1

# Build DVB-Kernel
( 
	for i in bttv.h bt848.h
	do
		rm -f dvb-kernel/build-2.6/$i
		cp video4linux/$i dvb-kernel/build-2.6/$i
	done
	cd dvb-kernel
	make BTTV=1 || exit 1
	exit 0
) || exit 1

# Test to see if /dev/dvb exists and if it has the right major number
if [ ! -d /dev/dvb/adapter0 ]
then
	echo ""
	echo "If your system does not use udev/devfs, you may need to run" 1>&2
	echo "$BUILDDIR/dvb-kernel/MAKEDEV-DVB.sh" 1>&2
	echo "to generate your DVB devices." 1>&2
	echo ""
else
	perl -e "my @s = stat('/dev/dvb/adapter0/frontend0'); print STDERR \"\nNote: You may need to run $BUILDDIR/dvb-kernel/MAKEDEV-DVB.sh\nThis will (re)generate your DVB devices with the new DVB device major.\n\n\" unless (\$s[6] == 154275);"
fi

