#!/usr/bin/python
#
# Snap! command line interface
#
# (C) Copyright 2011 Mo Morsi (mo@morsi.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, Version 3,
# as published by the Free Software Foundation
#
# This program 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.

import sys
import snap
import snap.config
import snap.callback

class ConsoleCallback(snap.callback.Callback):
    def message(self, msg):
        try:
            print msg
        except UnicodeEncodeError, e:
            pass

    def warn(self, warning):
        try:
            print 'WARNING: ' + warning
        except UnicodeEncodeError, e:
            pass

    def error(self, error):
        try:
            print 'ERROR: ' + error
        except UnicodeEncodeError, e:
            pass

def main():
    try:
        snap.callback.snapcallback=ConsoleCallback()

        conf = snap.config.Config()
        conf.read_config()
        conf.parse_cli()
        conf.verify_integrity()

        snapbase = snap.SnapBase()

        if snap.config.options.mode == snap.config.options.RESTORE:
            snapbase.restore()
            return 0
        if snap.config.options.mode == snap.config.options.BACKUP:
            snapbase.backup()
            return 0

        return 1

    except (snap.exceptions.SnapError), e:
        snap.callback.snapcallback.error(e.message)
        return 1

if __name__ == "__main__":
    sys.exit(main())
