# ------------------------------------------------------------------
# This file is part of bzip2/libbzip2, a program and library for
# lossless, block-sorting data compression.
#
# bzip2/libbzip2 version 1.0.5 of 10 December 2007
# Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
#
# Please read the WARNING, DISCLAIMER and PATENTS sections in the 
# README file.
#
# This program is released under the terms of the license contained
# in the file LICENSE.
# ------------------------------------------------------------------

SHELL=/bin/ksh

# To assist in cross-compiling
CC=cc
AR=wlib
LDFLAGS=

CFLAGS=-O


OBJS= blocksort.o  \
      huffman.o    \
      crctable.o   \
      randtable.o  \
      compress.o   \
      decompress.o \
      bzlib.o

all: bz2.lib bzip2 bzip2recover test

bzip2: bz2.lib bzip2.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o bzip2 bzip2.o -L. -lbz2
	./bzip2 --help 2>&1 | usemsg bzip2 -

bzip2recover: bzip2recover.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o bzip2recover bzip2recover.o

bz2.lib: $(OBJS)
	rm -f bz2.lib
	$(AR) -q -n bz2.lib $(OBJS)

check: test
test: bzip2
	@cat words1
	./bzip2 -1  < sample1.ref > sample1.rb2
	./bzip2 -2  < sample2.ref > sample2.rb2
	./bzip2 -3  < sample3.ref > sample3.rb2
	./bzip2 -d  < sample1.bz2 > sample1.tst
	./bzip2 -d  < sample2.bz2 > sample2.tst
	./bzip2 -ds < sample3.bz2 > sample3.tst
	cmp sample1.bz2 sample1.rb2 
	cmp sample2.bz2 sample2.rb2
	cmp sample3.bz2 sample3.rb2
	cmp sample1.tst sample1.ref
	cmp sample2.tst sample2.ref
	cmp sample3.tst sample3.ref
	@cat words3

clean: 
	rm -f *.o bz2.lib bzip2 bzip2recover \
	sample1.rb2 sample2.rb2 sample3.rb2 \
	sample1.tst sample2.tst sample3.tst

blocksort.o: blocksort.c
	$(CC) $(CFLAGS) -c blocksort.c
huffman.o: huffman.c
	$(CC) $(CFLAGS) -c huffman.c
crctable.o: crctable.c
	$(CC) $(CFLAGS) -c crctable.c
randtable.o: randtable.c
	$(CC) $(CFLAGS) -c randtable.c
compress.o: compress.c
	$(CC) $(CFLAGS) -c compress.c
decompress.o: decompress.c
	$(CC) $(CFLAGS) -c decompress.c
bzlib.o: bzlib.c
	$(CC) $(CFLAGS) -c bzlib.c
bzip2.o: bzip2.c
	$(CC) $(CFLAGS) -c bzip2.c
bzip2recover.o: bzip2recover.c
	$(CC) $(CFLAGS) -c bzip2recover.c

dist: all
	cp -c bz2.lib dist/usr/lib/
	cp -c bzlib.h dist/usr/include/
	cp -c manual.* dist/etc/readme/technotes/bzip2/
	cp -c bzip2 bzdiff bzgrep bzmore dist/usr/bin/
	chmod +x dist/usr/bin/*						#*/
	(cd dist/usr/bin; ln -sf bzip2 bzcat; ln -sf bzip2 bunzip2)
	echo -n \#\!/bin/ksh\\n\\nbzip2 -V 2\>\&1 \|head -1\>/etc/version/bzip2\\n\\n > dist/setup
	chmod +x dist/setup
	(cd dist; tar c * | gzip > ../bzip2-qnx4-1.0.5-bin.tar.gz)
	rm -rf dist

distclean: clean
	-rm -rf dist
	-rm bzip2-qnx4-1.0.5-bin.tar.gz
	