#!/usr/bin/python3
# Copyright (C) 2011 Canonical
#
# Author:
#  Michael Vogt
#
# This program 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; version 3.
#
# 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.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


import argparse
import datetime
import logging
import os
import string
import subprocess
import sys
import tempfile

import gettext
from gettext import gettext as _

from apt_btrfs_snapshot import AptBtrfsSnapshot


if __name__ == "__main__":

    gettext.bindtextdomain("apt-btrfs-snapshot")

    # command line parser
    description = _("Filesystem snapshot support for apt")
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("--debug", action="store_true", default=False,
                        help="enable debug output")
    subparser = parser.add_subparsers(title="Commands")
    # supported
    command = subparser.add_parser(
        "supported", help=_("Print if snapshots are supported"))
    command.set_defaults(command="supported")
    # list
    command = subparser.add_parser(
        "list", help=_("List available snapshots"))
    command.set_defaults(command="list")
    # snapshot
    command = subparser.add_parser(
        "snapshot", help=_("Create a new snapshot"))
    command.set_defaults(command="snapshot")
    # set-default
    command = subparser.add_parser(
        "set-default", help=_("Revert to previous snapshot"))
    command.add_argument("snapshot")
    command.set_defaults(command="set-default")
    # delete
    command = subparser.add_parser(
        "delete", help=_("Delete snapshot"))
    command.add_argument("snapshot")
    command.set_defaults(command="delete")
    # list-older-than
    command = subparser.add_parser(
        "list-older-than", help=_("Show snapshots older than N days"))
    command.add_argument("time")
    command.set_defaults(command="list-older-than")
    # delete-older-than
    command = subparser.add_parser(
        "delete-older-than", help=_("Delete snapshots older than N days"))
    command.add_argument("time")
    command.set_defaults(command="delete-older-than")

    # parse args
    args = parser.parse_args()

    # global options
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if os.getuid() != 0:
        print(_("Sorry, you need to be root to run this program"))
        sys.exit(1)

    apt_btrfs = AptBtrfsSnapshot()
    if not apt_btrfs.snapshots_supported():
        print(_("The system does not support apt-btrfs-snapshot"))
        sys.exit(1)

    res = False
    if args.command == "supported":
        res = apt_btrfs.snapshots_supported()
        if res:
            print(_("Supported"))
        else:
            print(_("Not supported"))
    elif args.command == "snapshot":
        res = apt_btrfs.create_btrfs_root_snapshot()
    elif args.command == "list":
        res = apt_btrfs.print_btrfs_root_snapshots()
    elif args.command == "set-default":
        res = apt_btrfs.command_set_default(args.snapshot)
    elif args.command == "delete":
        res = apt_btrfs.delete_snapshot(args.snapshot)
    elif args.command == "list-older-than":
        res = apt_btrfs.print_btrfs_root_snapshots_older_than(args.time)
    elif args.command == "delete-older-than":
        res = apt_btrfs.clean_btrfs_root_snapshots_older_than(args.time)
    else:
        print(_("ERROR: Unhandled command: '%s'") % args.command)

    # return the right exit code
    if res:
        sys.exit(0)
    else:
        sys.exit(1)




