#!/usr/bin/env python
from setuptools import setup

import os.path
import glob

# There are a number of local hacks in this file, to deal with the multiple
# ways in which setup.py is called by various scripts and packaging methods 
# that interact with spud, enabling setuptools to grok their intentions.

# In some cases, we will be passed a 'DESTDIR' from an upstream packagaing
# system. This will be a local directory to install into, and act as local '/'
# as far as all paths are concerned. Check for this, and fail nicely if not set.

cprefix = ""
prefix = None
import sys
# For Darwin / macOs packaging
packaging = False

# We may also be given prefix, either as a configuration option (which will be
# dealt with by substitutions later) or as a command line option. If a command
# line option is present, parse this. In some cases, just to add spice into the
# mix, PREFIX is supplied twice - once by configure, and again by a parent
# Makefile as a command-line flag.
#
# As an alternative to DESTDIR being set, Debian packaging calls setup.py with
# a --root command line option which does the same thing. This needs to be
# parsed if present, and supercedes any previous DESTDIR picked up from
# environment.

for i, arg in enumerate(sys.argv):
  if "--prefix" in arg:
    prefix = arg.split('=')[1]

# Given the above prefix possibilities, as well as root and DESTDIR, we need to
# construct a list of data directories to be installed
#
#   * configure-supplied prefixes are dealt with by substitution directly into
#       this file, with @ PREFIX @ being replaced.
#   * prefixes as command-line options are pushed by us
#
# Note that in the case of this and other packages which supply prefix twice
# (once in configure, and then again on the command line) we honour the prefix
# on the command line in preference to the configure prefix.

if prefix is not None and len(prefix) > 0 and not prefix.endswith('/'):
  prefix = prefix + '/'
if cprefix is not None and len(cprefix) > 0 and not cprefix.endswith('/'):
  cprefix = cprefix + '/'

# First parse the plugin directories
plugin_dirs = []
if os.path.exists('plugins'):
  plugin_dirs = [dir for dir in os.listdir('plugins') if os.path.isdir(os.path.join('plugins', dir)) and dir[0] != '.']
plugin_data_files = []
if sys.platform == 'darwin' and packaging:
  for plugin in plugin_dirs:
      plugin_data_files.append(("./plugins/" + plugin,
      glob.glob('plugins/' + plugin + '/*.py')))
else:
  for plugin in plugin_dirs:
    if prefix is None:
      plugin_data_files.append((cprefix + "share/diamond/plugins/" + plugin,
        glob.glob('plugins/' + plugin + '/*.py')))
    else:
      plugin_data_files.append((prefix + "share/diamond/plugins/" + plugin,
        glob.glob('plugins/' + plugin + '/*.py')))

# Now parse the GUI directories
gui_data_files = []
if sys.platform == 'darwin' and packaging:
  gui_data_files.append(("./gui",
    ["gui/gui.glade", "gui/diamond.svg", "gui/diamond.png"]))
else:
  if prefix is None:
    gui_data_files.append((cprefix + "share/diamond/gui",
      ["gui/gui.glade", "gui/diamond.svg"]))
  else:
    gui_data_files.append((prefix + "share/diamond/gui",
      ["gui/gui.glade", "gui/diamond.svg"]))

# Now parse the schema directories
schema_data_files = []
if sys.platform == 'darwin' and packaging:
  schema_data_files.append(("./schema",
    ["schema/spud_base.rnc", "schema/spud_base.rng"]))
else:
  if prefix is None:
    schema_data_files.append((cprefix + "share/diamond/schema",
      ["schema/spud_base.rnc", "schema/spud_base.rng"]))
  else:
    schema_data_files.append((prefix + "share/diamond/schema",
      ["schema/spud_base.rnc", "schema/spud_base.rng"]))

# We now have all the information we need; run setup.
setup(
      name='spud-diamond',
      version='1.3',
      description="Model option preprocessor",
      author = "The ICOM team and collaborators",
      author_email = "contact@candylab.org",
      url = "http://candylab.org",
      packages = ['diamond'],
      package_dir = {'diamond': 'diamond'},
      scripts=["bin/diamond"],
      data_files = schema_data_files + gui_data_files + plugin_data_files
          + [('', ['README.txt', 'COPYING', 'GPL-3'])],
      long_description="""
Diamond  (spud-diamond)
=======================

A model option preprocessor.

Maintained by Dr Adam S. Candy ([http://candylab.org](http://candylab.org "http://candylab.org")) primarily for use with the Shingle Project, see: [http://shingleproject.org](http://shingleproject.org "http://shingleproject.org").

""",
      long_description_content_type='text/markdown',
     )

