#!/usr/bin/python
# -*- coding: utf-8 -*-
#    Copyright (C) 2005, 2006, 2007 Stewart Adam
#    This file is part of fwbackups.

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

#    fwbackups is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with fwbackups; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
import os
import sys
import time
import signal
from fwbackups import backend
from fwbackups.i18n import _
from fwbackups import utils

def usage():
    print _("""
Usage: fwbackups-run SET_NAME [ SET_NAMES ]
  SET_NAME:\tThe set name to backup, without the .conf suffix.
  [SET_NAMES]:\tOptional arguments, if more than one set name is specified all will be processed.
""")

def handleStop(arg1, arg2):
    """ Handles a siging """
    backupHandle.cancel()

signal.signal(signal.SIGINT, handleStop)
if not len(sys.argv) > 1:
    print _('ERROR: Requires at least one argument')
    usage()
    sys.exit(1)
else:
    for i in sys.argv[1:]:
        try:
            backupHandle = backend.Automated(i)
            backupThread = utils.FuncAsThread(backupHandle.backup)
        except Exception, e:
            print _('ERROR: Cannot setup threads. Exiting.')
            print _('Error was:\n%s' % str(e))
            sys.exit(1)
        backupThread.start()
        while backupThread.isAlive():
            time.sleep(0.02)
        
