#! /usr/bin/env python
import argparse
import sys
import logging
import subprocess
from pathlib import Path

logger = logging.getLogger(__name__)


def main() -> int:
    # Handle program arguments
    ap = argparse.ArgumentParser(prog="ezlink", description="Easier symlink took")
    ap.add_argument("pointer", help="Link that points to the destination", type=Path)
    ap.add_argument("destination", help="Destination of the link", type=Path)
    ap.add_argument(
        "-f", "--force", help="Force the link to be created", action="store_true"
    )
    ap.add_argument(
        "--hard", help="Link directly to the destination inode", action="store_true"
    )
    ap.add_argument("--absolute", "-a", help="Use absolute paths", action="store_true")
    ap.add_argument(
        "--dereference-destination",
        help="Follow the destination if it is also a pointer",
        action="store_true",
    )
    ap.add_argument(
        "--dry-run", help="Don't actually create the link", action="store_true"
    )
    args = ap.parse_args()

    # Convert to absolute paths if requested
    if args.absolute:
        args.pointer = args.pointer.absolute()
        args.destination = args.destination.absolute()

    # Construct the appropriate LN command
    command = ["ln"]
    if not args.dereference_destination:
        command.append("-n")
    if not args.hard:
        command.append("-s")
    if args.force:
        command.append("-f")
    command.append(str(args.destination))
    command.append(str(args.pointer))

    # Print the command
    print(" ".join(command))

    # Run the command if not a dry run
    if not args.dry_run:
        return subprocess.run(command).returncode

    return 0


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