#!/usr/bin/python3
'''
    Conman interface
    Author: Julius Pedersen <deifyed+conman@gmail.com>
'''
# Python libs
import argparse
import os.path

# Custom libs
from libconman import verbose
from libconman.configuration import Configuration
from libconman.vault import Vault

config = Configuration()

DESCRIPTION = '''Makes backuping config files a breeze by hard linking the
config files into a single folder'''

parser = argparse.ArgumentParser(description=DESCRIPTION)
# Verbose
parser.add_argument('-v', '--verbose', action='store_true',
    help='enable verbose messages')
# Alternative conman directory
parser.add_argument('-c', '--conman-directory', metavar='target', type=str,
        help='use target directory as the conman directory')

# Sub parsers
sub_parsers = parser.add_subparsers(help='description')

### Commands
## SYNCHRONIZE
# Short description: conman -h
hlp = 'sync target file(s) to vault'
# Long description: conman sync -h
desc = '''Creates a hard link to the target file(s) inside the conman directory
(default: ~/.conman/).'''
sync_parser = sub_parsers.add_parser('sync', description=desc, help=hlp)
sync_parser.add_argument('synchronize', metavar='target', type=str, nargs='+',
        help='target file(s) or folder(s) to sync')
sync_parser.add_argument('-r', '--recursive', action='store_true',
        help='recursively sync files in target directories and subdirectories')

## DEPLOY
# Short description: conman -h
hlp = 'deploy file(s)'
# Long description: conman deploy -h
desc = '''Hard link items from conman directory (default: ~/.conman/) back to
where they where originally synced'''
deploy_parser = sub_parsers.add_parser('deploy', description=desc, help=hlp)
deploy_parser.add_argument('deploy', metavar='id', nargs='*',
        help='deploy target file(s) with id')
deploy_parser.add_argument('-a', '--all', action='store_true',
        help='deploy all files from vault. Useful when restoring after a format')

# QUERY
parser.add_argument('-q', '--query', action='store_true',
        help='lists all synchronized files')

# REMOVE
# Short description: conman -h
hlp = 'remove link from conman directory'
desc = '''Removes the hard link from conman directory (default: ~/.conman/)'''
remove_parser = sub_parsers.add_parser('remove', description=desc, help=hlp)
remove_parser.add_argument('remove', metavar='id', nargs='+', type=int,
        help='removes file synchronization for target identified by id')

args = parser.parse_args()

### Optional arguments
# Verbose mode
if args.verbose:
    config.VERBOSE = True
# Set alternative root directory
if args.conman_directory:
    config.CONMAN_PATH = os.path.realpath(args.conman_directory)
    verbose('Using alternative conman directory {}'.format(config.CONMAN_PATH))

vault = Vault()
# Print out vault contents 
if args.query:
    print('{:4}\t{:15}\t{}'.format('id', 'name', 'path'))
    for iid, name, path in vault.listTargets():
        print('{:<4}\t{:<15}\t{:<}'.format(iid, name, path))

### Required arguments
if 'synchronize' in args:
    print('Synchronizing {}'.format(args.synchronize))
    vault.secure(args.synchronize, args.recursive)

if 'deploy' in args:
    if len(args.deploy) == 0:
        if args.all:
            print('Deploying all items from vault')
            vault.deployAll()
        else:
            print('Deploying all items requires the -a/--all flag for confirmation')
    else:
        print('Deploying {}'.format(args.deploy))
        vault.deploy(args.deploy)

if 'remove' in args:
    print('Removing {}'.format(args.remove))
    vault.remove(args.remove)
