#!/usr/bin/python
# -*- coding: utf-8 -*-
# Helper functions to transform data from CSV files into haskell files

import csv
import os
import sys
import re

def transform_data(input_file, output_file):
    filename:str= os.path.splitext(os.path.basename(output_file))[0]
    with open(input_file, 'r') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        with open(output_file, 'w') as f:
            f.write('module '+filename+' where\n')
            f.write(filename.lower()+' = [\n')
            for row in reader:
                if reader.line_num == 1:
                    continue
                if reader.line_num == 2:
                    if len(row) > 0:
                        f.write('    ("' + row[0] + '", "' + row[1] + '")')
                else:
                    if len(row) > 0:
                        f.write(',\n    ("' + row[0] + '", "' + row[1] + '")')
            f.write('\n    ]\n')
            f.close()
    print('Data transformed from ' + input_file + ' to ' + output_file)
    
if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: transform_data.py input_file output_file')
    else:
        transform_data(sys.argv[1], sys.argv[2])