#!/usr/bin/env python3
#
# Copyright (C) 2012-2017 Chris Cummins.
#
# This file is part of emu.
#
# Emu 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 3 of the License, or (at your
# option) any later version.
#
# Emu 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 emu.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import print_function

import re
import sys
import os

from emu import *


def main(argv, argc):
    parser = Parser()
    parser.add_option("-d", "--dry-run", action="store_true",
                      dest="dry_run", default=False)
    parser.add_option("-f", "--force", action="store_true",
                      dest="force", default=False)
    parser.add_option("-i", "--ignore-errors", action="store_true",
                      dest="ignore_errors", default=False)
    parser.add_option("--no-archive", action="store_true",
                      dest="no_archive", default=False)
    parser.add_option("--no-owner", action="store_true",
                      dest="no_owner", default=False)
    (options, args) = parser.parse_args()

    source = Source(options.source_dir)
    sinks = parser.parse_sinks(source)

    # Fail if source has no sinks:
    if not len(sinks):
        print("Source has no sinks!")
        sys.exit(1)

    status = 0
    for sink in sinks:
        status ^= sink.push(force=options.force,
                            ignore_errors=options.ignore_errors,
                            archive=not options.no_archive,
                            owner=not options.no_owner,
                            dry_run=options.dry_run)

    return status


if __name__ == "__main__":
    argv = sys.argv[1:]
    ret = main(argv, len(argv))
    sys.exit(ret)
