#! /usr/bin/python
import os
import sys
import commands
import shutil
import datetime
from optparse import OptionParser

# Get time and date
now = datetime.datetime.now()

# Get and seperate options from arguments
parser = OptionParser()
parser.add_option("-d", "--dir", dest="dirname", help="associated backup directory", metavar="FOLDER")
(options, args) = parser.parse_args()

# If not supplied get default from environment, or just store locally
if type(options.dirname)!=type('str'):
  options.dirname=os.environ.get("BACKUPDIR")
  if options.dirname=='None': dirname='./'

print 'Associated backup folder: ' + options.dirname
# Do the copying (including associated files)
# Its a bit dense down here, this is perhaps not the cleanest way
# But its quite stable and has a lot of error checks
for arg in args:
  # State copy commands to move file to backup folder
  try:
    commands.getoutput('cp -f ' + arg + ' ' + os.path.join(options.dirname,arg))
    print 'Backed up file: ' + arg
    try:
      infile=str(commands.getoutput('Get <' + arg + ' in parform=n')).rstrip().lstrip()
      if os.path.exists(infile):
        commands.getoutput('cp -f ' + infile + ' ' + os.path.join(options.dirname,os.path.basename(infile)))
        f=open(os.path.join(options.dirname,arg), 'a')
        f.write('sdrToBackup: '+now.strftime("%Y-%m-%d %H:%M"+'\n'))
        f.write('           sets next: in="'+os.path.join(options.dirname,os.path.basename(infile))+'"\n\n')
        f.close()
        print 'Backed up file: ' + infile
      else:
        print 'Not backed up file: ' + infile
    except:
      print 'No associated datafile for: '+arg
    try:
      hfffile=str(commands.getoutput('Get <' + arg + ' hff parform=n')).rstrip().lstrip()
      if os.path.exists(hfffile):
        commands.getoutput('cp -f ' + hfffile + ' ' + os.path.join(options.dirname,os.path.basename(hfffile)))
        f=open(os.path.join(options.dirname,arg), 'a')
        f.write('sdrToBackup: '+now.strftime("%Y-%m-%d %H:%M"+'\n'))
        f.write('           sets next: hff="'+os.path.join(options.dirname,os.path.basename(hfffile))+'"\n\n')
        f.close()
        print 'Backed up file: ' + hfffile
      else:
        print 'Not backed up file: ' + hfffile
    except:
      print 'No associated headerfile for: '+arg
    try:
      hffinfile=str(commands.getoutput('Get <' + hfffile + ' in parform=n')).rstrip().lstrip()
      if os.path.exists(hffinfile):
        commands.getoutput('cp -f ' + hffinfile + ' ' + os.path.join(options.dirname,os.path.basename(hffinfile)))
        f=open(os.path.join(options.dirname,os.path.basename(hfffile)), 'a')
        f.write('sdrToBackup: '+now.strftime("%Y-%m-%d %H:%M"+'\n'))
        f.write('           sets next: in="'+os.path.join(options.dirname,os.path.basename(hffinfile))+'"\n\n')
        f.close()
        print 'Backed up file: ' + hffinfile
      else:
        print 'Not backed up file: ' + hffinfile
    except:
      print 'No associated header datafile for: '+arg
    
  except Exception, err:
    print 'Not backed up file: ' + arg
    print err


