#!/usr/bin/env python2
# -*- encoding: utf-8 -*-
from __future__ import print_function
from __future__ import with_statement

import argparse
import os.path
import sys

import colorgpgletter.common
import colorgpgletter.encrypt
import colorgpgletter.decrypt

parser = argparse.ArgumentParser()


parser.add_argument("source", help="The file to encrypt or decrypt (based on extension)", metavar="SOURCE", default=sys.stdin)
parser.add_argument("dest", help="The destination", metavar="DEST", default=sys.stdout)

parser.add_argument("-o", "--output", type=str, help="The output file if source is stdin")

group = parser.add_mutually_exclusive_group()

group.add_argument("-d", "--decrypt", action="store_true", help="Force decryption")
group.add_argument("-e", "--encrypt", action="store_true", help="Force encryption")

parser.add_argument("-c", "--colorfile", type=str, help="The color file to use, defaults to colors.json", metavar="FILE")
parser.add_argument("-f", "--pixelfactor", type=int, help="The factor to be used as pixelsize", metavar="N", default=1)

args = parser.parse_args()

if not args.colorfile:
    colorfile = None
else:
    colorfile = args.colorfile

colors = colorgpgletter.common.get_colors(colorfile)

if args.encrypt:
    doencrypt = True
elif args.decrypt:
    doencrypt = False
elif not args.encrypt and not args.decrypt:
    if args.source == sys.stdin:
        doencrypt = True
    elif os.path.splitext(args.source)[1] == ".asc":
        doencrypt = True
    elif os.path.splitext(args.source)[1] == ".png":
        doencrypt = False

if args.output:
    args.dest = args.output

if doencrypt:
    colorgpgletter.encrypt.encrypt(infile=args.source, outfile=args.dest, colors=colors, factor=args.pixelfactor)
else:
    colorgpgletter.decrypt.decrypt(infile=args.source, outfile=args.dest, colors=colors, factor=args.pixelfactor)
