#!/usr/bin/env python
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

### Generates symlinks to Rust Cargo.toml output files

import argparse
import os
import sys

ROOT_PATH = os.path.abspath(__file__ + "/../../..")
CONFIG_PATH = os.path.join(ROOT_PATH, ".config")

def main():
    parser = argparse.ArgumentParser(
            "Generate symlinks to Rust Cargo.toml output files")
    parser.add_argument("gn_target",
                        nargs=1,
                        help="GN target to generate a symlink for. \
                              Use the absolute path to the target \
                              (relative to $FUCHSIA_DIR). \
                              For example: //garnet/bin/foo/bar:baz")
    parser.add_argument("--output",
                        help="Path to Cargo.toml to generate",
                        required=False)
    parser.add_argument("--out-dir",
                        help="Path to the Fuchsia output directory",
                        required=False)
    args = parser.parse_args()

    if args.out_dir:
        out_dir = args.out_dir
    else:
        out_dir = ""
        with open(CONFIG_PATH, "r") as config:
            for line in config.readlines():
                if line.startswith("FUCHSIA_BUILD_DIR="):
                    segments = line.split("=")
                    assert(len(segments) == 2)
                    out_dir = segments[1].strip().strip("'")
        if out_dir == "":
            print "Invalid fuchsia/.config: no FUCHSIA_BUILD_DIR entry found"

    gn_target = args.gn_target[0]
    gn_target = gn_target.lstrip("/")

    gn_target_parts = gn_target.split(":", 1)
    if len(gn_target_parts) == 1:
        # Turn foo/bar into foo/bar/bar
        path = os.path.join(gn_target, os.path.basename(gn_target))
    elif len(gn_target_parts) == 2:
        # Turn foo/bar:baz into foo/bar/baz
        path = gn_target.replace(":", os.sep)
    else:
        print "Invalid gn_target: explected at most one colon."
        return 1

    path = os.path.join(ROOT_PATH, out_dir, "gen", path, "Cargo.toml")

    if args.output:
        output = args.output
    else:
        output = os.path.join(ROOT_PATH, gn_target_parts[0], "Cargo.toml")

    if os.path.exists(path):
        try:
            os.remove(output)
        except OSError:
            pass
        print "Creating '{}' pointing to '{}'".format(output, path)
        os.symlink(path, output)
    else:
        print "Internal error: path '{}' does not point to a Cargo.toml file".format(path)

if __name__ == '__main__':
    sys.exit(main())

