#!/usr/pkg/bin/python2.7

from optparse import OptionParser
import sys

hasFFTW = bool('/tmp/graphics/vigra/work/.buildlink/lib/libfftw3.so')

parser = OptionParser()

parser.add_option("--version", action = 'store_true',
                  help = "output version (1.10.0)")
#parser.add_option("--target", action = 'store_true',
#                 help = "output platform this VIGRA library was configured for")
parser.add_option("--impex-lib", "--libs", action = 'store_true',
                  help = "output flags for linking libvigraimpex")
parser.add_option("--fftw-lib", action = 'store_true',
                  help = "output flags for linking libfftw"
                  + " (unavailable)" if not hasFFTW else "")
#parser.add_option("--rfftw-lib", action = 'store_true',
#                 help = "output flags for linking librfftw and libfftw")
parser.add_option("--cppflags", action = 'store_true',
                  help = "output include flags for vigra"
                  + " and fftw" if hasFFTW else "")
parser.add_option("--include-path", "--includepath", action = 'store_true',
                  help = "output path to VIGRA includes")
parser.add_option("--docdir", action = 'store_true',
                  help = "output path to VIGRA documentation")

(op, args) = parser.parse_args()

if len(sys.argv) < 2:
    sys.stderr.write("ERROR: no parameters given.\n")
    parser.print_help()
    sys.exit(1)

# --------------------------------------------------------------------

import os.path

# don't output -L flags for the following dirs (assumed to be compiler
# built-in):
standardLibDirs = ["/lib", "/usr/lib", "/lib64", "/usr/lib64"]

libExt = ".so" if not sys.platform == "darwin" else ".dylib"

def filename2ldflags(libFilename):
    dir, fn = os.path.split(libFilename)
    if not (fn.startswith("lib") and fn.endswith(libExt)):
        if libFilename:
            sys.stderr.write("ERROR: don't know how to handle %r!\n" % libFilename)
        return []
    lib = fn[3:-len(libExt)]

    result = []
    if dir not in standardLibDirs:
        result.append("-L" + dir)
    result.append("-l" + lib)
    return result

if op.version:
    print "1.10.0"

if op.cppflags: # was: --cppflags|--cxxincludes|--cxxflags|--cincludes|--cflags
    print '-I/usr/pkg/include'

if op.impex_lib: # was: --impex_lib|--impex-lib|--libs
    ldflags = []

    libDir = '/usr/pkg/lib'
    if libDir not in standardLibDirs:
        ldflags.append('-L' + libDir)

    ldflags.append('-lvigraimpex')

    for flag in 'general;/tmp/graphics/vigra/work/.buildlink/lib/libjpeg.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libpng16.so;general;/lib/libz.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libtiff.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libIlmImf.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libImath.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libHalf.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libIex.so;general;/tmp/graphics/vigra/work/.buildlink/lib/libIlmThread.so;'.split(';'):
        if flag == 'general':
            continue
        for fl in filename2ldflags(flag):
            if fl not in ldflags:
                ldflags.append(fl)

    print " ".join(ldflags)

if op.fftw_lib:
    if not hasFFTW:
        sys.stderr.write("VIGRA was configured without FFTW switches, libpath unknown!\n")
        sys.exit(1)
    print " ".join(filename2ldflags('/tmp/graphics/vigra/work/.buildlink/lib/libfftw3.so'))

if op.include_path: # was: --include_path|--include-path|--includepath
    print '/usr/pkg/include'

if op.docdir:
    print '/tmp/graphics/vigra/work/vigra-1.10.0/doc'
