# main makefile for the smartupstools package
# Russell Kroll <rkroll@exploits.org>

CC = gcc
CFLAGS = -O2 -Wall -g

# base path - holds binaries and config file
INSTPATH = /usr/local/ups

# misc path - holds upsfetch.o and upsfetch.h if you link it into other things
MISCPATH = $(INSTPATH)/misc

# path for the cgi programs (used for "make install-cgi")
CGIPATH = /usr/local/ups/cgi-bin

# Uncomment this next line if you are running on a BSD type system
# other than BSD/OS and have problems compiling dowall.o...
#BSDHACK = -D__bsdi__

# No more trivial configuration lines beyond this point

VER = -DVERSION=\"0.34\"

DFLAGS = -DBASEPATH=\"$(INSTPATH)\" 
PROGS = upsd upsc upsmon upslog
CGIPROGS = upsstats.cgi upsimage.cgi multimon.cgi

all : $(PROGS) $(CGIPROGS)

upsd : upsd.c readconf.o serial.c
	$(CC) $(CFLAGS) -o upsd upsd.c readconf.o serial.c $(DFLAGS) $(VER)

upsc : upsc.c upsfetch.o
	$(CC) $(CFLAGS) -o upsc upsc.c upsfetch.o $(VER)

upsfetch.o : upsfetch.c
	$(CC) $(CFLAGS) -c upsfetch.c $(VER)

upsmon : upsmon.c upsfetch.o dowall.o readconf.o
	$(CC) $(CFLAGS) -o upsmon upsmon.c upsfetch.o dowall.o readconf.o $(DFLAGS) $(VER)

upslog : upslog.c upsfetch.o readconf.o
	$(CC) $(CFLAGS) -o upslog upslog.c upsfetch.o readconf.o $(DFLAGS) $(VER)

upsstats.cgi : upsstats.c upsfetch.o
	$(CC) $(CFLAGS) -o upsstats.cgi upsstats.c upsfetch.o $(DFLAGS) $(VER)

multimon.cgi : multimon.c upsfetch.o
	$(CC) $(CFLAGS) -o multimon.cgi multimon.c upsfetch.o $(DFLAGS) $(VER)

dowall.o : dowall.c
	$(CC) $(CFLAGS) -c dowall.c $(BSDHACK)

readconf.o : readconf.c
	$(CC) $(CFLAGS) -c readconf.c

gd1.3/libgd.a : gd1.3/gd.c
	cd gd1.3; make; cd ..

upsimage.cgi : upsimage.c gd1.3/libgd.a upsfetch.o
	$(CC) $(CFLAGS) -o upsimage.cgi upsimage.c upsfetch.o gd1.3/libgd.a -lm $(DFLAGS) $(VER)

clean : 
	rm -f *~ *.o $(PROGS) $(CGIPROGS)
	cd gd1.3; make clean; cd ..
	-rm gd1.3/webgif

install-dirs :
	@if (test ! -d $(INSTPATH)) then \
		echo "mkdir $(INSTPATH)"; \
		mkdir $(INSTPATH) || exit 1; \
	fi
	@if (test ! -d $(CGIPATH)) then \
		echo "mkdir $(CGIPATH)"; \
		mkdir $(CGIPATH) || exit 1; \
	fi

install : all install-dirs
	@echo Installing binaries in $(INSTPATH)...

	@for f in $(PROGS); do \
		echo cp $$f $(INSTPATH); \
		cp $$f $(INSTPATH); \
	done

	@-if (test -f "$(INSTPATH)/ups.conf") then \
		echo "Not replacing existing $(INSTPATH)/ups.conf" ; \
	else \
		echo "cp ups.conf $(INSTPATH)"; \
		cp ups.conf $(INSTPATH); \
		echo "chmod 600 $(INSTPATH)/ups.conf"; \
		chmod 600 $(INSTPATH)/ups.conf; \
	fi

	@-if (test -f "$(INSTPATH)/hosts.conf") then \
		echo "Not replacing existing $(INSTPATH)/hosts.conf" ; \
	else \
		echo "cp hosts.conf $(INSTPATH)"; \
		cp hosts.conf $(INSTPATH); \
		echo "chmod 644 $(INSTPATH)/hosts.conf"; \
		chmod 644 $(INSTPATH)/hosts.conf; \
	fi

install-cgi : install-dirs upsstats.cgi upsimage.cgi multimon.cgi
	@for f in $(CGIPROGS); do \
		echo cp $$f $(CGIPATH); \
		cp $$f $(CGIPATH); \
	done

install-misc : upsfetch.o
	@if (test ! -d $(MISCPATH)) then \
		echo "mkdir $(MISCPATH)"; \
		mkdir $(MISCPATH) || exit 1; \
	fi
	cp upsfetch.o upsfetch.h $(MISCPATH)
