#! /usr/bin/env python3

import sys
import delta
from delta import Diff
import itertools

help_message = f"""
usage: {sys.argv[0]} <diff>
       {sys.argv[0]} <source> <variant>

This script computes a distance between two programs by analyzing a diff. It
uses an annotated llvm-diff to acquire information.

If a single file is provided, it is taken to be the output of [llvm-diff
<source> <variant>].

This script assumes a modified version of llvm-diff that outputs unique
instruction identifiers and the size of added or removed basic blocks.

Produces a single integer on stdout.
""".strip()

#
#  Get command-line arguments
#

args = sys.argv[1:]

if "-h" in args or "--help" in args or "-?" in args:
    print(help_message)
    sys.exit(0)

if len(args) not in [1, 2]:
    print(help_message, file=sys.stderr)
    sys.exit(1)

#
#  Get the diff between input files
#

# If one argument is provided, it's the diff file.
if len(args) == 1:
    d = open(args[0]).read()
    d = Diff.from_llvm_diff_output(d)

# Otherwise, compute the diff now.
else:
    d = Diff.from_llvm_diff(args[0], args[1])

#
#  Output the diff size
#

print(d.size())
