# PVTool - Copyright 1996 Richard W.E. Furse (mainly)

###############################################################################

# The system uses two main directories, one to hold the program (the
# BINARY_DIRECTORY, which can be put on your path) and one to hold
# various library files required by the program. On compilation this
# library directory is hard-coded into the program and changing it
# will require a recompile. Sorry about this - if I've time I'll put
# in some checking of environment variables.
#
# You should change these directory names if they don't work for
# you. They work for me - I log in as root and run "make install". If
# don't have root access you'll definitely need to change these
# directories. If this file is in /whatever/pvtool/src then you may
# want to to make the directories /whatever/pvtool/bin and
# /whatever/pvtool/lib. This is fine but you should put the former on
# your path.
#
# Note that these directory names should not include the ending '/'
# character and should be full paths, not relative.

BINARY_DIRECTORY	 = /usr/local/bin
PVTOOL_LIBRARY_DIRECTORY = /usr/local/lib/pvtool

###############################################################################

# The rest of this is fairly standard material, at least on UNIX -
# hopefully nothing needs changing but it might be worth having a
# quick look.

PVTOOL_VERSION	= v1.0

SWITCHES	= -DPVTOOL_LIBRARY_DIRECTORY='"${PVTOOL_LIBRARY_DIRECTORY}/"'
DEFINES		= -DVERSION_STRING='"${PVTOOL_VERSION}"'
CFLAGS		= -O2
CC		= cc ${SWITCHES} ${DEFINES}
AR		= ar
RANLIB		= ${AR} s
BINARY		= ${BINARY_DIRECTORY}/pvtool
COMPILER	= ${BINARY_DIRECTORY}/pvcom
ARCHIVE_NAME	= `date '+../backup/pvtool.%d.%m.%y.tgz'`
BINARY_ACCESS	= chmod 711
LIBRARY_ACCESS	= chmod 644
TMP_DIR		= /tmp
PERL		= perl
MAIL		= Mail -s "PVTool Registration"

LIBRARY		= ${PVTOOL_LIBRARY_DIRECTORY}/libpv.a
OBJECTS		= pvoc.o pvlib.o specenv.o
SOURCES		= pvoc.c pvlib.c specenv.c

###############################################################################

# Modes for make:

# What will be built just with "make":
# ------------------------------------
all:		always
		@echo "----------------------------"
		@echo "BUILDING ALL SYSTEM ELEMENTS"
		@echo "----------------------------"
		${MAKE} ${PVTOOL_LIBRARY_DIRECTORY}
		${MAKE} components
		${MAKE} clean
		${MAKE} scripts
		${MAKE} register

# Use this (ie type "make install") to build an archive of the system:
# --------------------------------------------------------------------
archive:	PVTool.tgz

# Use this to store a dated backup of the system:
# -----------------------------------------------
backup:		PVTool.tgz
		@echo "--------------------"
		@echo "STORING DATED BACKUP"
		@echo "--------------------"
		cp PVTool.tgz ${ARCHIVE_NAME}

# Use this to update the first line of each file in ../scripts:
# -------------------------------------------------------------
correct:	always
		@echo "---------------------------------"
		@echo "CORRECTING PVTOOL PATH IN SCRIPTS"
		@echo "---------------------------------"
		correct.sh ${BINARY}
# This shell script changes the #!/ line of each file in the script/
# directory to point to the new executable.

# Use this to correct the contents of ../scripts and compile into ../bin:
# -----------------------------------------------------------------------
scripts:	always correct
		@echo "-----------------"
		@echo "COMPILING SCRIPTS"
		@echo "-----------------"
		@echo "This uses perl. If you do not have perl this will fail: don't worry about it,"
		@echo "just compile the files by hand using pvcom."
		-(cd ..; ${PERL} < makepv.pl)
		${MAKE} register
# This is a perl script to fill the bin/ directory from the script/
# directory. If you don't have perl then run the scripts from the
# scripts/ directory or compile them by hand.

# Use this to do a complete install:
# ----------------------------------
# (This tidies up after itself, so running this as root will not leave
# files lying around the place with incorrect user IDs.)
rootinstall:	always
# In case library directory hasn't been created:
		@echo "-----------------------------"
		@echo "INSTALLING PUBLIC BASE SYSTEM"
		@echo "-----------------------------"
		${MAKE} ${PVTOOL_LIBRARY_DIRECTORY}
		${MAKE} components
# Clean up
		${MAKE} clean
# Don't register as will leave root-owned files in user's
# space. Registration will happen when ${MAKE} scripts is run.
#		${MAKE} register

###############################################################################

# You'll probably only want to look at the rest of this if you're
# developing the system:

components:	${BINARY} ${COMPILER} ${LIBRARY} xfiles

register:	register.ok

register.ok:	
# Before you wonder, this is just so I've an idea of how many people
# have copies of the system. Plus I like getting mail. It should only
# happen once per installation.
		@echo "--------------------------------"
		@echo "SENDING EMAIL TO REGISTER PVTOOL"
		@echo "--------------------------------"
		@echo "If you don't have an email command this will fail."
		-echo "I've compiled PVTool ${PVTOOL_VERSION}." | ${MAIL} pvtool@muse.demon.co.uk
		echo > register.ok

depend:		${SOURCES} pvtool.c
		@echo "--------------------------"
		@echo "CHECKING FILE DEPENDENCIES"
		@echo "--------------------------"
		makedepend -- ${CFLAGS} -- ${SOURCES} pvtool.c

${PVTOOL_LIBRARY_DIRECTORY}:	
		@echo "----------------------------------------"
		@echo "ENSURING PVTOOL LIBRARY DIRECTORY EXISTS"
		@echo "----------------------------------------"
		-mkdir ${PVTOOL_LIBRARY_DIRECTORY}

clean:
		@echo "----------------------------"
		@echo "REMOVING ANY TEMPORARY FILES"
		@echo "----------------------------"
		-rm -f ${OBJECTS} *.bak *~ PVTool.tgz
		-rm -f ../scripts/*.bak ../scripts/*~

${COMPILER}:	${BINARY}
		@echo "----------------------------------------"
		@echo "CREATING SYMBOLIC LINK BETWEEN PROGRAMS"
		@echo "----------------------------------------"
		ln -s ${BINARY} ${COMPILER}

${BINARY}:	pvtool.c
		@echo "----------------------"
		@echo "COMPILING MAIN PROGRAM"
		@echo "----------------------"
		${CC} -o ${BINARY} pvtool.c
		${BINARY_ACCESS} ${BINARY}

${LIBRARY}:	${OBJECTS}
		@echo "-----------------"
		@echo "COMPILING LIBRARY"
		@echo "-----------------"
		${AR} cr ${LIBRARY} ${OBJECTS}
# This ranlib call is necessary on Linux for some reason, but not on
# SGI. Rather than sort out the subtleties, call ranlib and if it
# isn't there, don't worry about it:
		@echo "You may not have or may not need to call ranlib (ar s). If the next"
		@echo "instruction generates an error it is probably safe to ignore it."
		-${RANLIB} ${LIBRARY}
		${LIBRARY_ACCESS} ${LIBRARY}

xfiles:		${PVTOOL_LIBRARY_DIRECTORY}/top.x ${PVTOOL_LIBRARY_DIRECTORY}/end.x ${PVTOOL_LIBRARY_DIRECTORY}/pvtool.txt

${PVTOOL_LIBRARY_DIRECTORY}/top.x:	top.x
		@echo "-------------------------------"
		@echo "PUTTING A LIBRARY FILE IN PLACE"
		@echo "-------------------------------"
		cp top.x ${PVTOOL_LIBRARY_DIRECTORY}/
		${LIBRARY_ACCESS} ${PVTOOL_LIBRARY_DIRECTORY}/top.x

${PVTOOL_LIBRARY_DIRECTORY}/end.x:	end.x
		@echo "-------------------------------"
		@echo "PUTTING A LIBRARY FILE IN PLACE"
		@echo "-------------------------------"
		cp end.x ${PVTOOL_LIBRARY_DIRECTORY}/
		${LIBRARY_ACCESS} ${PVTOOL_LIBRARY_DIRECTORY}/end.x

${PVTOOL_LIBRARY_DIRECTORY}/pvtool.txt:	pvtool.txt
		@echo "-------------------------------"
		@echo "PUTTING A LIBRARY FILE IN PLACE"
		@echo "-------------------------------"
		cp pvtool.txt ${PVTOOL_LIBRARY_DIRECTORY}/pvtool.txt
		${LIBRARY_ACCESS} ${PVTOOL_LIBRARY_DIRECTORY}/pvtool.txt

always:

PVTool.tgz:	always clean
		@echo "----------------"
		@echo "BUILDING ARCHIVE"
		@echo "----------------"
		mkdir ${TMP_DIR}/pvtool
		mkdir ${TMP_DIR}/pvtool/src
		mkdir ${TMP_DIR}/pvtool/scripts
		mkdir ${TMP_DIR}/pvtool/bin
		mkdir ${TMP_DIR}/pvtool/lib
		mkdir ${TMP_DIR}/pvtool/backup
		cp -p * ${TMP_DIR}/pvtool/src/
		-rm -f ${TMP_DIR}/pvtool/src/register.ok
		cp -p ../scripts/* ${TMP_DIR}/pvtool/scripts/
		cp -p ../makepv.pl ${TMP_DIR}/pvtool/
		cp -p ../README ${TMP_DIR}/pvtool/
		(cd ${TMP_DIR};tar cvf pvtool.tar pvtool/;gzip pvtool.tar)
		mv ${TMP_DIR}/pvtool.tar.gz PVTool.tgz
		rm -r ${TMP_DIR}/pvtool

###############################################################################
