#
# Makefile for SVGA textmode manipulation program
#
# Authors : Koen Gadeyne (kmg@barco.be)
#           Linux/Alpha port: David Mosberger-Tang <davidm@cs.arizona.edu>
#           DOS port of grabmode: Kenneth Albanowski <kjahds@kjahds.com>
#           All other DOS porting: - Stephen Lee (sl14@crux....
#                                  - and DJGPP 2.0
#

ARCH=$(shell arch)
CFLAGS_alpha =
CFLAGS_i386  = -pipe
CFLAGS_i486  = $(CFLAGS_i386) -m486
CFLAGS_i586  = $(CFLAGS_i386)
LDFLAGS_alpha =
LDFLAGS_i386  = -s -N
LDFLAGS_i486  = $(LDFLAGS_i386)
LDFLAGS_i586  = $(LDFLAGS_i386)

CDEBUGFLAGS = 

CPPFLAGS = $(CONF_FLAGS) $(DEF_CONF) $(DEF_CLOCKCONF) $(STM_VERSION)
CFLAGS = -Wall -O2 $(CDEBUGFLAGS) $(CFLAGS_$(ARCH)) -IXFREE/include
LDFLAGS = $(LDFLAGS_$(ARCH))

export CFLAGS

# installation path for SVGATextMode
INSTBINDIR = /usr/sbin

# installation path for TextConfig. This is also the path that will be
# compiled into SVGATextMode.
INSTCONFDIR = /etc

# installation path for the manual files
INSTMANDIR = /usr/man

# misc configuration flags.
#
# possible flags are:
#
#     -DRUN_SECURE         Renounce superuser rights immediately after
#                          getting permission for VGA registers, so the
#                          external programs are NOT run as root.
#                          This is only useful if you set the SUID bits on.
#
#     -DNO_DEBUG           Don't include debugging code (for "-d" option)
#                          in all of the tools.
#
#     -DNO_RESIZE          Do not include screen resizing code. This will prohibit
#                          SVGATextMode from resizing the screen, leaving only screen
#                          enhancement functionality. Kernel versions
#                          prior to 1.1.54 NEED this to compile and run without errors.
#
#     -DDOSYNCRESET        Will do a synchronous reset of the timing sequencer before
#                          changing clocks. This seems to crash some ET4000's, although
#                          the data books recommend it...
#
#     -DS3_HS_TEXT         Enable S3 high speed text font writing
#                          This doesn't work yet! 
#                          This mode will only allow font changing from WITHIN SVGATextMode!
#
#     -DDOS                Compile for DOS, using a DOS compiler. DJGPP 2.0 recommended
#                          You MUST (!) use DJGPP 2.0 (released Sept 1995) for this!!!
#

CONF_FLAGS =

############ END ## OF ## USER ## CONFIGURATION #################################


VERSION_NUM = 1.0


STM_VERSION = -DVERSION=\"$(VERSION_NUM)\"

CONFIGFILE= $(INSTCONFDIR)/TextConfig
CLOCKCONFIGFILE= $(INSTCONFDIR)/ClockConfig
DEF_CONF = -DCONFIGFILE=\"$(CONFIGFILE)\"
DEF_CLOCKCONF = -DCLOCK_CONF_FILE=\"$(CLOCKCONFIGFILE)\"
XFREELIBS = XFREE/libxf86_hw.a
XFREELINK = -LXFREE -lxf86_hw

USERPROGS = SVGATextMode clockprobe grabmode
HACKPROGS = setVGAreg getVGAreg setpalette getpalette set80 ClockProg 
#probeclocks

DOSPROGS = grabmode.exe stm.exe clkprobe.exe
DOSHACKPROGS = clkprog.exe setvgarg.exe getvgarg.exe setpal.exe getpal.exe set80

default: user

all: user hack

$(XFREELIBS)::
	$(MAKE) -C XFREE all


user: $(USERPROGS)

hack: $(HACKPROGS)

dos: $(DOSPROGS)

doshack: $(DOSHACKPROGS)

#
# Object files
#

STM_OBJECTS = ttyresize.o messages.o vga_prg.o setclock.o configfile.o SVGATextMode.o \
              parse_clock.o parse_modeline.o parse_opt.o validate.o run_extprog.o \
              parse_font.o wait_vsync.o clockchip.o std_clock.o

CP_OBJECTS = ClockProg.o messages.o vga_prg.o setclock.o configfile.o \
             parse_clock.o parse_modeline.o parse_opt.o validate.o run_extprog.o \
             wait_vsync.o clockchip.o std_clock.o
             
SET80_OBJECTS = set80.o vga_prg.o messages.o ttyresize.o

VGAREG_OBJECTS = setVGAreg.o vga_prg.o configfile.o messages.o

PAL_OBJECTS = setpalette.o vga_prg.o configfile.o messages.o

GRAB_OBJECTS = grabmode.o modedata.o probe.o wait_vsync.o vga_prg.o messages.o

#PCLKS_OBJECTS = probeclocks.o messages.o vga_prg.o setclock.o configfile.o \
#                parse_clock.o parse_opt.o run_extprog.o clockchip.o \
#                std_clock.o modedata.o probe.o wait_vsync.o
             

#
# UNIX targets
#

SVGATextMode: $(STM_OBJECTS) $(XFREELIBS)
	$(CC) $(LDFLAGS) $(STM_OBJECTS) $(XFREELINK) -o $@
	
ClockProg: $(CP_OBJECTS) $(XFREELIBS)
	$(CC) $(LDFLAGS) $(CP_OBJECTS) $(XFREELINK) -o $@
	
#probeclocks: $(PCLKS_OBJECTS) $(XFREELIBS)
#	$(CC) $(LDFLAGS) $(PCLKS_OBJECTS) $(XFREELINK) -o $@
	
set80: $(SET80_OBJECTS)
	$(CC) $(LDFLAGS) $(SET80_OBJECTS) -o $@
	
setVGAreg: $(VGAREG_OBJECTS)
	$(CC) $(LDFLAGS) $(VGAREG_OBJECTS) -o $@
	
getVGAreg: setVGAreg
	ln -sf setVGAreg getVGAreg	
	
setpalette: $(PAL_OBJECTS)
	$(CC) $(LDFLAGS) $(PAL_OBJECTS) -o $@
	
getpalette: setpalette
	ln -sf setpalette getpalette	
	
clockprobe: grabmode
	ln -sf grabmode clockprobe

grabmode: $(GRAB_OBJECTS)
	$(CC) $(LDFLAGS) $(GRAB_OBJECTS) -o $@

#
# DOS targets
#

grabmode.exe: $(GRAB_OBJECTS)
	$(CC) $(CFLAGS) $(GRAB_OBJECTS) -o grabmode
	strip grabmode
	stubify grabmode

clkprobe.exe: grabmode.exe
	copy grabmode.exe clkprobe.exe

stm.exe: $(STM_OBJECTS) $(XFREELIBS)
	$(CC) $(CFLAGS) $(STM_OBJECTS) $(XFREELINK) -o stm
	strip stm
	stubify stm

clkprog.exe: $(CP_OBJECTS) $(XFREELIBS)
	$(CC) $(CFLAGS) $(CP_OBJECTS) $(XFREELINK) -o clkprog
	strip clkprog
	stubify clkprog

setpal.exe: $(PAL_OBJECTS)
	$(CC) $(CFLAGS) $(PAL_OBJECTS) -o setpal
	strip setpal
	stubify setpal

getpal.exe: setpal.exe
	copy setpal.exe getpal.exe

setvgarg.exe: $(VGAREG_OBJECTS)
	$(CC) $(CFLAGS) $(VGAREG_OBJECTS) -o setvgarg
	strip setvgarg
	stubify setvgarg

getvgarg.exe: setvgarg.exe
	copy setvgarg.exe getvgarg.exe

set80.exe: $(SET80_OBJECTS)
	$(CC) $(CFLAGS) $(SET80_OBJECTS) -o set80 
	strip set80
	stubify set80


#
# install/clean targets
#

bininstall:
	if [ ! -x SVGATextMode ]; then \
		make SVGATextMode; \
	fi
	if [ ! -x grabmode ]; then \
		make grabmode; \
	fi
	install -m 755 SVGATextMode $(INSTBINDIR)
	install -m 755 grabmode $(INSTBINDIR)
	ln -sf $(INSTBINDIR)/grabmode $(INSTBINDIR)/clockprobe
	
man-install maninstall:
	install -m 644 doc/SVGATextMode.man $(INSTMANDIR)/man8/SVGATextMode.8
	install -m 644 doc/TextConfig.man $(INSTMANDIR)/man5/TextConfig.5
	install -m 644 doc/grabmode.man $(INSTMANDIR)/man8/grabmode.8
	ln -sf $(INSTMANDIR)/man8/grabmode.8 $(INSTMANDIR)/man8/clockprobe.8
	
mangz-install mangzinstall compressedmaninstall: man-install
	gzip -v -f -9 $(INSTMANDIR)/man8/SVGATextMode.8
	gzip -v -f -9 $(INSTMANDIR)/man5/TextConfig.5
	gzip -v -f -9 $(INSTMANDIR)/man8/grabmode.8
	ln -sf $(INSTMANDIR)/man8/grabmode.8.gz $(INSTMANDIR)/man8/clockprobe.8.gz
	rm -f $(INSTMANDIR)/man8/clockprobe.8
	
install: bininstall
	@echo
	@echo "NOTE:"
	@if [ ! -f ${CONFIGFILE} ]; then \
		echo "	You must also have an \`$(INSTCONFDIR)/TextConfig' file."; \
		echo "	There's an example of such a file in the main SVGATextMode directory."; \
		echo "	(or type \`make newinstall' to install the TextConfig file together with the rest)"; \
	else \
		echo "	You seem to have an \`$(INSTCONFDIR)/TextConfig' file already."; \
		echo "	Consider upgrading it with the one in this package to"; \
		echo "	take full advantage of all the new (if any) features."; \
	fi
	@echo
	@echo "	To install the manual pages, type \`make man-install',"
	@echo "	or \`make mangz-install' to install compressed man-pages."
	@echo

newinstall: bininstall	
	install -m 644 TextConfig $(INSTCONFDIR)/TextConfig


backup:
	( cd ..; tar cvzf SVGATextMode-${VERSION_NUM}.tar.gz $(shell basename `pwd`); cd $(shell pwd))


clean:
	$(RM) *~ *.o *.bak *.orig *.rej
	$(MAKE) -C XFREE clean

pristine: mrproper

distclean: clean
	$(RM) -f $(HACKPROGS) DEADJOE 
	$(RM) -f `find . -name "*~"`

mrproper: distclean
	$(RM) -f $(USERPROGS)
	$(MAKE) -C XFREE mrproper



#
# dependencies
#

depend dep: .depend

.depend::
	gcc -MM *.c >.depend
	$(MAKE) -C XFREE depend


include .depend


