#!/bin/bash
cd /usr/src/bzip2-*/ ||exit

patch -p 1 <../bzip2-1.0.3-bzgrep_security-1.patch

#my own optimization patch (?):
patch -p 1 <<"EOF"
diff -Naur bzip2-1.0.3.orig/Makefile bzip2-1.0.3/Makefile
--- bzip2-1.0.3.orig/Makefile	2005-04-09 15:05:41.000000000 +0200
+++ bzip2-1.0.3/Makefile	2005-04-09 15:07:56.000000000 +0200
@@ -8,7 +8,7 @@
 LDFLAGS=
 
 BIGFILES=-D_FILE_OFFSET_BITS=64
-CFLAGS=-Wall -Winline -O -g $(BIGFILES)
+CFLAGS=-Wall -Winline $(MYCFLAGS) $(BIGFILES)
 
 # Where you want it installed when you do 'make install'
 PREFIX=/usr
diff -Naur bzip2-1.0.3.orig/Makefile-libbz2_so bzip2-1.0.3/Makefile-libbz2_so
--- bzip2-1.0.3.orig/Makefile-libbz2_so	2005-04-09 15:05:41.000000000 +0200
+++ bzip2-1.0.3/Makefile-libbz2_so	2005-04-09 15:06:46.000000000 +0200
@@ -9,7 +9,7 @@
 SHELL=/bin/sh
 CC=gcc
 BIGFILES=-D_FILE_OFFSET_BITS=64
-CFLAGS=-fpic -fPIC -Wall -Winline -O -g
+CFLAGS=-fpic -fPIC -Wall -Winline $(MYCFLAGS)
 
 OBJS= blocksort.o  \
       huffman.o    \
EOF




#creates a dynamic libbz2.so library and link the bzip2 utilities against it:
export MYCFLAGS=$CFLAGS && make -f Makefile-libbz2_so &&
make clean &&
make &&
#delete the stuff make install cant overwrite:
rm -f /usr/bin/bz{{e,f}grep,less,more,cmp} &&
make install &&
cp bzip2-shared /bin/bzip2 &&
cp -a libbz2.so* /lib &&
ln -sf ../../lib/libbz2.so.1.0 /usr/lib/libbz2.so &&
rm /usr/bin/{bunzip2,bzcat,bzip2} &&
ln -sf bzip2 /bin/bunzip2 &&
ln -sf bzip2 /bin/bzcat
