#!/bin/bash

#
# Warning: this script is *REALLY* dirty, but it took me a few
# minutes to get what I needed. Don't complain if it destroys
# your computer, it's your problem, not mine.
#

# This one only works on patch-o-matig-ng directories after 2005/06

KERNEL_DIR=${KERNEL_DIR:-/usr/src/linux-2.4-bk-pomng}
IPTABLES_DIR=${IPTABLES_DIR:-../iptables-1.2.10}

# some variables
list=( )
missing=0
do_list=0

if [ "$1" = "-n" ]; then
    families=( $(grep -h ^Repo patchlets/*/info|cut -f2 -d:|sort -bu) )
    for family in ${families[@]}; do
        echo "####### $family #######"
        grep "^Repository: $family" patchlets/*/info|cut -f2 -d/|while read; do
            grep -q "^[-+?!] $REPLY" $0 || echo "? $REPLY" 
        done
    done
    exit 0
elif [ "$1" = "-l" ]; then
  do_list=1
elif [ "$1" != "-a" ]; then
    echo "Usage: $0 [ -n | -a | -l ]"
    echo "  -n shows for each family the new lines to add for the current repository."
    echo "  -l lists all the patches marked with '+' in the script."
    echo "  -a applies all the patches marked with '+' in the script."
    echo "Using KERNEL_DIR=${KERNEL_DIR} and IPTABLES_DIR=${IPTABLES_DIR}"
    echo "This needs to be started from the patch-o-matic-ng directory."
    echo
    exit 1
fi

while read tag name comment ; do
    if [ "$tag" = "-" ]; then
	echo "Excluding $name"
    elif [ "$tag" = "+" ]; then
	if [ ! -e "patchlets/$name" ]; then
            echo "Missing patch: $name"
            ((missing++))
	else
	    echo "Including $name"
            list=( ${list[@]} $name )
	fi
    fi
done << EOF
# The first char indicates what to do with the patch
# '-' means 'do not apply it'
# '+' means 'apply it'
# '?' means 'not tested yet'
# '!' means 'does not apply'
# everything else is ignored.


####### merged ########
- expect-optimize
- proc-no-internal-targets
- ip_nf_assert-fix
- ipt_helper-invert-fix
- orphaned-expect-fix
- helper-locking_fix
- expect-evict-order
- nf_reset
- proc_net_conntrack-permissions
- mangle-reroute
- owner-broken
- unclean_fragment-fix

###### missing ######
- linuxdoc
- helper-reassign-fix
- expect-slab-cache

####### pending #######
- init_conntrack-optimize : check 20050305 and fix the dependencies
! nf-log
? CLASSIFY_more-hooks
? amanda_offset-fix
? conntrack-acct
? conntrack-cacheline-opt
? conntrack_error-api
? early-drop-norandom
? ip_nat_helper_static
? module_parm

####### base #######
- HOPLIMIT
+ IPV4OPTSSTRIP
+ NETLINK
+ NETMAP
+ REJECT	# broken, needs additional patch
+ SAME
+ TTL
+ connlimit
? dstlimit
? fuzzy
+ iprange
+ ipv4options
+ mport
+ nth
+ osf
+ pool
+ psd
? quota
+ random
? raw
? realm
? sctp
+ set
+ time
+ u32
- ip_conntrack_count
- hashlimit


####### extra #######
+ CLASSIFY
- CONNMARK
- IPMARK
+ ROUTE
+ TARPIT
- TCPLAG
- TRACE
+ XOR
+ addrtype
- childlevel
+ condition
? connbytes
? conntrack-seqfile
- ctstat
+ cuseeme-nat
+ eggdrop-conntrack
+ h323-conntrack-nat
? iptables-loopcheck-speedup
- layer2-hooks
+ mms-conntrack-nat
! netfilter-docbook
- nfnetlink-ctnetlink-0.13
+ owner-socketlookup
- owner-supgids
+ ownercmd
? pptp-conntrack-nat
+ quake3-conntrack-nat
+ rpc
+ rsh
+ rtsp-conntrack
+ string
+ talk-conntrack-nat
! tcp-window-tracking
+ account
? ipt_helper-any
? policy
- nat-reservations
? connrate
? dropped-table
? ipsec-01-output-hooks
? ipsec-02-input-hooks
? ipsec-03-policy-lookup
? ipsec-04-policy-checks
? sctp-conntrack-nat
+ goto
? MARK-operations
? comment
? conntrack_protocol-arrays
? tproxy
? conntrack_memsave
? directx8-conntrack-nat
? ACCOUNT
? ip_queue_vwmark
? nf_conntrack
? xover
? CLUSTERIP
+ geoip
+ msnp-conntrack-nat
- ULOG

####### kernel 2.6 only ########
- expire
- conntrack-event-api
- ctnetlink
- nfnetlink_queue
- sip-conntrack-nat
- conntrack_ftp-optimize
- ip_queue_nonlinear_skbs
- conntrack_arefcount
- conntrack_locking
- conntrack_nonat

EOF

if [ $missing -gt 0 ]; then
    echo
    echo "Some patches are missing. Please fix '${0##*/}'."
    exit 2
fi

for patch in ${list[@]}; do
    exist=0
    for f in linux linux-2.4 linux.patch linux-2.4.patch; do
        [ -e "patchlets/$patch/$f" ] && exist=1
    done
    if [ $exist -eq 0 ]; then
        echo "WARNING! no linux 2.4 code for patch '$patch'"
    fi
done

if [ $do_list -gt 0 ]; then
    exit 0
fi

echo "Now starting runme..."
./runme --kernel-path=$KERNEL_DIR --iptables-path=$IPTABLES_DIR --batch ${list[@]}
exit 0

