#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
padlock: lock/unlock EncFS-encrypted directory with GPG key/passphrase
"""
# Copyright (C) 2014 Hartmut Goebel <h.goebel@crazy-compilers.com>
# Part of the DebOps project - http://debops.org/

# 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; either version 3 of the License,
# or (at your option) any later version.
#
# 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., 59
# Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# An on-line copy of the GNU General Public License can
# be downloaded from the FSF web page at:
# http://www.gnu.org/copyleft/gpl.html

import os
import argparse

from debops import *
from debops.cmds import *

__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
__copyright__ = "Copyright 2014 by Hartmut Goebel <h.goebel@crazy-compilers.com>"
__licence__ = "GNU General Public License version 3 (GPL v3) or later"


def main(action):
    require_commands('encfs', 'fusermount', 'gpg')

    # Get the absolute path to script's directory
    encfs_encrypted = os.path.dirname(os.path.realpath(__file__))

    if action == 'lock':
        # Unmount the directory if mounted ...
        if padlock_lock(encfs_encrypted):
            print("Locked!")
        else:
            print("Is already locked.")
    elif action == 'unlock':
        # ... or mount it if unmounted
        if padlock_unlock(encfs_encrypted):
            print("Unlocked!")
        else:
            print("Is already unlocked.")

parser = argparse.ArgumentParser()
parser.add_argument('action', choices=['lock', 'unlock'])
args = parser.parse_args()

try:
    main(args.action)
except KeyboardInterrupt:
    raise SystemExit('... aborted')
