# -*- coding: utf-8 -*-
########################################################################
# dirs.py.in - Declare constants for directory names subbed in by
#   autosetup, partly so we don't have to do this in multiple modules
#   but also so that those files don't keep getting touched whenever
#   other *.in files get touched, thus forcing an OS/8 RK05 rebuild.
#
# Copyright © 2017 by Warren Young
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS LISTED ABOVE BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
# OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the names of the authors above
# shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written
# authorization from those authors.
########################################################################

import os
import re


# Anchor directories.  These have to be statically defined for the
# development vs installation cases because chances are high that both
# trees exist on the disk, and these are absolute paths, so we can't
# just do a "path exists" test to determine which we should be using.
# So, autosetup subs in the proper values into the .py version of this
# file in the development tree and the install script overwrites these
# for the installation tree.

build = "@builddir@"
src   = "@abs_top_srcdir@"

# Derived directories.  Where it matters, these are the development tree
# paths, overridden or adjusted below if we're installed.
bin = os.path.join(build, "bin/")
obj = os.path.join(build, "obj/")
log = os.path.join(build, "obj/")
media = os.path.join(src, "media/")
os8mi = os.path.join(media, "os8/")  # os8 media inputs
os8mo = bin  # os8 media outputs

# Adjust paths for the "installed" case
if not os.path.exists(log):
    # The obj/ dir doesn't exist in the install tree
    log = "/tmp/"

if not os.path.exists(media):
    # We bury media one extra level deeper in the install tree
    media = media.replace("/media/", "/share/media/")

    # os8-run scripts output *.rk05 and *.tu56 into bin/ at build time.
    # At install time, they are moved to media/os8 directory.
    # If we're building or modifying the OS/8 media in the installation
    # tree, though, we need to write straight to the media directory.
    os8 = media


#### expand_path #######################################################
# Simple minded variable substitution in a path.
# A path beginning with a dollar sign parses the characters between
# the dollar sign and the first slash seen becomes a name to
# expand with a couple local names: $home and the anchor directories
# defined here.
#
# Returns empty string if the expansion fails.  That signals the caller to fail.


def expand_path(path: str) -> str:
    """
    Expand $var/... into a path defined in this module.
    """
    m = re.match(r"^\$(\w+)/(.*)", path)
    if not m:
        return path

    var, rest = m.groups()
    base = globals().get(var)
    if not isinstance(base, str):
        print(f"Error: ${var} is not a valid path expansion in {path}")
        return ""
    return os.path.join(base, rest)
