# powstatd: a configurable UPS monitor.
# Copyright (C) 1999-2001 The University of Iowa
# Author: Alberto Maria Segre
#         segre@cs.uiowa.edu
#         S378 Pappajohn Building
#         The University of Iowa
#         Iowa City, IA  52242-1000
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

#======================================================================
# Local configuration information.

# Select compilation architecture. 
ARCH	= i386
#ARCH 	= alpha

# To compile the secure version of powstatd, first obtain the source
# code for TEA, the Tiny Encryption Algorithm, from
# ftp://vader.eeng.brad.ac.uk/pub/crypto/xtea.c 
# and then make sure the line below is not commented out.
#
# On the other hand, if you intend to run powstatd on a single machine
# (i.e., no slaves), or simply do not wish to bother with encrypted
# communication, make sure the following line is commented out.
CFLGS	+= -DSECURE

# You probably shouldn't have to edit these variable definitions,
# although some inits look for status in /var/run/powerstatus rather
# than /etc/powerstatus. If your init seems to ignore signals from UPS
# monitoring daemon, try switching. Be sure to escape string
# delimiters!
STATUS  = \"/etc/powerstatus\" 
CFGDIR	= /etc
DESTDIR	= /sbin
MANDIR	= /usr/man/man8

# Debian Linux (also SPARC/Solaris and SGI/IRIX)
INITDIR = /etc/init.d
# RedHat and Slackware
#INITDIR = /etc/rc.d/init.d

#======================================================================
# Stop: no user configurable items below this point.
#======================================================================
VERSION	= 1.5.1
DATE = "April 27 2001"

SRC	= powstatd.c 
CRYPT	= xtea.c
ACRYPT	= xtea.alpha.c
INC	= 
LIB	=
BIN	= powstatd

SCR	= powstatd.ok powstatd.fail powstatd.low
INIT	= powstatd.init
CFG	= powstatd.conf

MAN	= powstatd
MSEC	= 8

DOC	= {License,History,QuickStart,ReadMe,ReadMe.sparc,ReadMe.sgi}
ALTCFG	= {powstatd.conf.{apc,cps,tpl,master,slave}}
XTAR	= {Makefile,powstatd.{c,init,ok,fail,dumb,low,man},$(DOC),$(ALTCFG)}
USTAR	= {$(XTAR),$(CRYPT)}

CFLGS	+= -Wall -g -DDEBUG

#======================================================================
# Targets
i386: $(SRC) $(INC) $(CRYPT) Makefile
	$(CC) $(CFLGS) -DSTATFILE=$(STATUS) -DCONFIG="\"$(CFGDIR)/$(CFG)\"" $(SRC) $(CRYPT) -o $(BIN) $(LIB)

alpha: $(SRC) $(INC) $(ACRYPT) Makefile
	$(CC) $(CFLGS) -DALPHA -DSTATFILE=$(STATUS) -DCONFIG="\"$(CFGDIR)/$(CFG)\"" $(SRC) $(ACRYPT) -o $(BIN) $(LIB)

sparc: $(SRC) $(INC) $(ACRYPT) Makefile
	$(CC) $(CFLGS) -DSPARC -DSTATFILE=$(STATUS) -DCONFIG="\"$(CFGDIR)/$(CFG)\"" $(SRC) $(ACRYPT) -lxnet -o $(BIN) $(LIB)

sgi: $(SRC) $(INC) $(ACRYPT) Makefile
	$(CC) $(CFLGS) -DSGI -DSTATFILE=$(STATUS) -DCONFIG="\"$(CFGDIR)/$(CFG)\"" $(SRC) $(ACRYPT) -o $(BIN) $(LIB)

install: $(ARCH) $(MAN).$(MSEC) uninstall
	strip $(BIN)
	cp $(BIN) $(DESTDIR)                      # powstatd
	cp $(SCR) $(DESTDIR)	          	  # powstatd.{ok,fail,low}
	(cd $(DESTDIR); chmod 744 $(BIN) $(SCR))
	cp $(MAN).$(MSEC) $(MANDIR)		  # powstatd.8
	cp $(INIT) $(INITDIR)/$(BIN)        	  # powstatd.init
	chmod 774 $(INITDIR)/$(BIN)
	ln -s $(INITDIR)/$(BIN) $(INITDIR)/../rc0.d/K91$(BIN)
	ln -s $(INITDIR)/$(BIN) $(INITDIR)/../rc1.d/K91$(BIN)
	ln -s $(INITDIR)/$(BIN) $(INITDIR)/../rc3.d/S91$(BIN)
	ln -s $(INITDIR)/$(BIN) $(INITDIR)/../rc5.d/S91$(BIN)
	ln -s $(INITDIR)/$(BIN) $(INITDIR)/../rc6.d/K91$(BIN)

uninstall:
	(cd $(DESTDIR); rm -f $(BIN) $(SCR))
	rm -f $(MANDIR)/$(MAN).$(MSEC)
	rm -f $(INITDIR)/$(BIN)
	rm -f $(INITDIR)/../rc?.d/?91$(BIN)

$(MAN).$(MSEC): powstatd.man
	sed -e 's/%%VERSION%%/\"Version $(VERSION)\"/' -e 's/%%DATE%%/$(DATE)/' < $(MAN).man > $(MAN).$(MSEC)

doc: $(MAN).man $(MAN).$(MSEC)
	gtbl $(MAN).$(MSEC) | groff -man -Tps > $(MAN).ps
#	gtbl $(MAN).$(MSEC) | groff -man -Tdvi > $(MAN).dvi

# A hack for those not having access to TEA. Will create an empty file
# to satisfy make. You'll still need to remove -DSECURE from the CFLGS
# variable.
$(CRYPT):
	echo "/* Obtain xtea.c from ftp://vader.eeng.brad.ac.uk/pub/crypto/xtea.c */" > $(CRYPT)

# This is a major hack. We can't include TEA in the source because of
# export control rules, but we need to modify it for the Alpha, which
# has 64 bit unsigned long ints. Sigh.
$(ACRYPT): $(CRYPT)
	sed -e"s%unsigned long%unsigned int%g" < $(CRYPT) > $(ACRYPT)

config:
	cp $(CFG) $(CFGDIR)	          	  # powstatd.conf

clean:
	rm -f $(BIN) *~ $(BIN)*-$(VERSION).{tgz,tar,tar.Z,sh} $(MAN).{$(MSEC),dvi,ps} $(ACRYPT)

tar:
	@sed -e"s%^CFLGS	+= -DSECURE%#CFLGS	+= -DSECURE%g" < Makefile > Makefile.tmp
	@mv Makefile.tmp Makefile
	tar -cf $(BIN)-$(VERSION).tar -C .. $(BIN)-$(VERSION)/$(XTAR)

tgz:
	@sed -e"s%^CFLGS	+= -DSECURE%#CFLGS	+= -DSECURE%g" < Makefile > Makefile.tmp
	@mv Makefile.tmp Makefile
	tar -czf $(BIN)-$(VERSION).tgz -C .. $(BIN)-$(VERSION)/$(XTAR)

shar:
	@sed -e"s%^CFLGS	+= -DSECURE%#CFLGS	+= -DSECURE%g" < Makefile > Makefile.tmp
	@mv Makefile.tmp Makefile
	shar $(XTAR) > $(BIN)-$(VERSION).sh

dist: 
	@sed -e"s%^#CFLGS	+= -DSECURE%CFLGS	+= -DSECURE%g" < Makefile > Makefile.tmp
	@mv Makefile.tmp Makefile
	tar -czf $(BIN)-crypt-$(VERSION).tgz -C .. $(BIN)-$(VERSION)/$(USTAR)


