# SPDX-FileCopyrightText: 2021 Galagic Limited, et. al. <https://galagic.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# figular generates visualisations from flexible, reusable parts
#
# For full copyright information see the AUTHORS file at the top-level
# directory of this distribution or at
# [AUTHORS](https://gitlab.com/thegalagic/figular/AUTHORS.md)
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import os
import subprocess
import sys


def usage():
    usage = '''
fig is short for figular which generates visualisations from reusable
parts.

Usage:
  fig -[FIGURE] ARGUMENTS

Available figures:
  concept/circle     A circle of text blobs with (optionally) one in the
                     centre. It can be rotated and the font changed. It takes
                     arguments: blob, middle, degreeStart and font.

Flags:
  --help             Help for Figular

Example:
  fig -concept/circle blob=Hello blob=There blob=You'''
    print(usage)
    exit(0)


def main(args=None):
    if args is None:
        args = sys.argv[1:]

    diagram = "concept/circle.asy"
    cleanArgs = []

    for arg in args:
        if arg == "--help":
            usage()
        elif arg.startswith("-"):
            diagram = f"{arg[1:]}.asy"
        else:
            cleanArgs.append(arg)

    cmd = ['asy',
           '-autoimport',
           os.path.join(os.path.dirname(__file__), diagram),
           '-c' 'run(currentpicture);',
           '-safe', '-noView',
           '-f', 'svg',
           '-o', 'out.svg'
           ]
    subprocess.run(cmd, input="\n".join(cleanArgs), text=True)
