#!/usr/bin/env python3
#
# RabbitVCS dde-file-manager Action Handler
#
# This script is called by the dde-file-manager custom menu
# system (.conf files) to execute VCS operations.
#
# Usage: rabbitvcs-dfm-action <module> [path1] [path2] ...
#
# Copyright (C) 2024 by RabbitVCS Contributors
#
# RabbitVCS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

import os
import sys

from rabbitvcs.util.helper import launch_ui_window
from rabbitvcs.util._locale import initialize_locale
from rabbitvcs import gettext

_ = gettext.gettext
initialize_locale()

usage = _("""Usage: rabbitvcs-dfm-action <module> [path1] [path2] ...

Available modules:
    about, add, annotate, applypatch, branch, branches, browser, changes,
    checkmods, checkout, clean, clone, commit, createpatch, create, delete,
    diff, editconflicts, export, ignore, import, lock, log, merge, properties,
    push, relocate, remotes, rename, markresolved, reset, revert, settings,
    stage, switch, tags, unstage, unlock, update, updateto
""")

args = sys.argv

if len(args) < 2 or args[1] in ("-h", "--help"):
    raise SystemExit(usage)

args.pop(0)  # remove this script's path
module = args.pop(0)

# Expand relative paths to absolute
for i, arg in enumerate(args):
    if os.path.exists(arg):
        args[i] = os.path.abspath(arg)

if not args:
    sys.stderr.write("Error: No paths provided\n")
    sys.exit(1)

launch_ui_window(module, args)
