#!/usr/bin/env python
"""
Move files to a directory, making sure that the directory exists before moving
the files there.
"""

__author__ = 'Martin Blais <blais@furius.ca>'

import sys
import os
import shutil
from os.path import *

def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip())
    opts, args = parser.parse_args()

    if len(args) < 2:
        parser.error("You need to provide source and destination files.")

    sources, dest = args[0:-1], args[-1]

    if not exists(dest):
        print >> sys.stderr, "Warning: Creating directory %s" % dest
        os.makedirs(dest)

    uniq = set(map(basename, sources))
    if len(uniq) != len(sources):
        parser.error("Error: Collision in destination filenames.")

    for src in sources:
        shutil.copyfile(src, join(dest, basename(src)))
        os.remove(src)


if __name__ == '__main__':
    main()
