#!/usr/bin/python3
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4

############################################################################
#   Copyright © 2015 José Manuel Santamaría Lema <panfaust@gmail.com>      #
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
############################################################################

import subprocess
import sys
import argparse

from debian.changelog import Changelog, Version

from lib.utils import *

#Parse arguments
parser = argparse.ArgumentParser(description="Uploads the source package.")
parser.add_argument("-t", "--target", help="dput/dupload host", required=True)
args = parser.parse_args()
host = args.target

#Find out some data about the source package
changelog = Changelog()
changelog.parse_changelog(open('./debian/changelog', 'r'))

src_pkg = changelog.get_package()
version = changelog.upstream_version
if changelog.debian_revision != None:
    version += "-" + changelog.debian_revision
    upload_area_dir = readConfigFile("areas.json")["upload-area"]


config = readConfigFile("uploadsource.json")
config_areas = readConfigFile("areas.json")
changes_file = config_areas["upload-area"] + "/" + src_pkg + "_" + version + "_source.changes"
command = config["upload_command"]
command = command % (host, changes_file)

#Upload the source package
returncode = subprocess.call(command,shell=True)
sys.exit(returncode)
