#!/usr/bin/env python3
#   Copyright (C) 2013 Canonical Ltd.
#
#   Author: Scott Moser <scott.moser@canonical.com>
#
#   Simplestreams is free software: you can redistribute it and/or modify it
#   under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or (at your
#   option) any later version.
#
#   Simplestreams 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 Affero General Public
#   License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with Simplestreams.  If not, see <http://www.gnu.org/licenses/>.

import os
import os.path
import sys

from sign_helper import signjson_file


def status_cb(fname):
    sys.stderr.write("%s\n" % fname)


def main():
    force = False
    if len(sys.argv) > 1 and sys.argv[1] == "--force":
        sys.argv = sys.argv[1:]
        force = True

    for path in sys.argv[1:]:
        if os.path.isfile(path):
            if not path.endswith(".json"):
                sys.stderr.write("file must end with .json\n")
                sys.exit(1)
            signjson_file(path, force=force)
        elif os.path.isdir(path):
            for root, _dirs, files in os.walk(path):
                for f in [f for f in files if f.endswith(".json")]:
                    signjson_file(os.path.join(root, f),
                                  status_cb=status_cb, force=force)
        else:
            sys.stderr.write("input must be file or dir\n")
            sys.exit(1)


if __name__ == '__main__':
    main()
