#!/usr/bin/env python3

import os
import subprocess
import sys
import tempfile

if len(sys.argv) < 2:
    print("Usage: {} file...".format(sys.argv[0]), file=sys.stderr)
    sys.exit(1)

editor = os.getenv("EDITOR", "vi")
separator = ": "

# Prepare format string to get a nice padding of the numbers. Also
# include the separator.
num_indices = len(sys.argv) - 1
str_format = "%%%dd%s%%s\n" % (len(str(num_indices)), separator)

# Write all names to a file and store them in a dict.
(fd, filename) = tempfile.mkstemp()
fp = os.fdopen(fd, "w")
i = 0
assoced_names = {}
for n in sys.argv[1:]:
    if os.path.isdir(n):
        fp.write("d ")
    elif os.path.islink(n):
        fp.write("l ")
    elif os.path.isfile(n):
        fp.write("  ")
    elif not os.path.lexists(n):
        fp.write("x ")
    else:
        fp.write("? ")
    fp.write(str_format % (i, n))
    assoced_names[i] = n
    i += 1
fp.close()

# Call the editor, re-read the file and clean up.
retval = subprocess.call([editor, filename])

try:
    with open(filename) as fd:
        entries = fd.readlines()
except OSError as e:
    print("Could not read {}: {}".format(filename, e), file=sys.stderr)
    sys.exit(1)

os.remove(filename)

# Leave out the indicator, get indices, the original name and move the
# files.
for n in entries:
    # If it's empty, skip this entry.
    if n == "":
        continue

    # Try to remove one trailing newline. If it's empty afterwards, skip
    # to the next entry.
    if n[-1] == "\n":
        n = n[:-1]
        if n == "":
            continue

    # Remove the file type indicator. If it's empty afterwards, skip to
    # the next entry.
    n = n[2:]
    if n == "":
        continue

    # Try to separate the filename from the index. If there's no
    # "separator", skip this entry.
    tokens = n.split(separator)
    if len(tokens) < 2:
        continue

    # Try to parse the index. Skip this entry on error.
    try:
        index = int(tokens[0])
    except ValueError:
        continue

    # Try to find this index in our dict. If it can't be found, skip
    # this entry.
    try:
        name_from = assoced_names[index]
    except KeyError:
        continue

    # Now, we got the old name and the new name. Rename the file if the
    # names are not equal. Use "mv" to retain well known behaviour.
    name_to = separator.join(tokens[1:])
    if name_from != name_to:
        if subprocess.call(["mv", "-vi", "--", name_from, name_to]) != 0:
            sys.exit(1)
