#!/usr/bin/python2

import os
import os.path
import commands
import sys
import ConfigParser
import getopt
import cPickle
import zlib
import string
import time
import curses

from commondic import *

def exit( message ) :
    print message
    try :
        curses.endwin()
    except curses.error :
        pass
    sys.exit( 1 )

# lists the contents of the disks
def list():
    for diskName in catalog.diskNames() :
        disk = catalog.loadDisk( diskName )
        path = ''
        if disk.status == '' :
            disk.status = 'no status'
        if catalog.extfsByStatus == '1' :
            path = disk.status + '/'
        if catalog.extfsByCategory == '1' :
            path = disk.category + '/'
        if catalog.extfsByStatus == '2' :
            path = path + disk.status + '/'
        if catalog.extfsByCategory == '2' :
            path = path + disk.category + '/'
        for file in disk.files :
            diskPath = path + diskName
            if file.type == TYPEFILE :
                line = '-r--r--r-- 001 '
            else :
                line = 'dr-xr-xr-x 002 '
            line = line + '%d %d %d ' % ( os.getuid(), os.getgid(), file.size ) + \
                time.strftime( '%m-%d-%y %H:%M', time.localtime( file.time ) ) + \
                ' ' + diskPath + file.name
            print line

def printMessageAndWait( message ) :
    if catalog.yCoord > 20 :
        catalog.yCoord = 0
    if catalog.mustInitScreen == 1 :
        catalog.mustInitScreen = 0
        catalog.myScreen = curses.initscr()
        catalog.myScreen.erase()

    catalog.myScreen.addstr( yCoord, 0, message )
    yCoord = yCoord + 1
    curses.doupdate()
    try:
        enter = catalog.myScreen.getstr()
    except KeyboardInterrupt:
        catalog.exit( 'exiting.' )

def printMessage( message ) :
#    if catalog.yCoord > 20 :
#        catalog.yCoord = 0
#
#    if catalog.mustInitScreen == 1 :
#        catalog.mustInitScreen = 0
#        catalog.myScreen = curses.initscr()
#        catalog.myScreen.erase()
#
#    catalog.myScreen.addstr( message  )
#    catalog.yCoord = catalog.yCoord + 1
#    curses.doupdate()
    pass

def getFile( fileName, destFile ) :
    # we must use curses, because raw_input doesn't work under mc.

    # remove the directories for 'status' and 'category'
    if catalog.extfsByStatus != '0' :
        firstSlash = string.find( fileName, '/' )
        fileName = fileName[ firstSlash + 1 : ]
    if catalog.extfsByCategory != '0' :
        firstSlash = string.find( fileName, '/' )
        fileName = fileName[ firstSlash + 1 : ]

    files = [ fileName ]
    catalog.printMessageAndWait = printMessageAndWait
    catalog.printMessage = printMessage
    catalog.exit = exit
    catalog.getFiles( files, destFile )

    try :
        curses.endwin()
    except curses.error :
        pass

# main
if __name__ == '__main__':

    try:
        catalog = Catalog()
    except DicNoDeviceInConfigException:
        exit( 'No devices found, please check your config-files. (/etc/dic.conf and/or $HOME/.dic/dic.conf)' )
    catalog.mustInitScreen = 1
    catalog.yCoord = 0
    catalog.currentDevice = catalog.defaultDevice
    if len( sys.argv ) == 1:
        exit( 'No command given, exiting.' )
    elif sys.argv[ 1 ] == 'copyout' :
        getFile( sys.argv[ 3 ], sys.argv[ 4 ] )
    elif sys.argv[ 1 ] == 'list' :
        list()
    else :
        sys.exit( 1 )
    sys.exit( 0 )
