# Specify the source directory and files to link to.
SRCDIR=..
FILES = \
        case4.cgd       \
        case4.cfl       \
        chist_l2.gen    \
        chist_mass.gen  \
        chist_big.gen   \
        pdist_lower.gen \
        pdist_upper.gen \
        pcont.gpc       

######################################################################
.PHONY: help
help:
	@grep '^#HELP: ' Makefile | sed -e 's/#HELP: //'

#HELP: Makefile to create and clean-up links to data files
#HELP: for use with the CFPOST plotting scripts.  Care will
#HELP: be taken to not overwrite non-link files.
#HELP:
#HELP: Targets:
#HELP:   links
#HELP:   clean_all
#HELP:

######################################################################
# Create symbolic links.
.PHONY: links
links: $(FILES)

# Each file in the list will be linked to the same name in the 
# source directory.  If a current link exists, it will be replaced.  
# No physical files will be overwritten, only links.
# Three cases to consider:
#   Link file exists, replace it.
#   Regular file exists, do nothing and print warning.
#   File does not exist, create link.
$(FILES): %: $(SRCDIR)/%
	@echo "Creating $@"
	@if [ -L $* ]; then \
	  rm -f $@; \
	  echo "Trying:  ln -s $(SRCDIR)/$* $@"; \
	                 ln -s $(SRCDIR)/$* $@ ; \
	elif [ -e $* ]; then \
	  echo "  ERROR:  File $* already exists and will not be overwritten."; \
	  echo "          Delete it manually and try again."; \
	  exit 1; \
	else \
	  ln -s $(SRCDIR)/$* $@; \
	fi;

# Remove symbolic links, but not regular files.
.PHONY: clean_all
clean_all:
	@echo "Cleaning files:"
	@for i in $(FILES); do \
	  if [ -L $$i ]; then \
	    echo "rm -f $$i"; \
	          rm -f $$i ; \
	  elif [ -e $$i ]; then \
	    echo "rm -f $$i"; \
	    echo "  File $$i is not a link and will NOT be removed."; \
	  fi; \
	done

