#!/usr/bin/python

""" 	A small script to compress files using tar (because 
	I never remember the options)
"""

import sys, subprocess, datetime

helpstring = """
	pack is a script that uses tar to compress files.

	Usage: pack <file or directory> [-o <output name>] [-d]
	Options:
		-h		prints this help
		-o <name>	specifies output filename
		-d		appends the current date and time to the output 
				filename

	Examples:
		pack my_folder/
		results in my_folder.tar.gz

		pack myfile.txt -d
		results in myfile.txt.2011-11-24.00h01m.tar.gz
"""
# bz2 -> -j
#tar cvzf $1.tar.gz $1

cmd = "tar"
arg = "czf"	# Add v for verbose mode; j for bz2 instead of z
inp = None
out = None
date = ""

if len(sys.argv) < 2:
	print "Usage: pack <file or directory> [-o <output name>] [-d]"
	sys.exit(0)

# Parse args
i = 1
while i < len(sys.argv):
	curr = sys.argv[i]
	if curr.startswith("-"):
		if curr[1] == "o":
			i += 1
			out = sys.argv[i]
		elif curr[1] == "d":
			date = datetime.datetime.now().strftime(".%Y-%m-%d.%Hh%Mm")
		elif curr[1] == "h":
			print helpstring
			sys.exit(0)
		else:
			raise Exception("Unknown option")
	else:
		inp = curr
	i += 1

if inp.endswith("/"):
	inp = inp[:len(inp)-1]

if out is None:
	out = inp

out = out + date + ".tar.gz"

callstr = cmd + " " + arg + " " + out + " " + inp
print callstr

subprocess.call(callstr, shell=True)

