# Copyright (C) 2016-2018 The ESPResSo project
# Copyright (C) 2014 Olaf Lenz
#
# This file is part of ESPResSo.
#
# ESPResSo 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.
#
# ESPResSo 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This script generates gen_pxiconfig.cpp, which in turn generates myconfig.pxi.
#
from __future__ import print_function
import inspect
import sys
import os
# find featuredefs.py
moduledir = os.path.dirname(inspect.getfile(inspect.currentframe()))
sys.path.append(os.path.join(moduledir, '..', '..'))
import featuredefs

if len(sys.argv) != 3:
    print("Usage: {} DEFFILE CPPFILE".format(sys.argv[0]), file=sys.stderr)
    exit(2)

deffilename, cfilename = sys.argv[1:3]

print("Reading definitions from " + deffilename + "...")
defs = featuredefs.defs(deffilename)
print("Done.")

# generate cpp-file
print("Writing " + cfilename + "...")
cfile = open(cfilename, 'w')

cfile.write("""
#include "config.hpp"
#include <iostream>
using namespace std;
int main() {

cout << "# This file was autogenerated." << endl
     << "# Do not modify it or your changes will be overwritten!" << endl;

""")

template = """
#ifdef {0}
cout << "DEF {0} = 1" << endl;
#else
cout << "DEF {0} = 0" << endl;
#endif
"""

for feature in defs.allfeatures:
    cfile.write(template.format(feature))

cfile.write("""
}
""")

cfile.close()
print("Done.")
