# 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).

.EXPORT_ALL_VARIABLES:

# 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
SHAREDIMAGE = libvga.so.1.2.4
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 /lib/libvga.so.1.1.2 /lib/libvga.so.1.1.3 \
	/lib/libvga.so.1.1.4 /lib/libvga.so.1.1.5 /lib/libvga.so.1.1.6 \
	/lib/libvga.so.1.1.7 /lib/libvga.so.1.1.8 /lib/libvga.so.1.1.9 \
	/lib/libvga.so.1.2.0 /lib/libvga.so.1.2.1 /lib/libvga.so.1.2.2 \
	/lib/libvga.so.1.2.3
SVGALIBSHAREDSTUBS = sharedlib/libvga.sa sharedlib/libvgagl.sa
BACKUP   = svgalib123.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

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).
	@if [ -f /usr/include/vga.h ]; then \
		rm -f /usr/include/vga.h /usr/include/vgagl.h; \
		echo Old header files in /usr/include removed.; \
	fi
	@cp src/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 src/mouse/vgamouse.h $(INCLUDEINSTALLDIR)/vgamouse.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vgamouse.h
	@cp src/keyboard/vgakeyboard.h $(INCLUDEINSTALLDIR)/vgakeyboard.h
	@chmod a+r $(INCLUDEINSTALLDIR)/vgakeyboard.h

installsharedlib: sharedlib/$(SHAREDIMAGE) $(SVGALIBSHAREDSTUBS)
	@echo Installing shared library stubs in $(LIBINSTALLDIR).
	@if [ -f /usr/lib/libvga.sa ]; then \
		rm -f /usr/lib/libvga.sa /usr/lib/libvgagl.sa; \
		echo Old stubs in /usr/lib removed.; \
	fi
	@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: src/libvga.a gl/libvgagl.a
	@echo Installing static libraries in $(LIBINSTALLDIR).
	@rm -f /usr/lib/libvga.a
	@cp src/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
	@if [ -f /usr/bin/restorefont ]; then \
		echo Removing old utilities in /usr/bin.; \
		for x in $(UTILS); do rm -f /usr/bin/$$x; done; \
	fi
	@if [ -f /usr/bin/convfont ]; then \
		echo Removing inappropriate utilities in /usr/bin.; \
		rm -f /usr/bin/convfont /usr/bin/setmclk; \
	fi
	@echo Installing textmode utilities in $(UTILINSTALLDIR):
	@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)/$$; 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

src/libvga.a: src/*.c src/*.regs src/*.h # kludge
	# Make static libvga library
	(cd ./src; make libvga.a; cd ..)

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

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

diffs:
	gendiff $(DIFFFILES)

backup: $(BACKUP)

$(BACKUP): $(BACKFILES) dep
	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

clean:
	
	rm -f *~ *.o $(BACKUP) *.uu c_diffs*
	(cd src; make clean; cd ..)
	(cd gl; make clean; cd ..)
	(cd utils; make clean; cd ..)
	(cd demos; make clean; cd ..)
	(cd mach32; make clean; cd ..)
	(cd jump; rm -f *.s jump.params jump.undefs; cd ..)
