#!/usr/bin/env python

'''
make a directory of raw logging files suitable to be burned on a cd;
optionally gzip the ascii files (shrink 80%)
optionally gzip raw adcp files  (shrink 25%)
files are copied, then gzipped

eg. if sourcedir is ''raw'' with logging directories
             adcp_pings/  adu2/  gyro/  pcode/  sndspd/
    make a new directory ''rawcd'', make all 5 subdirectories, and link
            all files of the form prefix_ddd_* to the appropriate
            subdiredctory (ddd are the days specified).
    optionally: 
       make an iso9660 cd image in the current directory
       burn the cd
       mount the cd device, list it, eject it
    

--prefix      start of filenames (eg. ''nbp2003'')
                          default is everything up to the first underscore
--sourcedir   path to rawlogging root directory (default: ''raw'')
--destdir     path to cd sourcedir root directory (default: ''srcdir'')
--listends    regardless of prefix, list first 4 and last 4 files
                            of each subdir in sourcedir and exit.

--logdirs     colon-separated list of directories to deal with
                default is to try all of them.  Use this option to select
                files by directory or add unusual ones.   

======== (not yet implimented): =========

--gzip       gzip everything specified

                
--cdburn      yes, burn the cd  (otherwise it just prints the options)
--cdname      use this as the volume name (default ''uhdas'')
--mountpoint  cdwriter device name (for mounting)  default is /mnt/cdrom
--scsibus     cdwriter device name  (find from ''cdrecord --scanbus'')
                         eg. 0,1,0

=======================================

           

usage: raw2cd.py --sourcedir vg0301 --listends

usage: raw2cd.py --sourcedir vg0301   day1 [day2 day3 ...] [startdday:endday]

usage: raw2cd.py --prefix nbp2003 \\
          [--sourcedir dirname]  [--destdir dirname] [--gzip] \\
          [--cdburn]  [--scsibus scsiname]   day1 [day2 day3 ...]          

'''
# JH 2003/07/13

import string, sys, glob, os, getopt, shutil, re


def headlist(sourcedir = None, headlength = 4, taillength = 4):
   subdirs = os.listdir(sourcedir)
   if os.path.isdir(os.path.join(sourcedir, subdirs[0])):
      logdirs = [subdirs[0]]
      for name in subdirs[1:]:
         if os.path.isdir(os.path.join(sourcedir, name)):
            logdirs.append(name)
   for name in logdirs:
      if os.path.isdir(os.path.join(sourcedir, name)):
         filelist = os.listdir(os.path.join(sourcedir, name))
         print os.path.join(sourcedir, name) + ':'
         print string.join(filelist[:headlength], '\n')
         print '\n'
         print string.join(filelist[-taillength:], '\n')
         print '----------------'
   print 'existing subdirectories: ' , string.join(logdirs, ', ')
   sys.exit();

# ----- end of headlist    


def copyfiles(days):
   if  os.path.exists(destdir):
      print '===> WARNING: %s exists. ' % (destdir)
   else:
      print 'creating new directory %s'  % (destdir)
      os.mkdir(destdir)
      
   for logdir in logdirs:
      if  os.path.exists(os.path.join(destdir, logdir)):
         print '===> WARNING: %s exists' \
               % (os.path.join(destdir, logdir))
      else:
         print 'creating new directory %s'  %  (os.path.join(destdir, logdir))
         os.mkdir(os.path.join(destdir, logdir))

      flogdir = os.path.join(sourcedir, logdir)
      for day in days:
         globstr = os.path.join(flogdir,
                               string.join( (prefix, '%03d' % (day), '*'), '_'))
         filelist = glob.glob(globstr)
         if verbose:
            print 'globstr is %s' % (globstr)
            print 'filelist is %s', string.join(filelist, '\n')
         for srcfile in filelist:
            destfile = os.path.join(destdir, logdir, os.path.split(srcfile)[1])
            print '%s --> %s' %(srcfile, destfile)
            if os.path.exists(destfile):
               print '===> WARNING: %s exists.  exiting' % (destfile)
               sys.exit()
            else:
               shutil.copy2(srcfile, destfile)

def usage():
   print __doc__

# end of usage   ----------


###-----------------------------------------------------------


# get the options
try:
   options, args = getopt.getopt(sys.argv[1:], 'hv',
                 ['sourcedir=', 'destdir=',  'prefix=', 'logdirs=', 
                  'mountpoint=', 'scsibus=', 'cdburn', 'listends',
                  'help', 'verbose', 'gzipascii', 'gzipadcp'])
except getopt.GetoptError:
   usage()
   
sourcedir  = 'raw'
destdir = 'srcdir'
logdirs = None
prefix = '*'
cdburn = 0
scsibus = '0,0,0'
mountpoint = '/mnt/cdrom'
cdname = 'uhraw'
listends = 0
gzip = 0
help = 0
verbose = 0


for o, a in options:
   if o in ('', '--sourcedir'):
      sourcedir = a
   elif o in ('', '--destdir'):
      destdir = a
   elif o in ('', '--prefix'):
      prefix = a 
   elif o in ('', '--scsibus'):
      scsibus = a 
   elif o in ('', '--device'):
      device = a 
   elif o in ('', '--logdirs'):
      logdirs = a
   elif o in ('', '--cdburn'):
      cdburn = 1
   elif o in ('', '--listends'):
      listends = 1
   elif o in ('', '--gzip'):
      gzip = 1
   elif o in ('-h', '--help'):
      usage()
   elif o in ('-v', '--verbose'):
      verbose = 1

### main

if (help):
   usage()
   sys.exit()

if  not os.path.exists(sourcedir):
   usage()
   print '===>  ERROR: sourcedir %s does not exist <===' % (sourcedir)
   sys.exit()
        
if listends:
   headlist(sourcedir);


if not logdirs:
   subdirs = os.listdir(sourcedir)
else:
   subdirs = logdirs.split(':')
if os.path.isdir(os.path.join(sourcedir, subdirs[0])):
   logdirs = [subdirs[0]]
for name in subdirs[1:]:
   if os.path.isdir(os.path.join(sourcedir, name)):
      logdirs.append(name)

print 'copying files from directories: ' , string.join(logdirs, ', ')
if len(args) == 0:
   print 'no days specified; no files copied'



startend = args[0].split(':')
if verbose:
   print 'startend is', startend
if len(startend) > 1:
   days = range(int(startend[0]), int(startend[1])+1)
else:
   days = [int(startend[0])]


for arg in args[1:]:
   startend = arg.split(':')
   if len(startend) > 1:
      days.append(range(int(startend[0]), int(startend[1])+1))
   else:
      days.append(int(startend[0]))

   if verbose:
      print 'startend is', startend


copyfiles(days)         
      

# now make the CDs


# command1 = 'mkisofs -V %s -A %s  -J -l -L -r  -o %s.img %s' \
#               %(cdname, cdname, cdname, cdname)
# command2 = 'cdrecord -v dev=%s  %s.img' % (scsibus, cdname)
# command3 = 'mount %s; ls -lR %s; umount %s; eject %s' % \
#            (mountpoint, mountpoint, mountpoint, mountpoint)


# if cdburn:
#    print 'making iso9660 image:\n%s' % (command1)
#    os.system(command1)

#    print 'burning cd:\n%s' % (command2)
#    os.system(command2)

#    print 'try to list cd:\n' % (command3)
#    os.system(command3)
# else:
#    print 'could make iso9660 image:\n%s' % (command1)
#    print 'burn cd with:\n%s' % (command2)
#    print 'then list it with:\n%s' % (command3)

   
   

