VERSION = 1.2

STRIP =  strip
DEBUG = -g
CC =   egcs

OPTS = -O

CFLAGS = $(DEBUG) $(OPTS) -Wall -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE=1 -I /usr/include/rpm -DUSE_ALT_DNS=1
LDFLAGS = $(DEBUG) 
CRYPT=/usr/lib/libcrypt.a

ARCH := $(patsubst i%86,i386,$(shell uname -m))
ifeq (alpha,$(ARCH))
ARCHOBJS2 = kernel.o kbd.o
ARCHOBJS1 = kbd.o
else
ifeq (i386,$(ARCH))
ARCHOBJS2 = lilo.o smb.o kbd.o
ARCHOBJS1 = smb.o kbd.o
else
# SPARC
ARCHOBJS2 = lilo.o
endif
endif

OBJS = net.o log.o nfsmount.o mount_xdr.o windows.o devices.o \
	scsi.o fs.o run.o bootpc.o dns.o kickstart.o lang.o cdrom.o \
	cpio.o otherinsmod.o
OBJS1 = earlymethods.o install.o $(ARCHOBJS1)
OBJS2 = doit.o pkgs.o config.o hd.o latemethods.o install2.o ftp.o \
	upgrade.o hash.o commands.o printercfg.o gettext.o hints.o \
	mkswap.o mtab.o syslog.o ls.o idmap.o fsedit.o $(ARCHOBJS2) 
ALLOBJS = $(OBJS) $(OBJS1) $(OBJS2)
PROGS = install install2 genhdlist dmphdlist

SOURCES = $(subst .o,.c,$(OBJS))
SOURCES1 = $(subst .o,.c,$(OBJS1))
SOURCES2 = $(subst .o,.c,$(OBJS2))
EARLYSECOND = hd.c fsedit.c latemethods.c mtab.c mkswap.c install2.c ftp.c \
	      mkswap.c
LATESECOND = $(filter-out $(EARLYSECOND),$(SOURCES2))
ALLSOURCES = $(SOURCES) $(SOURCES1) $(SOURCES2)

#---------------

INST1LIBS:=-lpopt /usr/lib/libnewt.a /usr/lib/libslang.a /usr/lib/libm.a \
		/usr/lib/libresolv.a modutils/insmod/libmodutils.a \
		modutils/obj/libobj.a modutils/util/libutil.a
INST2LIBS:=-lpopt /usr/lib/libdb.a /usr/lib/libz.a /usr/lib/libslang.a \
	        /usr/lib/libm.a /usr/lib/libresolv.a modutils/insmod/libmodutils.a \
		modutils/obj/libobj.a modutils/util/libutil.a
INST1DEPLIBS=
INST2DEPLIBS=./libfdisk/libfdisk.a

SUBDIRS=modutils libfdisk

ifeq (i386,$(ARCH))
STATIC=-static
INST1DEPLIBS+=./pci-probing/libpciprobe.a ./pcmcia-probing/libpcmciaprobe.a
INST2LIBS:=/usr/lib/libnewt.a $(INST2LIBS)
INST2DEPLIBS+=./pci-probing/libpciprobe.a
SUBDIRS+=pci-probing pcmcia-probing 
else
ifeq (alpha,$(ARCH))
STATIC=
INST1DEPLIBS+=
INST2LIBS:=/usr/lib/libnewt.a $(INST2LIBS)
SUBDIRS+=
else
ifeq (sparc,$(ARCH))
STATIC=
INST1DEPLIBS+=
INST2LIBS:=/usr/lib/libnewt.a $(INST2LIBS)
INST2DEPLIBS+=
SUBDIRS+=
endif
endif
STATIC=
INST2LIBS:=/usr/lib/libnewt.a $(INST2LIBS)
endif

INST1LIBS+=/usr/lib/libz.a -lrpm

ifeq (.depend,$(wildcard .depend))
TARGET=subdirs $(PROGS)
else
TARGET=depend subdirs $(PROGS)
endif

LANGS = cs fi no tr ro sr sk de da se it
TR1 = $(patsubst %,tr/%1.tr,$(LANGS))
TR2 = $(patsubst %,tr/%2.tr,$(LANGS))
TR3 = $(patsubst %,tr/%3.tr,$(LANGS))

all: $(TARGET) 
#translations

translations: install1.tr install2.tr install3.tr

install: $(OBJS) $(OBJS1) $(INST1DEPLIBS)
	gcc $(STATIC) $(LDFLAGS) -o install $^ $(INST1LIBS) $(INST1DEPLIBS)

install2: $(OBJS) $(OBJS2) /usr/lib/librpm.a $(INST2DEPLIBS)
	gcc $(LDFLAGS) -o install2 $^ $(INST2LIBS) $(CRYPT) $(INST2DEPLIBS)

subdirs:
	for d in $(SUBDIRS); do \
	(cd $$d; $(MAKE)) \
	  || case "$(MFLAGS)" in *k*) fail=yes;; *) exit 1;; esac;\
        done && test -z "$$fail"

po/rhinstall.po: $(ALLSOURCES)
	cd po; xgettext --default-domain=rhinstall --add-comments \
		--keyword=_ --keyword=N_ \
		$(patsubst %,../%,$(ALLSOURCES))

tr/%3.tr: po/%.po install3.po
	msgmerge $< install3.po | ./simplemot > $@

tr/%2.tr: po/%.po install2.po
	msgmerge $< install2.po | ./simplemot > $@

tr/%1.tr: po/%.po install1.po 
	msgmerge $< install1.po | ./simplemot > $@

install1.tr: $(TR1) 
	(cd tr; ls *1.tr | cpio --quiet -Hcrc -o | gzip -9) > $@

install2.tr: $(TR2) 
	(cd tr; ls *2.tr | cpio --quiet -Hcrc -o | gzip -9) > $@

install3.tr: $(TR3) 
	(cd tr; ls *3.tr | cpio --quiet -Hcrc -o | gzip -9) > $@

.PHONY: update-po
update-po: po/rhinstall.po
	@for n in $(LANGS); do \
	    echo msgmerge po/$$n.po po/rhinstall.po ;\
	    msgmerge po/$$n.po po/rhinstall.po > po/$$n.pot && \
	    mv -f po/$$n.pot po/$$n.po ;\
	done


# install1.po and install2.po aren't strictally necessary, but they let
# us move the install2 specific translations off the first disk. 
#
# We split into an install3 as well for now. It makes install2.tr smaller,
# which lets it fit onto the supp disk easier <sigh>, and install3.tr is
# brought over frm the installation media. We should get rid of the install3
# tr hack altogether, and completely setup the installation media in the
# first stage. 
install1.po: $(SOURCES) $(SOURCES1)
	xgettext --default-domain=install1 --add-comments \
		--keyword=_ --keyword=N_ $(SOURCES) $(SOURCES1)

install2.po: $(EARLYSECOND)
	xgettext --default-domain=install2 --add-comments \
		--keyword=_ --keyword=N_ $(EARLYSECOND)

install3.po: $(LATESECOND)
	xgettext --default-domain=install3 --add-comments \
		--keyword=_ --keyword=N_ $(LATESECOND)

ftp.o: ftp.c
	$(CC) $(CFLAGS) -DHAVE_CONFIG_H=1 -DHAVE_ALLOCA_H=1 -DHAVE_NETINET_IN_SYSTM_H=1 -c $<

genhdlist: /usr/lib/librpm.a gettext.o genhdlist.o
	gcc -o genhdlist genhdlist.o -lrpm -lz

dmphdlist: /usr/lib/librpm.a gettext.o dmphdlist.o
	gcc -o dmphdlist dmphdlist.o -lrpm -lz

ugtest: ugtest.o log.o
	gcc $(LDFLAGS) -o ugtest $^ $(INST2LIBS) -lrpm -ldb -lz

clean: 
	rm -f *.o .depend

	# get subdirectories as well
	for d in $(SUBDIRS); do \
	(cd $$d; $(MAKE) clean) \
	  || case "$(MFLAGS)" in *k*) fail=yes;; *) exit 1;; esac;\
        done && test -z "$$fail"

ifeq (i386,$(ARCH))
installit: translations
	rm -f ../../../RedHat/instimage/usr/bin/install2
	cp install2 ../../../RedHat/instimage/usr/bin
	cp install2 install2.foo
	strip install2.foo
	bzip2 -9 < install2.foo > ../trees/supp/usr/bin/install2
	rm -f ../../../RedHat/instimage/usr/etc/install2.tr
	cp install2.tr ../../../RedHat/instimage/usr/etc
	cp install2.tr ../trees/supp/usr/etc/install2.tr
	rm -f ../../../RedHat/base/install3.tr
	cp install3.tr ../../../RedHat/base/install3.tr
	install -s install ../trees/initrd/bin/install
	cp -f install1.tr ..//trees/initrd/etc/install1.tr
else
ifeq (alpha,$(ARCH))
installit: translations
	rm -f ../trees/boot/bin/install
	rm -f ../trees/boot/bin/install2
	rm -f ../trees/boot/etc/install1.tr
	rm -f ../trees/boot/etc/install2.tr
	install -s install ../trees/boot/bin/install
	install -s install2 ../trees/boot/bin/install2
	install install1.tr ../trees/boot/etc/install1.tr
	install install2.tr ../trees/boot/etc/install2.tr
	rm -f ../../../RedHat/base/install3.tr
	install install3.tr ../../../RedHat/base/install3.tr
else
ifeq (sparc,$(ARCH))
installit: translations
	rm -f ../trees/ramdisk/bin/install
	rm -f ../trees/ramdisk/bin/install2
	rm -f ../trees/ramdisk/etc/install1.tr
	rm -f ../trees/ramdisk/etc/install2.tr
	install -s install ../trees/ramdisk/bin/install
	install -s install2 ../trees/ramdisk/bin/install2
	install install1.tr ../trees/ramdisk/etc/install1.tr
	install install2.tr ../trees/ramdisk/etc/install2.tr
	rm -f ../../../RedHat/base/install3.tr
	install install3.tr ../../../RedHat/base/install3.tr
endif
endif
endif

depend:
	$(CPP) $(CFLAGS) -M $(ALLSOURCES) > .depend

co:
	co RCS/*,v

ifeq (.depend,$(wildcard .depend))
include .depend
endif                                           
