# Copyright (C) 2022 Jake Gordon, <jacob.gordon@duke.edu>
#
#    This file is part of Babase.
#
#    Babase 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.
#
#    This program 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 Babase.  If not, see <http://www.gnu.org/licenses/>.
#
# Read the file at ./all_comments.txt, which contains a list of
# objects and the "comment" the object should have in the DB.  Then
# use the data in that file to write SQL that will add those comments
# in the DB. Print all output so we can use it in stdout.
#
# We could EASILY paste the contents of all_comments.txt into an m4
# file and use m4 to parse them.  Adding the SAME comments for all the
# views and tables-as-views in the babase_xxx_views schemas is not so
# simple, however.  Use Python here, and not m4, so that we're being
# consistent with how we're handling those views.
#

def writeComment(object_type, object_name, object_comment):
    '''Print an SQL "COMMENT ..." statement with these parameters'''
    print ('''COMMENT ON {0} {1}
  IS '{2}';
'''.format(object_type, object_name, object_comment))

print ("""-- Copyright (C) 2022 Jake Gordon, <jacob.gordon@duke.edu>
--
--    This file is part of Babase.
--
--    Babase 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.
--
--    This program 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 Babase.  If not, see <http://www.gnu.org/licenses/>.
--

-- DO NOT EDIT THIS FILE.  It was automatically generated.  Edit the
-- makecomments.py or all_comments.txt file instead.  (This file
-- _should_ be re-created by typing 'make' at the command line.)
""")


# Open the file
commentsFile = open("./all_comments.txt", "r")
line = commentsFile.readline() # Skip header
line = commentsFile.readline()
while line:
    thisLine = line.strip().split("\t")
    writeComment(thisLine[0], thisLine[1], thisLine[2])
    line = commentsFile.readline()

commentsFile.close()

print()
