#!/usr/bin/env python2.7
import subprocess, urllib2, os

remote_dir  = "."
remote_url  = "http://community.elphel.com/files/production/"
private_key_path = "/home/user/.ssh/id_rsa"

production_dir = "production"
bkp_dir        = "uploaded"
rsync          = "rsync --ignore-existing -ashvv --exclude */ --exclude README . "+remote_dir
ssh            = ["-e","ssh -i "+private_key_path]
rsync = rsync.split()
rsync.extend(ssh)

#list of files - needs to be manually saved
source_files_extensions     = [".stp",".dxf",".igs",".dwb",".bmp",".png",".jpg",".pdf~"]

production_files_extensions = [".dwb.tar.gz",".dxf.tar.gz",".igs.tar.gz",".jpeg",".pdf",".stp.tar.gz"]
# file extension in design/xxxx-xx/xxx.dwb
design_files_extensions     = [".dwb"]

#change to production_dir/
os.chdir(os.getcwd()+"/"+production_dir)
#archive and convert file in production_dir
subprocess.check_output("../archive_stp_dxf_pdf_any_year.sh", stderr=subprocess.STDOUT);

#analyze production files
error_counter = 0

for file in os.listdir("."):
  if (file.endswith(".dwb.tar.gz")):
    #file_stripped=file.strip(".dwb.tar.gz")
    file_stripped=file[:-len(".dwb.tar.gz")]
    print "Processing "+file_stripped
    #download the list of production files
    if (urllib2.urlopen(remote_url).read().find(file)!=-1): 
      print "ERROR("+file_stripped+"): Already in production"
      error_counter-=1
    else:
      for ext in production_files_extensions:
	if (not os.path.isfile(file_stripped+ext)):
	  print "ERROR("+file_stripped+"): Missing production file: "+file_stripped+ext
	  error_counter-=1
      if (error_counter==0):
	#move original source files to production_dir/bkp_dir
	for ext in source_files_extensions:
	  #move file to bkp_dir
	  if (os.path.isfile(file_stripped+ext)): os.rename(os.getcwd()+"/"+file_stripped+ext,os.getcwd()+"/"+bkp_dir+"/"+file_stripped+ext)
	#change .dwb to read-only but executable (as git supports only 0644 and 0755)
	for ext in design_files_extensions:
	  #print "Looking for "+file_stripped+ext
	  for root,dirs,files in os.walk(os.getcwd()+"/../design"):
	    for name in files:
	      if (name==file_stripped+ext):
		#git supports only 0644 and 0755
		os.chmod(os.path.join(root, name),0555)
		#commit change
		subprocess.check_output(["git","commit","-am","Production"], stderr=subprocess.STDOUT);
		break

if (error_counter==0):
  #run rsync
  subprocess.check_output(rsync, stderr=subprocess.STDOUT)
  #move all files from production_dir to production_dir/bkp_dir
  for file in os.listdir("."):
    if (os.path.isfile(file))and(file!="README"): os.rename(os.getcwd()+"/"+file,os.getcwd()+"/"+bkp_dir+"/"+file)

  print "PRE-PUSH HOOK: SUCCESS"

exit(error_counter)