#!/usr/bin/env python
#python version 2.4.3 - 2.7.1
#backup
#cp abc .abc_20140514182828_username

import os
import sys
import shutil
import time

date_now = str(time.strftime("%Y%m%d%H%M%S"))
whoami = str(os.getlogin())

def backup(file_name):
	'''
Name: bakcup Version: 0.1.2
usage:
	backup src(abs path or Relative path of current path)
update:
	backup -u

Website: https://github.com/cjsund/lazy_tools
    '''
	name = os.path.basename(file_name)
	if not os.path.exists(file_name):
		print "\033[31m %s: No such file or directory\033[0m" % name
		sys.exit(0)

	new_file_name = '_'.join([".%s" % file_name, date_now, whoami])

	try:
		if os.path.isfile(file_name):
			shutil.copyfile(file_name, new_file_name)
		else:
			shutil.copytree(file_name, new_file_name)
	except shutil.Error, e:
		print "\033[31m %s \033[0m" % e
	except IOError, e:
		print "\033[31m %s \033[0m" % e

argv = sys.argv
if len(argv) is 1 or len(sys.argv) >= 3:
	print backup.__doc__
elif argv[1] == "-u":
	os.popen('curl -sSL https://raw.githubusercontent.com/cjsund/lazy_tools/master/install/online_install | bash -s update')
	#os.system('ls')
else:
	file_name = sys.argv[1]
	backup(file_name)