#!/usr/bin/python
from libtrash import File
from libtrash import TrashDirectory
from optparse import OptionParser
from optparse import IndentedHelpFormatter
import libtrash

def trash (f) :
    """ 
    Trash a file in the appropriate trash directory.
    If the file belong to the same volume of the trash home directory it will
    be trashed in the home trash directory.
    Otherwise it will be trashed in one of the relevant volume trash directories.
    
    Each volume can have two trash directories, they are 
        - $volume/.Trash/$uid
        - $volume/.Trash-$uid
    
    Firstly the software attempt to trash the file in the first directory then 
    try to trash in the second trash directory.
    """
    try: 
        volume = f.getParent().getVolume()

        if TrashDirectory.getHomeTrashDirectory().getVolume() == volume :
            TrashDirectory.getHomeTrashDirectory().trash(f)
        else :
            if volume.hasCommonTrashDirectory() :
                volume.getCommonTrashDirectory().trash(f)
            else :
                volume.getUserTrashDirectory().trash(f)

        if options.verbose:
            print "trashed: `%s'" % arg
    except OSError, e:
        # occour when the file cannot be moved
        print "trash: cannot trash `%s': %s" % (arg, str(e))
    except IOError, e:
        # occour when the file does not exist
        print "trash: cannot trash `%s': %s" % (arg, str(e))
    

class MyFormatter (IndentedHelpFormatter) :
    def format_epilog(self, epilog):
        if epilog:
            return "\n" + epilog + "\n"
        else:
            return ""

parser = OptionParser(usage="%prog [OPTION]... FILE...",
                      description="Put files in trash",
                      version="%%prog %s" % libtrash.version,
                      formatter=MyFormatter(),
                      epilog=
"""To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:

  trash -- -foo

  trash ./-foo
  
Report bugs to <andreafrancia@sourceforge.net>.""")

parser.add_option("-d",
                  "--directory",
                  action="store_true",
                  help="ignored (for GNU rm compatibility)")

parser.add_option("-f",
                  "--force",
                  action="store_true",
                  help="ignored (for GNU rm compatibility)")

parser.add_option("-i",
                  "--interactive",
                  action="store_true",
                  help="ignored (for GNU rm compatibility)")

parser.add_option("-r",
                  "-R",
                  "--recursive",
                  action="store_true",
                  help="ignored (for GNU rm compatibility)")

parser.add_option("-v",
                  "--verbose",
                  action="store_true",
                  help="explain what is being done",
                  dest="verbose")

(options, args) = parser.parse_args()


if len(args) <= 0:
    parser.error("Please specify the files to trash.")


for arg in args :
    f = File(arg)
    trash(f)
