# makefile for upsd/upsc/upsmon/upsstats/upsimage
# Copyright (C) 1997, Russell Kroll

CC = gcc
CFLAGS = -O2 -Wall

# path for the binaries and config file 
INSTPATH = /usr/local/ups

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

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

all : $(PROGS)

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

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

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

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

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

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

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

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

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

clean : 
	rm -f *~ *.o upsd upsc upsmon upsfetch.o upsstats.cgi upsimage.cgi
	cd gd1.2; make clean; cd ..
	-rm gd1.2/webgif

install-dirs :
	@if (test ! -d $(INSTPATH)) then \
		echo "mkdir $(INSTPATH)"; \
		mkdir $(INSTPATH) || 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

install-cgi : upsstats.cgi upsimage.cgi
	cp upsstats.cgi $(CGIPATH)
	cp upsimage.cgi $(CGIPATH)

