# 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 compile and install the utilities.
INSTALLUTILS = installutils


AR	= ar
CC	= gcc
LIBS    = #-ltermcap
PROGS   = vgatest setmclk
UTILS	= restorefont runx restorepalette restoretextmode textmode \
	  savetextmode dumpreg fix132x43
PROGS_O	= vgatest.o # setmclk.o  
DRIVERS = vgadrv.o cirrus.o et4000.o tvga8900.o oak.o egadrv.o s3.o \
	  vgahico.o regextr.o modetab.o
MODULES = vga.o \
          vgadraw.o vgaclear.o vgaline.o vgapix.o \
          vgapal.o vgacol.o vgamodesel.o vgamisc.o \
	  vgadump.o mouse/mouse.o
SHAREDIMAGE = libvga.so.1.0.5
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
SVGALIBSHAREDSTUBS = sharedlib/libvga.sa sharedlib/libvgagl.sa
BACKUP   = svgalib095.tgz
DIFFFILES = Makefile README *.c *.h *.regs support/*.c et4000/* gl/makefile
BACKFILES = $(DIFFFILES) support/*.exe gendiff

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

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


all:	installed $(PROGS) vgaglprogs

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

installed:
	@if [ ! -f $(INCLUDEINSTALLDIR)/vga.h ]; then \
		echo Do make install first.; \
		error; \
	fi
	@if [ ! -f $(LIBINSTALLDIR)/libvga.sa ]; then \
		echo Do make install first.; \
		error; \
	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

installsharedlib: sharedlib/$(SHAREDIMAGE) $(SVGALIBSHAREDSTUBS)
	@echo Installing shared library stubs in $(LIBINSTALLDIR).
	@cp $(SVGALIBSHAREDSTUBS) $(LIBINSTALLDIR)
	@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)
	@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)

uninstall:
	@echo Removing textmode utilities.
	@for x in $(UTILS); do rm -f $(UTILINSTALLDIR)/$$x; done
	@echo Removing shared library.
	@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

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

vgaglprogs:
	if [ -d gl ]; then (cd ./gl; make progs; 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 '# Program dependencies' >.depend
	gcc -MM $(patsubst %.o,%.c,$(PROGS_O)) >>.depend
	echo '# Module dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(MODULES)) >>.depend
	echo '# Driver dependencies' >>.depend
	gcc -MM $(patsubst %.o,%.c,$(DRIVERS)) >>.depend

vgatest:	 vgatest.o
dumpreg:	 dumpreg.o
setmclk:	 setmclk.o
fix132x43:	 fix132x43.o
restorepalette:  restorepalette.o
restoretextmode: restoretextmode.o
sptest:		 sptest.o

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

cleanbin:
	rm -f $(PROGS)

include .depend

