#!/bin/sh
#
# build latest v4l modules.
# Author: vincent.mcintyre@gmail.com
# License: GPL2+
#
# Config ----------------------------------------------------------------------
#Location of linux-media and new_build clones
TOP=/home/me/git/clones/linuxtv.org
VERBOSE=1

# Internal config -------------------------------------------------------------
DEBUG=0
INSTALL=0

# Main ------------------------------------------------------------------------
LOGFILE=build.`date +%Y%m%dT%H%M%S`.log
echo "Logging to '$LOGFILE'"
echo "The build could take some time..."
echo "Use 'tail -f $LOGFILE' to follow progress"
exec 1> $LOGFILE 2>&1
echo "
This is '$0'
called on `date`
from `pwd`
with arguments '$@'
"

while test $# -gt  0
do
    case $1 in
      -[iI]*) INSTALL=1; echo "Modules will be INSTALLed"
      ;;
      -[dD]*) DEBUG=1; echo "DEBUG on"
      ;;
      -[vV]*) VERBOSE=1; echo "VERBOSE on"
      ;;
    esac
done

test "1" = "$DEBUG" && set -x
cd "$TOP" || exit 1

echo update the v4l tree first
cd v4l-dvb
if test "1" = "$VERBOSE" ; then
    echo "Now in `pwd`"
    # list all branches
    git branch -a
    # list remotes
    git remote -v show
fi
git checkout media-master
git reset --hard
git clean -f -d
git pull . remotes/linuxtv/staging/for_v2.6.38
# show last log entry
test "1" = "$VERBOSE" && git log -1

echo ""
echo "Now update the new_build"
cd ../new_build
if test "1" = "$VERBOSE" ; then
    echo "Now in `pwd`"
    git branch -a
    git remote -v show
fi
git checkout master
git reset --hard
git clean -f -d
git pull -u
# show last log entry
test "1" = "$VERBOSE" && git log -1
cd linux

echo ""
echo "Clean up"
make clean; make distclean

echo ""
echo "Start build"
make tar DIR=/home/vjm/git/clones/linuxtv.org/v4l-dvb
make untar

# call make to create v4l/.configure
make
#vi ../v4l/.config #turn off FIREDTV
sed -i -e 's/CONFIG_DVB_FIREDTV=m/CONFIG_DVB_FIREDTV=n/' ../v4l/.config

make

if test "1" = "$INSTALL" ; then
    make install
else
    echo " SIMULATING module install"
    make -n install
fi

exit
