# Copyright (c) 2012 by Zuse-Institute Berlin and the Technical University of Denmark.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holders nor contributors may not
#        be used to endorse or promote products derived from this software
#        without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS NOR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Direct execution requires top level directory on python path                          
if __name__ == "__main__":
  import os, sys, inspect
  scriptdir = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
  packagedir = os.path.realpath(os.path.abspath(os.path.join(scriptdir,'..')))
  if packagedir not in sys.path:
    sys.path.insert(0, packagedir)


import csv, os, inspect

class textable:
  def opentable(self):
    print('% Requires package "longtable" and "booktabs"')
    print('\\begin{scriptsize}')
    print('\\begin{longtable}{>{\\raggedright\\arraybackslash}p{0.45\\linewidth}p{0.5\\linewidth}}')
    print('\\toprule')
    print(' '.join(['Instances', '&', 'Origin and description','\\\\']))
    print('\\midrule')

  def closetable(self):
    print('\\bottomrule')
    print('\\hiderowcolors')
    print('\\caption{\\cblibreftablecaption}')
    print('\\label{cblib:reftabel}')
    print('\\end{longtable}')
    print('\\end{scriptsize}')

  def addrow(self, instances, contributor, refkeys, text):
    print(instances.replace('_','\_') + ' & ')
    print(''.join(['\\textsf{', \
                   ', '.join([''.join(['\\citet{', x, '}']) for x in refkeys.replace(' ','').split(',')]), \
                   '.}\\newline']))
    print(text + '\\\\')
    print('%')


def reftable():
  # Find the directory of this script
  scriptdir = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
  rootdir = os.path.join(scriptdir,'..','..')

  out = textable()
  out.opentable()

  csvfile = open(os.path.join(rootdir,'instances','ref.csv'), 'rt')
  csvreader = csv.reader(csvfile, delimiter=';', quotechar='"')
  next(csvreader)
  for row in csvreader:
    out.addrow(row[0],row[1],row[2],row[3])
  csvfile.close()

  out.closetable()

if __name__ == "__main__":
  try:
    # Print reference table
    reftable()

  except Exception as e:
    print(str(e))

