#!/usr/bin/python

#Copyright Jonathan Riddell 2013
# may be copied under the terms of the GNU GPL v 3 or later
#nicer than upstream2orig in calligra-l10n

import re
import glob
import argparse
import subprocess
import os
import shutil

renameLangs = {
               "pt_BR": "ptbr",
               "en_GB": "engb",
               "ca@valencia": "cavalencia",
               "zh_CN": "zhcn",
               "zh_TW": "zhtw"
              }

parser = argparse.ArgumentParser(description="Upload new KDE SC version to the PPA. Downloads to current working directory.")
parser.add_argument("-v", "--version", required=True, help="Upstream version")
parser.add_argument("-o", "--oldversion", required=True, help="Current version in archive")

args = parser.parse_args()
version = args.version
oldversion = args.oldversion

versionParts = version.split(".")
lastDigit = int(versionParts[-1])

if lastDigit >= 80:
    stability = "unstable"
else:
    stability = "stable"

# fetch orig tarball
remote = "depot.kde.org:/home/ftpubuntu/%s/calligra-%s/calligra-l10n/*tar*" % (stability, version)
subprocess.check_call(["rsync", "--progress", "-CvzapP", "--compress-level=9", "-e ssh", remote, "."])

tars = glob.glob("calligra-l10n-*-%s.tar.xz" % (version))
for tar in tars:
  print tar
  result = re.match(r"calligra-l10n-(.*)-2.(.*).tar.xz", tar)
  lang = result.group(1)
  if lang in renameLangs:
    lang = renameLangs[lang]
  print "lang: " + lang
  try:
    os.symlink(tar, "calligra-l10n_%s.orig-%s.tar.xz" % (version, lang))
  except OSError:
    pass #file exists
  #os.rename(tar, "calligra-l10n_%s.orig-%s.tar.xz" % (version, lang))


subprocess.check_call(["wget", "-c",
                       "https://launchpad.net/ubuntu/+archive/primary/+files/calligra-l10n_%s-0ubuntu1.debian.tar.bz2" % (oldversion)])

subprocess.check_call(["tar", "xf", "calligra-l10n_%s-0ubuntu1.debian.tar.bz2" % (oldversion)])

try:
  os.mkdir("calligra-l10n-%s" % (version))
except OSError:
  pass
subprocess.check_call(["tar", "zcf", "calligra-l10n_%s.orig.tar.gz"%(version), "calligra-l10n-%s"%(version)])

for tar in tars:
  result = re.match(r"calligra-l10n-(.*)-2.(.*).tar.xz", tar)
  lang = result.group(1)
  upstreamLang = lang
  if lang in renameLangs:
    lang = renameLangs[lang]
  print [ "tar", "xf", "calligra-l10n_%s.orig-%s.tar.xz"%(version, lang) ]
  subprocess.check_call([ "tar", "xf", "calligra-l10n_%s.orig-%s.tar.xz"%(version, lang) ])
  try:
    os.rename("calligra-l10n-%s-%s"%(upstreamLang,version), "calligra-l10n-%s/%s"%(version, lang))
  except OSError:
    print "dir already exists %s"%(lang)

os.chdir("calligra-l10n-%s" % (version))

subprocess.check_call(["rm", "-rf", "debian/"])

try:
  shutil.copytree("../debian", "debian")
except OSError:
  print str(OSError.message) #FIXME umm what?

subprocess.check_call(["dch", "--newversion", "1:%s-0ubuntu1" % (version), "--distribution", "saucy", "New upstream release"])

os.chdir("..")
os.chdir("calligra-l10n-%s" % (version))




#orig = "%s_%s.orig.tar.xz" % (package, version)
#os.rename("%s-%s.tar.xz" % (upstreamName(package), version), orig)
