#!/usr/bin/python
# trash-put: put files and dirs in the trashcan
#
# Copyright (C) 2007-2009 Andrea Francia Trivolzio(PV) Italy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301, USA.

from trashcli.filesystem import Path
from trashcli.trash import trashcan
from trashcli import version

from optparse import OptionParser
from optparse import IndentedHelpFormatter

class NoWrapFormatter(IndentedHelpFormatter) :
    def _format_text(self, text) :
        "[Does not] format a text, return the text as it is."
        return text

parser = OptionParser(usage="%prog [OPTION]... FILE...",
                      description="Put files in trash",
                      version="%%prog %s" % version,
                      formatter=NoWrapFormatter(),
                      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 http://code.google.com/p/trash-cli/issues/""")

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 = Path(arg)
    try:
        trashcan.trash(f)
        if options.verbose:
            print "`%s' trashed in %s " % (arg, trashed_file.trash_directory)
    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))    
