#!/usr/bin/env python

#***************************************************************************
#                          cdemu  -  description
#                             -------------------
#    copyright            : (C) 2003 by Robert Penz
#    email                : robert.penz@outertech.com
#***************************************************************************

#***************************************************************************
#*                                                                         *
#*   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.                                   *
#*                                                                         *
#***************************************************************************

"""CDemu is a user space utility for mounting and unmounting CD-images
on a virtual CD device.

Usage: cdemu [options] [<drive number>] [<CD image description file>]

Options:
  -d, --device    use the given device
  -h, --help      show this screen
  -s, --status    shows the status of all virtual drives
  -u, --unload    unloads the given device
  -v, --verbose   be verbose
  -V, --version   show version and copyright notice

CD image description files:
  foobar.cue   (CDRWin)
  foobar.iso   (ISO9660)
  foobar.mds   (Alchol 120%)
  foobar.ccd   (CloneCD)
  foobar.nrg   (Nero Burning ROM)

Examples:
  cdemu 0 foobar.cue   load cd
  cdemu -u 0           unload cd
  cdemu -s             status

Report bugs to <cdemu-devel@lists.sourceforge.net>
"""

import getopt
import sys
import os
import re
import string

import libcdemu

# Print usage message and exit
def usage(*args):
    sys.stdout = sys.stderr
    print __doc__
    print "ERROR:"
    for msg in args: print msg
    sys.exit(2)

def version():
    print "CDemu version 0.8"
    print
    print "Copyright (c) 2003 by Robert Penz."
    print "This program is free software; you can redistribute it and/or modify"
    print "it under the terms of the GNU General Public License as published by"
    print "the Free Software Foundation; either version 2 of the License, or"
    print "(at your option) any later version."
    print
    print "Written by the CDemu project @ http://cdemu.sourceforge.net"

# Main program: parse command line and start processing
def main():
    # parse the command lines and build regex
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hsuvVd:',["help","status","unload", "verbose", "version", "device="])
    except getopt.error, msg:
        usage(msg)

    unload = 0
    verbose = 0
    device = ""
 
    for o, a in opts:
        if o in ["-h", "--help"]:
            print __doc__
            sys.exit()
        if o in ["-V", "--version"]:
            version()
            sys.exit()
        if o in ["-s", "--status"]:
            try:
                print open ("/proc/cdemu","r").read()
            except:
                print "cdemu: cdemu kernel module not loaded"
            sys.exit(1)
        if o in ["-u", "--unload"]: unload = 1
        if o in ["-v", "--verbose"]: libcdemu.verbose = 1
        if o in ["-d", "--device"]: device = a

    if unload:
        # only one argument
        if len(args) != 1 and device == "":
            usage("for unloading a CD I need a (one) drive number or a --device")
        
        # call the unload function
        if device == "":
            device = libcdemu.get_device_from_drive_number(args[0])
        if libcdemu.verbose: print "Using", device
        try:
            libcdemu.unload_cd(device)
        except libcdemu.CDEmu_Error, msg:
            print msg
            
    else: # load
        # 2 args or 1 and --device needed
        if len(args) != 2 and (len(args) != 1 or device == ""):
            usage("for loading a CD I need a drive number or a --device and a cd-image description file")
        
        # call the load function
        if device == "":
            device = libcdemu.get_device_from_drive_number(args[0])
        if libcdemu.verbose: print "Using", device
        try:
            libcdemu.load_cd(device, args[-1],os.path.basename(args[-1]))
        except libcdemu.CDEmu_Error, msg:
            print msg

if __name__ == '__main__':
    main()
