#!/usr/bin/env python

"""usage: ignis_label [options] name

    -f  force labeling e.g. if the medium has already a label  
    -d  use other config file than /etc/ignis.conf
    -i  force an format of the dvd with dvd+rw-format (needed if
        medium is not preformated)
    -h  this help

ignis_label is the special program for ignis to label and optional
to format the mediums. Use this to prepare your disks for the backup process.
"""

import sys
import getopt

import config_manager
import lock
import medium

myConfigManager = None


# Print usage message and exit
def usage(*args):
    sys.stdout = sys.stderr
    for msg in args: print msg
    print __doc__
    sys.exit(2)
    
# Main program: parse command line and start processing
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'fd:hi')
    except getopt.error, msg:
        usage(msg)

    force = False
    format = False
    
    configFile = None
    for o, a in opts:
        if o == '-h':
            print __doc__
            sys.exit()
        if o == '-f':
            force = True
        if o == '-d':
            configFile = a
        if o == '-i':
            format = True

    if len(args) != 1:
        usage()
    
    global myConfigManager
    myConfigManager = config_manager.ConfigManager(configFile)

    # first lock other instances of me
    if not lock.test_and_lock(myConfigManager.getLockFile()):
        print "An other part of ignis is already running! Aborting"
        sys.exit(2)

    myMedium = medium.Medium(myConfigManager)
    myMedium.init(args[0], format, force=force)
    
    lock.unlock(myConfigManager.getLockFile())
    
if __name__ == '__main__':
    main()
