# Makefile for svgalib. It's pretty unreadable, but just doing make install
# should be enough. This will install the headerfiles and shared library first
# (which is enough to compile things), after which the static version is
# optionally compiled and installed (if it fails, the shared libraries should
# still work fine).

# Directory where the header files will be installed.
INCLUDEINSTALLDIR = /usr/local/include

# Directory where the shared stubs and static library will be installed.
LIBINSTALLDIR = /usr/local/lib

# Directory where the font and textmode utilities will be installed.
UTILINSTALLDIR = /usr/local/bin

# You might want to add -m386 here if you have a recently installed 
# (486 configured) compiler on a 386. The only real difference is the
# generous alignment padding of function entry-points for the 486.
OPTIMIZE = -fomit-frame-pointer -O2
CFLAGS	 = -Wall $(OPTIMIZE)
LFLAGS   = -N -s

# Uncomment this if want you to compile and install the static libs.
#INSTALLSTATICLIB = installstaticlib

# Comment this out if you don't want to install the shared libs.
INSTALLSHAREDLIB = installsharedlib

# Comment this out if you don't want to compile and install the utilities.
INSTALLUTILS = installutils


# Determine what library (static or shared) we will be linking programs with
ifdef INSTALLSHAREDLIB
  LVGA = -lvga
else
  LVGA    = ./libvga.a
  LVGADEP = libvga.a
endif

AR	= ar
CC	= gcc
LIBS    = $(LVGA) #-ltermcap
UTILS	= restorefont runx restorepalette restoretextmode textmode \
	  savetextmode dumpreg fix132x43
DRIVERS = vgadrv.o cirrus.o et4000.o tvga8900.o oak.o egadrv.o s3.o et3000.o \
	  vgahico.o regextr.o modetab.o mach32.o
MODULES = vga.o \
          vgadraw.o vgaclear.o vgaline.o vgapix.o \
          vgapal.o vgacol.o vgamodesel.o vgamisc.o \
	  vgadump.o vgaconvplanar.o
MOUSE   = mouse/mouse.o
KEYBOARD = keyboard/keyboard.o
SHAREDIMAGE = libvga.so.1.1.2
OBSOLETESHAREDIMAGES = /lib/libvga.so.1.0 /lib/libvga.so.1.0.1 \
	/lib/libvga.so.1.0.2 /lib/libvga.so.1.0.3 /lib/libvga.so.1.0.4 \
	/lib/libvga.so.1.0.5 /lib/libvga.so.1.0.6 /lib/libvga.so.1.0.7 \
	/lib/libvga.so.1.0.8 /lib/libvga.so.1.0.9 /lib/libvga.so.1.0.10 \
	/lib/libvga.so.1.0.11 /lib/libvga.so.1.0.12 /lib/libvga.so.1.1 \
	/lib/libvga.so.1.1.1
SVGALIBSHAREDSTUBS = sharedlib/libvga.sa sharedlib/libvgagl.sa
BACKUP   = svgalib11.tgz
DIFFFILES = Makefile README *.c *.h *.regs utils/*.c utils/Makefile \
	utils/README support/*.c et4000/* gl/makefile
BACKFILES = $(DIFFFILES) et4000/*.exe gendiff

.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<

.o:
	$(CC) $(CFLAGS) $(LFLAGS) -o $* $*.o $(LIBS)
	chmod a+rs,go-w $*


all:	installed demoprogs

libvga.a: $(MODULES) $(MOUSE) $(KEYBOARD) $(DRIVERS)
	rm -f libvga.a
	$(AR) rcs libvga.a $(MODULES) $(MOUSE) $(KEYBOARD) $(DRIVERS)

installed:
	@if [ ! -f $(INCLUDEINSTALLDIR)/vga.h ]; then \
		echo Do make install first.; \
		error; \
	fi
	@if [ ! -f $(LIBINSTALLDIR)/libvga.a ]; then \
		if [ ! -f $(LIBINSTALLDIR)/libvga.sa ]; then \
			echo Do make install first.; \
			error; \
		fi; \
	fi

installheaders:
	@echo Installing header files in $(INCLUDEINSTALLDIR).
	@rm -f /usr/include/vga.h
	@rm -f /usr/include/vgagl.h
	@cp vga.h $(INCLUDEINSTALLDIR)/vga.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vga.h
	@cp gl/vgagl.h $(INCLUDEINSTALLDIR)/vgagl.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vgagl.h
	@cp mouse/vgamouse.h $(INCLUDEINSTALLDIR)/vgamouse.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vgamouse.h
	@cp keyboard/vgakeyboard.h $(INCLUDEINSTALLDIR)/vgakeyboard.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vgakeyboard.h

installsharedlib: sharedlib/$(SHAREDIMAGE) $(SVGALIBSHAREDSTUBS)
	@echo Installing shared library stubs in $(LIBINSTALLDIR).
	@cp $(SVGALIBSHAREDSTUBS) $(LIBINSTALLDIR)
	@chmod a+r $(addprefix $(LIBINSTALLDIR)/,$(notdir $(SVGALIBSHAREDSTUBS)))
	@echo Installing shared library image as /lib/$(SHAREDIMAGE).
#	@echo You might want to delete any old images.
	@rm -f $(OBSOLETESHAREDIMAGES)
	@cp sharedlib/$(SHAREDIMAGE) /lib/$(SHAREDIMAGE)
	@chmod a+rx /lib/$(SHAREDIMAGE)
	@ln -sf /lib/$(SHAREDIMAGE) /lib/libvga.so.1

installstaticlib: libvga.a gl/libvgagl.a
	@echo Installing static libraries in $(LIBINSTALLDIR).
	@rm -f /usr/lib/libvga.a
	@cp libvga.a $(LIBINSTALLDIR)/libvga.a
	@chmod a+r $(LIBINSTALLDIR)/libvga.a
	@rm -f /usr/lib/libvgagl.a
	@cp gl/libvgagl.a $(LIBINSTALLDIR)/libvgagl.a
	@chmod a+r $(LIBINSTALLDIR)/libvgagl.a

installutils: textutils
	@if [ ! -d $(UTILINSTALLDIR) ]; then \
		echo No $(UTILINSTALLDIR) directory, creating it.; \
		mkdir $(UTILINSTALLDIR); \
	fi
	@echo Installing textmode utilities:
	@echo "restorefont:      Save/restore textmode font."
	@cp utils/restorefont $(UTILINSTALLDIR)
	@echo "runx:             Script that runs XFree86 and then restores the textmode font."
	@cp utils/runx $(UTILINSTALLDIR)
	@echo "restorepalette:   Set standard VGA palette."
	@cp utils/restorepalette $(UTILINSTALLDIR)
	@echo "dumpreg:          Write ASCII dump of SVGA registers."
	@cp utils/dumpreg $(UTILINSTALLDIR)
	@echo "restoretextmode:  Save/restore textmode registers."
	@cp utils/restoretextmode $(UTILINSTALLDIR)
	@echo "textmode:         Script that tries to restore textmode."
	@cp utils/textmode $(UTILINSTALLDIR)
	@echo "savetextmode:     Script that saves textmode information used by 'textmode'."
	@cp utils/savetextmode $(UTILINSTALLDIR)
	@echo "fix132x43:        Improve 132x43 textmode."
	@cp utils/fix132x43 $(UTILINSTALLDIR)

installconfig:
	@if [ ! -f /usr/local/lib/libvga.config ]; then \
		echo Installing configuration file in /usr/local/lib.; \
		cp libvga.config /usr/local/lib/libvga.config; \
	fi
	@if [ ! -f /usr/local/lib/libvga.et4000 ]; then \
		echo Installing dynamically loaded ET4000 registers in /usr/local/lib.; \
		cp et4000.regs /usr/local/lib/libvga.et4000; \
	fi
 
install: installheaders $(INSTALLSHAREDLIB) installconfig $(INSTALLUTILS) $(INSTALLSTATICLIB)
	@echo Now run "'make'" to make the test and demo programs in demos/.

uninstall:
	@echo Removing textmode utilities.
	@for x in $(UTILS); do rm -f $(UTILINSTALLDIR)/$$x; done
	@echo Removing shared library stubs \(shared image not removed\).
#	@rm -f /lib/$(SHAREDIMAGE)
#	@rm -f /lib/libvga.so.1
	@rm -f $(LIBINSTALLDIR)/libvga.sa
	@rm -f $(LIBINSTALLDIR)/libvgagl.sa
	@echo Removing static libraries.
	@rm -f $(LIBINSTALLDIR)/libvga.a
	@rm -f $(LIBINSTALLDIR)/libvgagl.a
	@echo Removing header files.
	@rm -f $(INCLUDEINSTALLDIR)/vga.h
	@rm -f $(INCLUDEINSTALLDIR)/vgagl.h
	@rm -f $(INCLUDEINSTALLDIR)/vgamouse.h
	@rm -f $(INCLUDEINSTALLDIR)/vgakeyboard.h

gl/libvgagl.a: gl/*.c	# kludge
	# Make static vgagl library.
	if [ -d gl ]; then (cd ./gl; make libvgagl.a; cd ..); fi

demoprogs:
	if [ -d demos ]; then (cd ./demos; make; cd ..); fi

textutils:
	(cd ./utils; make; cd ..)

diffs:
	gendiff $(DIFFFILES)

backup: $(BACKUP)

$(BACKUP): $(BACKFILES) 
	tar cvf - $(BACKFILES) | gzip -9 >$(BACKUP)

uu: backup diffs
	uuencode <$(BACKUP) $(BACKUP) >$(BACKUP).uu
	gzip -9 -c c_diffs | uuencode c_diffs.z >c_diffs.z.uu

dep:
	rm -f .depend
	make .depend

.depend:
	echo '# Module dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(MODULES)) >>.depend
	echo '# Mouse dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(MOUSE)) >.depend.mouse
	sed 's#^#mouse/#g' <.depend.mouse >>.depend
	rm -f .depend.mouse
	echo '# Keyboard dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(MOUSE)) >.depend.keyboard
	sed 's#^#keyboard/#g' <.depend.keyboard >>.depend
	rm -f .depend.keyboard
	echo '# Driver dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(DRIVERS)) >>.depend

vgatest:	 $(LVGADEP) vgatest.o
dumpreg:	 dumpreg.o
setmclk:	 setmclk.o
fix132x43:	 fix132x43.o
restorepalette:  restorepalette.o
restoretextmode: restoretextmode.o
sptest:		 sptest.o

clean:
	rm -f *~ *.o mouse/*.o keyboard/*.o libvga.a $(BACKUP) *.uu c_diffs*

include .depend


