# smartupstools Makefile
#
# basic config - defaults should be good for most people

# base path for installing the programs
BASEPATH=/usr/local/ups

# path where the CGI programs get installed (for "make install-cgi")
CGIPATH=/usr/local/ups/cgi-bin

# complete filename for libgd.a
GDPATH=/usr/local/lib/libgd.a

# path to gd include files
GDINC=/usr/local/include

# ========================================
# nothing beyond here should need changing
# ========================================

CC = gcc
CFLAGS = -O -Wall
DFLAGS = -DBASEPATH=\"$(BASEPATH)\"
PROGS = smartups upsd backups backupspro upsc upsd upslog upsmon
CGIPROGS = upsstats.cgi upsimage.cgi multimon.cgi
MISCPATH=$(BASEPATH)/misc
CONFFILES=upsmon.conf

all : $(PROGS) $(CGIPROGS)

smartups : smartups.c upscommon.c smartups.h shared.h
	$(CC) $(CFLAGS) $(DFLAGS) -o smartups smartups.c upscommon.c

backups : backups.c upscommon.c shared.h
	$(CC) $(CFLAGS) $(DFLAGS) -o backups backups.c upscommon.c

backupspro : backupspro.c upscommon.c shared.h
	$(CC) $(CFLAGS) $(DFLAGS) -o backupspro backupspro.c upscommon.c

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

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

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

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

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

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

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

upsimage.cgi : upsimage.c upsfetch.o 
	$(CC) $(CFLAGS) $(DFLAGS) -o upsimage.cgi upsimage.c upsfetch.o $(GDPATH) -lm -I $(GDINC)

clean :
	rm -f *~ *.o $(PROGS) $(CGIPROGS)

install : all install-dirs
	@echo Installing programs in $(BASEPATH)/bin...

	@for f in $(PROGS) ; do \
		echo cp $$f $(BASEPATH)/bin; \
		cp $$f $(BASEPATH)/bin; \
	done

	@echo Installing config files in $(BASEPATH)/etc...
	@for f in $(CONFFILES) ; do \
		if [ -f $(BASEPATH)/etc/$$f ]; then \
			echo "Preserving existing config file: $$f"; \
		else \
			echo cp $$f $(BASEPATH)/etc; \
			cp $$f $(BASEPATH)/etc; \
		fi; \
	done

install-cgi : install-dirs $(CGIPROGS)
	@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)

install-dirs :
	@if (test ! -d $(BASEPATH)) then \
		echo "mkdir -p $(BASEPATH)"; \
		mkdir -p $(BASEPATH) || exit 1; \
	fi
	@if (test ! -d $(BASEPATH)/bin) then \
		echo "mkdir -p $(BASEPATH)/bin"; \
		mkdir -p $(BASEPATH)/bin || exit 1; \
	fi
	@if (test ! -d $(BASEPATH)/etc) then \
		echo "mkdir -p $(BASEPATH)/etc"; \
		mkdir -p $(BASEPATH)/etc || exit 1; \
	fi
	@if (test ! -d $(CGIPATH)) then \
		echo "mkdir -p $(CGIPATH)"; \
		mkdir -p $(CGIPATH) || exit 1; \
	fi
	@if (test ! -d $(STATEPATH)) then \
		echo "mkdir -p $(STATEPATH)"; \
		mkdir -p $(STATEPATH) || exit 1; \
	fi

