#******************************************************************************
#                                  Makefile                                   *
#                                 ----------                                  *
# Description : Makefile for GNU Spice GUI project house-keeping.             *
# Started     : 08/01/2004                                                    *
# Last update : 29/09/2011                                                    *
# Copyright   : (C) 2004 by MSWaters                                          *
# Email       : M.Waters@bom.gov.au                                           *
#******************************************************************************

#******************************************************************************
#                                                                             *
#     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.                                     *
#                                                                             *
#******************************************************************************

#******************************************************************************
# Any or all of the values in this section may be set below or on the command
# line when envoking make eg. :
#
#   make INSTALLDIR=/new/dest/dir install
#   make INSTALLDIR=/new/dest/dir uninstall
#
# Note : Values specified on the command line over-ride those specified below.
#******************************************************************************

#DESTDIR = /new/dest/dir

# Install directory
ifndef DESTDIR
  INSTALLDIR = /usr/local
else
  INSTALLDIR = $(DESTDIR)
endif

#******************************************************************************
# Specify string values
#******************************************************************************

# Root directory
ROOT := $(shell pwd)

#******************************************************************************
# Make these targets
#******************************************************************************

all :
	$(MAKE) config
	cd $(ROOT)/src ; $(MAKE)

#******************************************************************************
# Calculate the size of the various sources making up this project
#******************************************************************************

size :
	$(ROOT)/charcnt.sh >> charcnt.txt

#******************************************************************************
# Create an archive of the essential project files as follows :
#
#   1.  Change the make variable "TAR_NAME" (below) to the current version no.
#   2.  Execute "make tar" from the project root directory.
#******************************************************************************

TAR_NAME=gspiceui-v1.0.00
TAR_DEST= $(TAR_NAME)/$(TAR_NAME).tar.gz
TAR_SRCS =$(TAR_NAME)/Authors
TAR_SRCS+=$(TAR_NAME)/ChangeLog
TAR_SRCS+=$(TAR_NAME)/Install
TAR_SRCS+=$(TAR_NAME)/License
TAR_SRCS+=$(TAR_NAME)/Makefile
TAR_SRCS+=$(TAR_NAME)/ReadMe
TAR_SRCS+=$(TAR_NAME)/ToDo
TAR_SRCS+=$(TAR_NAME)/gspiceui.1
TAR_SRCS+=$(TAR_NAME)/gspiceui.lsm
TAR_SRCS+=$(TAR_NAME)/src/*
TAR_SRCS+=$(TAR_NAME)/html/Makefile
TAR_SRCS+=$(TAR_NAME)/html/*.html
TAR_SRCS+=$(TAR_NAME)/html/*.jpg
TAR_SRCS+=$(TAR_NAME)/sch/Makefile
TAR_SRCS+=$(TAR_NAME)/sch/*/*.sch
TAR_SRCS+=$(TAR_NAME)/lib/Makefile
TAR_SRCS+=$(TAR_NAME)/lib/*/*.ckt
TAR_SRCS+=$(TAR_NAME)/lib/*/*.mod
TAR_SRCS+=$(TAR_NAME)/lib/sym/*
TAR_SRCS+=$(TAR_NAME)/mac/Info.plist
TAR_SRCS+=$(TAR_NAME)/mac/GSpiceUI.icns
TAR_SRCS+=$(TAR_NAME)/fig/Makefile
TAR_SRCS+=$(TAR_NAME)/fig/*.fig

tar :
	make clean
	cd fig ; make ; cd ..
	cd .. ; mv gspiceui $(TAR_NAME)
	cd .. ; tar --totals --exclude .svn -zcf $(TAR_DEST) $(TAR_SRCS)
	cd .. ; mv $(TAR_NAME) gspiceui

#******************************************************************************
# Perform configuration tasks
#******************************************************************************

# Everyone should call this once
config :
ifneq ($(ROOT)/bin,$(wildcard $(ROOT)/bin))
	mkdir $(ROOT)/bin
endif
ifneq ($(ROOT)/src/obj,$(wildcard $(ROOT)/src/obj))
	mkdir $(ROOT)/src/obj
endif

# You should also call this once if running on a MAC
maccfg : $(ROOT)/mac/Info.plist $(ROOT)/mac/GSpiceUI.icns
ifneq ($(ROOT)/GSpiceUI.app,$(wildcard $(ROOT)/GSpiceUI.app))
	-mkdir $(ROOT)/GSpiceUI.app
	-mkdir $(ROOT)/GSpiceUI.app/Contents
	-mkdir $(ROOT)/GSpiceUI.app/Contents/MacOS
	-mkdir $(ROOT)/GSpiceUI.app/Contents/Resources
	cp $(ROOT)/mac/Info.plist $(ROOT)/GSpiceUI.app/Contents/
	echo -n 'APPL????' > $(ROOT)/GSpiceUI.app/Contents/PkgInfo
	cp $(ROOT)/mac/GSpiceUI.icns $(ROOT)/GSpiceUI.app/Contents/Resources/
endif

#******************************************************************************
# Perform installation tasks
#******************************************************************************

install :
	mkdir -p     $(INSTALLDIR)/share/gspiceui
	cp ReadMe    $(INSTALLDIR)/share/gspiceui
	cp Install   $(INSTALLDIR)/share/gspiceui
	cp ChangeLog $(INSTALLDIR)/share/gspiceui
	mkdir -p     $(INSTALLDIR)/share/gspiceui/icons
	cp src/icons/gspiceui-*.xpm $(INSTALLDIR)/share/gspiceui/icons
	mkdir -p $(INSTALLDIR)/share/man/man1
	cp gspiceui.1 $(INSTALLDIR)/share/man/man1
	cd $(ROOT)/src  ; $(MAKE) install INSTALLDIR=$(INSTALLDIR)
	cd $(ROOT)/html ; $(MAKE) install INSTALLDIR=$(INSTALLDIR)
	cd $(ROOT)/sch  ; $(MAKE) install INSTALLDIR=$(INSTALLDIR)
	cd $(ROOT)/lib  ; $(MAKE) install INSTALLDIR=$(INSTALLDIR)

#******************************************************************************
# Perform uninstall tasks
#******************************************************************************

uninstall :
	rm -fr $(INSTALLDIR)/share/gspiceui
	rm -f  $(INSTALLDIR)/share/man/man1/gspiceui.1

#******************************************************************************
# Remove temporary files and backup files
#******************************************************************************

clean :
	rm -f Makefile~ charcnt~
	cd $(ROOT)/src  ; $(MAKE) cleanall
	cd $(ROOT)/html ; $(MAKE) clean
	cd $(ROOT)/sch  ; $(MAKE) clean
	cd $(ROOT)/lib  ; $(MAKE) clean

#******************************************************************************
# Specify phony targets
#******************************************************************************

.PHONY : size tar config install uninstall clean

#******************************************************************************
