/*
This file is part of dia2code. It generates code from an UML Dia Diagram.
Copyright (C) 2001-2014 Steffen Macke

This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "dia2code.hpp"

void generate_code_shp(batch *b) {
    /*
    umlclasslist tmplist,parents,dependencies;
    umlassoclist associations;
    namelist used_classes,tmpnamelist;
    umlattrlist umla,tmpa;
    umloplist umlo;
    */
    umlclasslist tmplist;
    umlclasslist parentlist, parentlist2;
    umlattrlist umla;
    char *tmpname;
    char outfilename[BIG_BUFFER];
    FILE * outfileshp, *dummyfile;

    int tmpdirlgth, tmpfilelgth;

    if (b->outdir == NULL) {
        b->outdir = ".";
    }

    tmpdirlgth = strlen(b->outdir);

    tmplist = b->classlist;

    while ( tmplist != NULL ) {

        if ( ! ( is_present(b->classes, tmplist->key->name) ^ b->mask ) ) {

            tmpname = strtolower(tmplist->key->name);

            /* This prevents buffer overflows */
            tmpfilelgth = strlen(tmpname);
            if (tmpfilelgth + tmpdirlgth > sizeof(*outfilename) - 2) {
                fprintf(stderr, "Sorry, name of file too long ...\nTry a smaller dir name\n");
                exit(4);
            }

            sprintf(outfilename, "%s/createshapefiles.bat", b->outdir);
            dummyfile = fopen(outfilename, "r");
            if ( b->clobber || ! dummyfile ) {

                outfileshp = fopen(outfilename, "a");
                if ( outfileshp == NULL ) {
                    fprintf(stderr, "Can't open file %s for writing\n", outfilename);
                    exit(3);
                }

                /* This prevents buffer overflows */
                tmpfilelgth = strlen(tmpname);
                if (tmpfilelgth + tmpdirlgth > sizeof(*outfilename) - 2) {
                    fprintf(stderr, "Sorry, name of file too long ...\nTry a smaller dir name\n");
                    exit(4);
                }

                free(tmpname);

                if(tmplist->key->isabstract == 0) {

                    /* create attribute table */
                    umla = tmplist->key->attributes;
                    parentlist = tmplist;
                    fprintf(outfileshp, "dbfcreate %s", tmplist->key->name);

                    while ( umla != NULL) {
                        if((strcmp(umla->key.name,"Shape") != 0)&&
                           (umla->key.visibility != 1)) {
                            if(strcmp(umla->key.type,"String") == 0) {
                                fprintf(outfileshp, " -s %s 255",
                                        umla->key.name);
                            }
                            else if((strcmp(umla->key.type,
                                           "CodedValue") == 0)||
                                strcmp(umla->key.type, "Integer") ==0 ) {
                                fprintf(outfileshp, " -n %s 16 0",
                                         umla->key.name);
                            }
                            else if(strcmp(umla->key.type, "Float") == 0) {
                                fprintf(outfileshp, " -n %s 16 3",
                                        umla->key.name);
                            }
                        }
                        umla = umla->next;

                        if((umla == NULL)&&(parentlist != NULL)) {
                            parentlist = parentlist->parents;
                            if(parentlist != NULL) {
                                umla = parentlist->key->attributes;
                                parentlist2 = b->classlist;
                                while((strcmp(parentlist->key->name,
                                              parentlist2->key->name) != 0)&&
                                      (parentlist2 != NULL))
                                    parentlist2 = parentlist2->next;
                                parentlist = parentlist2;
                            }
                        }

                    }
                    fprintf(outfileshp, "\n");

                    /* create shp file */
                    umla = tmplist->key->attributes;
                       parentlist = tmplist;
                    while ( umla != NULL) {
                        if(strcmp(umla->key.name,"Shape") == 0) {
                            if(strcmp(strtolower(umla->key.type),
                                      "polyline") == 0) {
                                strcpy(umla->key.type, "arc");
                            }
                            fprintf(outfileshp, "shpcreate %s %s\n\n",
                                    tmplist->key->name,
                                    strtolower(umla->key.type));
                            break;
                        }
                        umla = umla->next;

                        if((umla == NULL)&&(parentlist != NULL)) {
                            parentlist = parentlist->parents;
                            if(parentlist != NULL) {
                                umla = parentlist->key->attributes;
                                parentlist2 = b->classlist;
                                while((strcmp(parentlist->key->name,
                                              parentlist2->key->name) != 0)&&
                                      (parentlist2 != NULL))
                                    parentlist2 = parentlist2->next;
                                parentlist = parentlist2;
                            }
                        }

                    }
                }
                fclose(outfileshp);
            }
        }
        tmplist = tmplist->next;
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
