#!/bin/bash

## SOURCES
BINUTILS=binutils-2_18-branch
GCC=gcc-4_1-branch
GNUMACH=gnumach-1-branch
GLIBC=glibc-2_7-branch
MIG=mig
HURD=hurd

## Patch directories
PATCH0_DIR=patch0
PATCH1_DIR=patch1

## Patches
PATCH1=( 0003-2007-09-13-H.J.-Lu-hongjiu.lu-intel.com.patch \
	0005-Hurd-specific-kernel-features.h.patch \
	0007-2007-10-05-version-of-stat.patch.patch \
	0008-r2425-of-debian-patches-hurd-i386-local-atomic-no-mu.patch \
	0010-r2425-of-debian-patches-hurd-i386-local-gscope.diff.patch \
	0012-r2425-of-debian-patches-hurd-i386-local-no-strerror_.patch \
	0013-r2626-of-debian-patches-hurd-i386-local-tls-support.patch \
	0014-r2591-of-debian-patches-hurd-i386-local-tls.diff.patch \
	0015-r2630-of-debian-patches-hurd-i386-submitted-libc_onc.patch \
	0016-Include-stdint.h.patch \
	0017-r2598-of-debian-patches-any-local-stdio-lock.diff.patch \
	0018-r2650-of-debian-patches-hurd-i386-submitted-strtoul.patch \
	0019-2007-11-12-Aurelien-Jarno-aurelien-aurel32.net-Tho.patch \
	0020-r2656-of-debian-patches-any-submitted-sched_h.diff.patch \
	0022-2007-11-18-Roland-McGrath-roland-frob.com.patch )

PATCH0=( 0009-2007-07-22-version-of-init-first.c_vs._GCC_4.1.patch.patch \
	0011-2007-02-08-version-of-resolv_res_send.c.patch.patch )


apply_glibc_patch() {
    get_glibc_patches
    apply_glibc_patches
}

get_glibc_patches() {
    # create patch directories
    mkdir ${GLIBC_DIR}/${PATCH0_DIR}
    mkdir ${GLIBC_DIR}/${PATCH1_DIR}

    # get patch0 patches
    index=0
    while [ $index -lt ${#PATCH0[@]} ]; do
	wget -r -np -nd -P ${GLIBC_DIR}/${PATCH0_DIR} http://www.schwinge.homeip.net/~thomas/tmp/glibc-patches/${PATCH0[index]}
	let "index = $index + 1"
    done

    # get patch1 patches
    index=0
    while [ $index -lt ${#PATCH1[@]} ]; do
	wget -r -np -nd -P ${GLIBC_DIR}/${PATCH1_DIR} http://www.schwinge.homeip.net/~thomas/tmp/glibc-patches/${PATCH1[index]}
	let "index = $index + 1"
    done
}

apply_glibc_patches() {
    # enter directory
    index=0
    cd ${GLIBC_DIR}

    # apply patch0 patches
    while [ $index -lt ${#PATCH0[@]} ]; do
	patch -p0 < ${PATCH0_DIR}/${PATCH0[index]}
	let "index = $index + 1"
    done

    index=0
    # apply patch1 patches
    while [ $index -lt ${#PATCH1[@]} ]; do
	patch -p1 < ${PATCH1_DIR}/${PATCH1[index]}
	let "index = $index + 1"
    done
}
