#!/bin/sh
set -x
KBUILDASM=`mktemp /tmp/extract.$$.XXXXXX` || exit 1
KBUILDFILE=`mktemp /tmp/extract.$$.XXXXXX` || exit 1
MAKEFILE=`mktemp /tmp/extract.$$.XXXXXX` || exit 1

TEMPFILE=`mktemp /tmp/extract.$$.XXXXXX` || exit 1

cat > $MAKEFILE <<EOF
include $KBUILDFILE

header-y        := \$(sort \$(header-y))
unifdef-y       := \$(sort \$(unifdef-y))
subdir-y        := \$(patsubst %/,%,\$(filter %/, \$(header-y)))
header-y        := \$(filter-out %/, \$(header-y))
header-y        := \$(filter-out \$(unifdef-y),\$(header-y))

asm-%:
	@echo ACTION=subdir

\$(subdir-y):
	@echo ACTION=subdir

\$(header-y):
	@echo ACTION=header

\$(unifdef-y):
	@echo ACTION=unifdef

%::
	@echo ACTION=ignore
EOF
export UPSTREAM_GITDIR=/pub/scm/linux/kernel/git/torvalds/linux-2.6.git/
#export UPSTREAM_GITDIR=/home/git/mtd-2.6.git
cd $UPSTREAM_GITDIR || exit 1
export GIT_DIR=/pub/scm/linux/kernel/git/dwmw2/kernel-headers.git
cd $GIT_DIR || exit 1
export EQUIV_DIR=/home/dwmw2/.khdr-equiv
cd $EQUIV_DIR || exit 1

function includeparent()
{
    git-rev-list --max-count=1 --topo-order $1 -- include
}

function sedblob()
{
    if [ ! -r $EQUIV_DIR/sed-$1 ]; then
	git-cat-file blob $1 | sed  -e "s/ inline / __inline__ /g" \
            -e "s/[[:space:]]__user[[:space:]]\+/ /g" \
            -e "s/(__user[[:space:]]\+/ (/g" \
            -e "s/[[:space:]]__force[[:space:]]\+/ /g" \
            -e "s/(__force[[:space:]]\+/ (/g" \
            -e "s/[[:space:]]__iomem[[:space:]]\+/ /g" \
            -e "s/(__iomem[[:space:]]\+/ (/g" \
            -e "s/[[:space:]]__attribute_const__[[:space:]]\+/\ /g" \
            -e "s/[[:space:]]__attribute_const__$$//" \
            -e "/^\#include <linux\/compiler.h>/d" > $TEMPFILE
	git-hash-object -t blob -w $TEMPFILE > $EQUIV_DIR/sed-$1
    fi
    cat $EQUIV_DIR/sed-$1
}

function unifdefblob()
{
    if [ ! -r $EQUIV_DIR/unifdef-$1 ]; then
	git-cat-file blob $1 | unifdef -U__KERNEL__ |
	    sed  -e "s/ inline / __inline__ /g" \
		-e "s/[[:space:]]__user[[:space:]]\+/ /g" \
		-e "s/(__user[[:space:]]\+/ (/g" \
		-e "s/[[:space:]]__force[[:space:]]\+/ /g" \
		-e "s/(__force[[:space:]]\+/ (/g" \
		-e "s/[[:space:]]__iomem[[:space:]]\+/ /g" \
		-e "s/(__iomem[[:space:]]\+/ (/g" \
		-e "s/[[:space:]]__attribute_const__[[:space:]]\+/\ /g" \
		-e "s/[[:space:]]__attribute_const__$$//" \
		-e "/^\#include <linux\/compiler.h>/d" > $TEMPFILE
	git-hash-object -t blob -w $TEMPFILE > $EQUIV_DIR/unifdef-$1
    fi
    cat $EQUIV_DIR/unifdef-$1
}

function converttree()
{
    local COMMIT TREE MODE TYPE SHA1 NAME BASENAME HEXSHA1 NEWSHA NEWDIR NEWTREE KBUILDASMSHA TEMPFILE2
    TREE=$1
    KBUILDASMSHA=$2
    NEWDIR=
    if [ ! -r $EQUIV_DIR/tree-$TREE-$KBUILDASMSHA ] ; then
	TEMPFILE2=`mktemp /tmp/extract.$$.XXXXXX` || exit 1
	> $TEMPFILE2
	KBUILDSHA=`git-ls-tree $TREE -- Kbuild | cut -f3 -d\  | cut -f1`
        # If no Kbuild file in this directory, abort 
	if [ "$KBUILDSHA" = "" ]; then
	    rm $TEMPFILE2
	    exit
	fi
	git-cat-file blob $KBUILDSHA | sed s:include/asm-generic/Kbuild.asm:$KBUILDASM: > $KBUILDFILE
	git-ls-tree $TREE | while read MODE TYPE SHA1 NAME; do 
	    NEWSHA=
	    eval `make -f $MAKEFILE $NAME`
	    case $ACTION in
		subdir)
		    NEWSHA=`converttree $SHA1 $KBUILDASMSHA`
		    git-cat-file blob $KBUILDSHA | sed s:include/asm-generic/Kbuild.asm:$KBUILDASM: > $KBUILDFILE
		    ;;
			
		header)
		    NEWSHA=`sedblob $SHA1`
		    ;;
	    
		unifdef)
		    NEWSHA=`unifdefblob $SHA1`
		    ;;
		ignore)
		    ;;
		*)
		    echo fuck > /dev/tty
	    esac
	    if [ "$NEWSHA" != "" ]; then
		HEXSHA1=`echo $NEWSHA | sed 's/\(..\)/\\\x\1/g'`
		echo -en $MODE $NAME\\x00$HEXSHA1 >> $TEMPFILE2
	    fi
	done
	git-hash-object -t tree -w $TEMPFILE2 > $EQUIV_DIR/tree-$TREE-$KBUILDASMSHA
	rm -f $TEMPFILE2
    fi
    cat $EQUIV_DIR/tree-$TREE-$KBUILDASMSHA
}

function convertcommit()
{
    local COMMIT PARENTS CONVERTEDPARENTS TREE INCDIR KBUILDASMSHA

    COMMIT=$1
    PARENTS=
    CONVERTEDPARENTS=

    KBUILDASMSHA=`git-ls-tree $COMMIT include/asm-generic/Kbuild.asm | cut -f3 -d\  | cut -f1`
    # If this version didn't have include/asm-generic/Kbuild, then bail
    if [ "$KBUILDASMSHA" == "" ]; then
	exit
    fi
    if [ ! -r $EQUIV_DIR/$COMMIT ] ; then
	git-cat-file commit $1 | sed /^\$/q | grep ^parent > $TEMPFILE
	while read foo PARENT; do
	    PARENTS="$PARENTS `includeparent $PARENT`"
	done < $TEMPFILE

	for a in $PARENTS; do
	    CONVERTEDPARENTS="$CONVERTEDPARENTS `convertcommit $a`"
	done
        # Store the contents of include/asm-generic/Kbuild for this commit,
        # because we'll need them later.
	git-cat-file blob $KBUILDASMSHA > $KBUILDASM
	INCDIR=`git-ls-tree $COMMIT include | cut -f3 -d\  | cut -f1`
	TREE=`converttree $INCDIR $KBUILDASMSHA`
	echo tree $TREE > $TEMPFILE

	for a in $CONVERTEDPARENTS; do
	    echo parent $a >> $TEMPFILE
	done
	git-cat-file commit $COMMIT | while read key value; do
	    case $key in
		tree|parent)
		    ;;
		*)
		    echo "$key $value"
		    cat
		    ;;
	    esac
	done >> $TEMPFILE
	git-hash-object -t commit -w $TEMPFILE > $EQUIV_DIR/$COMMIT
    fi
    cat $EQUIV_DIR/$COMMIT
}

NEWUPSTREAM=`cat $UPSTREAM_GITDIR/refs/heads/master`
#NEWUPSTREAM=e8f75588dd5885868147b329ced4a5093dc6402e
#NEWUPSTREAM=c0bc8721b8d0380ec69fa97578c91201201b05a9
#NEWUPSTREAM=a8e0c51c71fc973b400f6502382063553b82af5a
#NEWUPSTREAM=ca78f6baca863afe2e6a244a0fe94b3a70211d46
#NEWUPSTREAM=6fa0cb1141da80eed4f86155fb51931bc1c3188

#NEWUPSTREAM=6a87a86d064cee49b3c298c558b990b65d736cf4
#NEWUPSTREAM=8555255f0b426858d8648c6206b70eb906cf4ec7
TOP=`includeparent $NEWUPSTREAM`
NEWMASTER=`convertcommit $TOP`

echo $NEWMASTER > $GIT_DIR/refs/heads/stage1.new
mv $GIT_DIR/refs/heads/stage1.new $GIT_DIR/refs/heads/stage1
rm -f $TEMPFILE $KBUILDASM $KBUILDFILE $MAKEFILE
exit 0
DAY=`date +%w`

if [ "$DAY" = "0" ]; then
    REPACKOPT="-a -d"
fi

git-repack -l $REPACKOPT
git-prune-packed



