#!/usr/bin/python
# $Id: dpm-delreplica,v 1.1 2009/03/04 11:18:51 gcowan Exp $

__author__ = 'Greig A Cowan'
__date__ = 'March 2009'
__version__ = 0.1

'''
Deletes a replica from the filesystem
'''

import sys
try:
    import dpm
except:
    print 'Failed to import dpm API. Check your $PYTHONPATH. If this is correct, please check the bitness of your system libraries - it may be that you have 64bit python and 32bit DPM.'
    sys.exit(1)


from optparse import OptionParser

def main():
    usage = '''usage: %prog pfn

    Deletes a specified replica from the system.

    $ dpm-delreplica pool1.glite.ecdf.ed.ac.uk/gridstorage001/path/to/replica
    '''

    parser = OptionParser(
        usage = usage)
    parser.add_option('-d', '--debug', dest='debug', action='store_true',
                      help='Use debug flag only for testing.')

    (options, args) = parser.parse_args()

    if len(args) > 1:
        parser.error("ERROR: Incorrect number of arguments")

    pfn = args[0]

    res = dpm.dpm_delreplica( pfn)

    if res == 0:
        if options.debug:
            print 'Replica deleted'
        sys.exit(0)
    else:
        err_num = dpm.cvar.serrno
        err_string = dpm.sstrerror(err_num)
        print 'Error while deleting ' + pfn + ': Error ' \
              + str(err_num) + ' (' + err_string + ')'
        sys.exit(1)

if __name__ == '__main__':
         main()
