#***************************************************************************************************
#                                             Makefile                                             *
#                                            ----------                                            *
# Description : Makefile for GNU SPICE GUI project house-keeping.                                  *
# Started     : 2004-01-08                                                                         *
# Updated     : 2020-11-26                                                                         *
# Copyright   : (C) 2004-2021 by MSWaters                                                          *
# Note        : Enter "make help" to view available Makefile targets.                              *
#***************************************************************************************************

#***************************************************************************************************
# Release Check List :
#
#  1. Build gspiceui (ie. <ROOT>/src/make), should be no errors or warnings
#  2. Run syntax checker (ie. <ROOT>/src/make check), should be no errors or warnings
#  3. Build all test apps. (ie. <ROOT>/src/make tests), should be no errors or warnings
#  4. Build under Qemu/Windows (no other way to test Windows code), should be no errors or warnings
#  5. Create a release notes file in the project root directory (eg. release-notes-v1.2.87.txt)
#  6. Close off the section for the release in the ChangeLog file
#  7. Demo. schematics should all work OK
#  8. Clean project sources (ie. <ROOT>/make clean)
#  9. Update SVN repository (eg. svn -m "This is the last update before the next release" ci)
# 10. Set archive version string (ie. TAR_VER) in this file
# 11. Build archive (ie. <ROOT>/make tar)
# 12. Copy archive to ~/tmp and check that it builds properly and runs
# 13. Refer to howto-svn.txt and create a tag in the SVN repository
# 14. Download the files (ie. release notes and archive) to sourceforge.net
# 15. On sourceforge.net set archive as the default download. (Find the archive in the "Files" tab
#     and select the "i" link which is to the right of the file name.)
# 16. Put a note in the project news
# 17. Update my web site
#***************************************************************************************************

#***************************************************************************************************
#                                                                                                  *
#       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 3 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 DESTDIR=/new/dest/dir install
#   make DESTDIR=/new/dest/dir uninstall
#
# Note : Values specified on the command line over-ride those specified below.
#***************************************************************************************************

# Destination (install) directory
DESTDIR = /usr/local

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

# Root directory
ROOT := $(shell pwd)

# Symbol file directory
SYM_DIR = $(ROOT)/lib/symbols

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

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

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

# Everyone should call this once
config : cfg_geda cfg_leda
	@test -d $(ROOT)/bin     || mkdir $(ROOT)/bin
	@test -d $(ROOT)/src/obj || mkdir $(ROOT)/src/obj

# Configure gEDA/gaf   to incorporate local symbol files from the directory "lib/symbols"
GEDA_CFG_DIR = $(HOME)/.gEDA
cfg_geda :
	@test -d $(GEDA_CFG_DIR) || mkdir $(GEDA_CFG_DIR)
	@grep -c "$(SYM_DIR)" $(GEDA_CFG_DIR)/gafrc &> /dev/null || \
	   echo '(component-library "$(SYM_DIR)" "gSpiceUI")' > $(GEDA_CFG_DIR)/gafrc

# Configure Lepton-EDA to incorporate local symbol files from the directory "lib/symbols"
LEDA_CFG_DIR = $(HOME)/.config/lepton-eda
cfg_leda :
	@test -d $(LEDA_CFG_DIR) || mkdir $(LEDA_CFG_DIR)
	@grep -c "$(SYM_DIR)" $(LEDA_CFG_DIR)/gafrc &> /dev/null || \
	   echo '(component-library "$(SYM_DIR)" "gSpiceUI")' > $(LEDA_CFG_DIR)/gafrc

# 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

#***************************************************************************************************
# Create an archive of the essential project files as follows :
#
#   1. Change to the project root directory
#   2. Execute "make tar" to create the project archive file eg. gspiceui-v1.1.00.tar.gz
#
# Note : If a release notes file with the correct name isn't found the archive can't be built, so
#        this script looks for the release notes before attempting to build the archive.
#***************************************************************************************************

TAR_VER:=$(shell grep -m 1 "APP_VERSION" src/Version.hpp | gawk '{print $$3}')

TAR_DEST= gspiceui-$(TAR_VER)/gspiceui-$(TAR_VER).tar.gz

TAR_SRCS =gspiceui-$(TAR_VER)/release-notes-$(TAR_VER).txt
TAR_SRCS+=gspiceui-$(TAR_VER)/Authors
TAR_SRCS+=gspiceui-$(TAR_VER)/ChangeLog
TAR_SRCS+=gspiceui-$(TAR_VER)/Install
TAR_SRCS+=gspiceui-$(TAR_VER)/License
TAR_SRCS+=gspiceui-$(TAR_VER)/Makefile
TAR_SRCS+=gspiceui-$(TAR_VER)/ReadMe
TAR_SRCS+=gspiceui-$(TAR_VER)/ToDo
TAR_SRCS+=gspiceui-$(TAR_VER)/gspiceui.1
TAR_SRCS+=gspiceui-$(TAR_VER)/gspiceui.lsm
TAR_SRCS+=gspiceui-$(TAR_VER)/src/*
TAR_SRCS+=gspiceui-$(TAR_VER)/html/Makefile
TAR_SRCS+=gspiceui-$(TAR_VER)/html/*.html
TAR_SRCS+=gspiceui-$(TAR_VER)/html/*.png
TAR_SRCS+=gspiceui-$(TAR_VER)/html/*.jpg
TAR_SRCS+=gspiceui-$(TAR_VER)/sch/Makefile
TAR_SRCS+=gspiceui-$(TAR_VER)/sch/gnet-spice-msw.scm
TAR_SRCS+=gspiceui-$(TAR_VER)/sch/*/*.sch
TAR_SRCS+=gspiceui-$(TAR_VER)/lib/Makefile
TAR_SRCS+=gspiceui-$(TAR_VER)/lib/models/*.ckt
TAR_SRCS+=gspiceui-$(TAR_VER)/lib/models/*.mod
TAR_SRCS+=gspiceui-$(TAR_VER)/lib/symbols/*
TAR_SRCS+=gspiceui-$(TAR_VER)/mac/Info.plist
TAR_SRCS+=gspiceui-$(TAR_VER)/mac/GSpiceUI.icns
TAR_SRCS+=gspiceui-$(TAR_VER)/svg/Makefile
TAR_SRCS+=gspiceui-$(TAR_VER)/svg/*.svg

tar :
	@echo gspiceui-$(TAR_VER).tar.gz
ifeq ($(shell test -f release-notes-$(TAR_VER).txt ; echo $$?), 0)
	make clean
	cd svg ; make
	cd .. ; mv gspiceui gspiceui-$(TAR_VER)
	cd .. ; tar --totals --exclude .svn -zcf $(TAR_DEST) $(TAR_SRCS)
	cd .. ; mv gspiceui-$(TAR_VER) gspiceui
else
	@echo
	@echo -n "ERROR : The release notes file for this release wasn't found : "
	@echo "release-notes-$(TAR_VER).txt"
	@echo
endif

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

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

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

uninstall :
	cd $(ROOT)/src  ; $(MAKE) uninstall DESTDIR=$(DESTDIR)
	cd $(ROOT)/html ; $(MAKE) uninstall DESTDIR=$(DESTDIR)
	cd $(ROOT)/sch  ; $(MAKE) uninstall DESTDIR=$(DESTDIR)
	cd $(ROOT)/lib  ; $(MAKE) uninstall DESTDIR=$(DESTDIR)
	rm -fr $(DESTDIR)/share/gspiceui
	rm -fr $(DESTDIR)/share/doc/gspiceui
	rm -f  $(DESTDIR)/share/man/man1/gspiceui.1
#	rmdir --ignore-fail-on-non-empty $(DESTDIR)/share/man/man1
#	rmdir --ignore-fail-on-non-empty $(DESTDIR)/share/man
#	rmdir --ignore-fail-on-non-empty $(DESTDIR)/share

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

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

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

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

lines :
	cd $(ROOT)/src ; cloc Makefile *.?pp */*.?pp */*/*.?pp

#***************************************************************************************************
# Display a help message
#***************************************************************************************************

help :
	@echo
	@echo -e "gSpiceUI project build system. "
	@echo
	@echo -e "Available make targets :"
	@echo
	@echo -e "  all       - Build the config target (default)"
	@echo -e "  config    - Configure the build system"
	@echo -e "  tar       - Build a archive (tar.gz) file of the project sources"
	@echo -e "  install   - Perform project wide   install tasks"
	@echo -e "  uninstall - Perform project wide uninstall tasks"
	@echo -e "  clean     - Remove temporary files and backup files"
	@echo -e "  size      - Log source code file sizes to the file charcnt.txt"
	@echo -e "  lines     - Perform a line count using cloc (Count Lines Of Code)"
	@echo -e "  help      - Display this message"
	@echo

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

.PHONY : config cfg_geda cfg_leda maccfg tar install uninstall clean size lines help

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