#!/usr/bin/env python

#    Copyright (C) 2014 Christian T. Jacobs, Alexandros Avdis, Gerard J. Gorman, Matthew D. Piggott.

#    This file is part of PyRDM.

#    PyRDM 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 3 of the License, or
#    (at your option) any later version.
#
#    PyRDM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PyRDM.  If not, see <http://www.gnu.org/licenses/>.

import ConfigParser
import glob
import logging
import argparse
import os
import re
import sys

import git
from xml.dom import minidom
pyrdm_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir)
sys.path.insert(0, pyrdm_path)

import pyrdm
from pyrdm.publisher import Publisher

_LOG = logging.getLogger(__name__)
_HANDLER = logging.StreamHandler()
_LOG.addHandler(_HANDLER)
_HANDLER.setFormatter(logging.Formatter(
    '%(module)s %(levelname)s: %(message)s'))
del(_HANDLER)
_LOG.setLevel(logging.DEBUG)

class PyRDMPublish:

   def __init__(self):
      return

   def publish(self, path, version=None, private=False):
      """ Publish the PyRDM source code. """

      self.publisher = Publisher(service="figshare")

      # Publish the software
      pid, doi = self.publisher.publish_software(name="PyRDM", local_repo_location=path, version=version, private=private)
      _LOG.info("PyRDM has been published. Publication ID: %s, DOI: %s" % (pid, doi))
      return
         
if(__name__ == "__main__"):
   # Parse the command line arguments
   parser = argparse.ArgumentParser(prog="pyrdm-publish", description="Publishes the PyRDM source code to an online citable repository.")
   parser.add_argument("-v", "--version", help="Publish a specific version of the PyRDM source code identified by a given SHA-1 hash.", action="store", type=str, default=None, metavar="HASH")
   parser.add_argument("-l", "--log-level", action="store", type=str, metavar="LEVEL", default=None, choices=['critical', 'error', 'warning', 'info', 'debug'], help=("Log verbosity. Defaults to %s" % (logging.getLevelName(pyrdm.LOG.level).lower())))
   parser.add_argument("path", help="The path to the local Git repository of PyRDM on your file system.", action="store", type=str)
   args = parser.parse_args()
   
   if(args.log_level):
      level = getattr(logging, args.log_level.upper())
      pyrdm.LOG.setLevel(level)
      _LOG.setLevel(level)
      
   if(os.path.exists(args.path)):
      rdm = PyRDMPublish()
      rdm.publish(path = args.path, version = args.version, private = True)
   else:
      _LOG.error("The path to the local PyRDM directory does not exist.")
      sys.exit(1)
   
