<commit_before>#include "pihm.h"
#include "optparse.h"

void ParseCmdLineParam(int argc, char *argv[], char *outputdir)
{
    int             option;
    struct optparse options;
    struct optparse_long longopts[] = {
        {"output", 'o', OPTPARSE_REQUIRED},
        {"elevation_correction", 'c', OPTPARSE_NONE},
        {"debug", 'd', OPTPARSE_NONE},
        {"tecplot", 't', OPTPARSE_NONE},
        {"verbose", 'v', OPTPARSE_NONE},
        {"print_version", 'V', OPTPARSE_NONE},
        {0, 0, 0}
    };

    optparse_init(&options, argv);

    while ((option = optparse_long(&options, longopts, NULL)) != -1)
    {
        switch (option)
        {
            case 'o':
                /* Specify output directory */
                sprintf(outputdir, "output/%s/", options.optarg);
                break;
            case 'c':
                /* Surface elevation correction mode */
                corr_mode = 1;
                printf("\tSurface elevation correction mode turned on.\n");
                break;
            case 'd':
                /* Debug mode */
                debug_mode = 1;
                printf("\tDebug mode turned on.\n");
                break;
            case 't':
                /* Tecplot output */
                tecplot = 1;
                printf("\tTecplot output turned on.\n");
                break;
            case 'v':
                /* Verbose mode */
                verbose_mode = 1;
                printf("\tVerbose mode turned on.\n");
                break;
            case 'V':
                /* Print version number */
                printf("\nMM-PIHM Version %s.\n", VERSION);
                PIHMexit(EXIT_SUCCESS);
                break;
            case '?':
                printf("Option not recognisable %s\n", options.errmsg);
                break;
            default:
                break;
        }

        fflush(stdout);
    }

    if (options.optind >= argc)
    {
        fprintf(stderr, "Error:You must specify the name of project!\n");
        fprintf(stderr,
            "Usage: ./pihm [-o output_dir] [-c] [-d] [-v] [-V]"
            " <project name>\n");
        fprintf(stderr, "\t-o Specify output directory\n");
        fprintf(stderr, "\t-c Correct surface elevation\n");
        fprintf(stderr, "\t-d Debug mode\n");
        fprintf(stderr, "\t-v Verbose mode\n");
        fprintf(stderr, "\t-V Version number\n");
        PIHMexit(EXIT_FAILURE);
    }
    else
    {
        /* Parse remaining arguments */
        strcpy(project, optparse_arg(&options));
    }
}

void CreateOutputDir(char *outputdir)
{
    time_t          rawtime;
    struct tm      *timestamp;
    char            str[11];
    char            icdir[MAXSTRING];

    if (0 == (PIHMmkdir("output")))
    {
        PIHMprintf(VL_NORMAL, " Output directory was created.\n\n");
    }

    if (outputdir[0] == '\0')
    {
        /* Create default output directory name based on project and time */
        time(&rawtime);
        timestamp = localtime(&rawtime);
        strftime(str, 11, "%y%m%d%H%M", timestamp);
        sprintf(outputdir, "output/%s.%s/", project, str);
    }

    if (PIHMmkdir(outputdir) != 0)
    {
        if (errno != EEXIST)
        {
            PIHMprintf(VL_ERROR,
                "Error creating output directory %s\n", outputdir);
            PIHMexit(EXIT_FAILURE);
        }
        else
        {
            PIHMprintf(VL_NORMAL,
                "Output directory %s already exists. Overwriting.\n",
                outputdir);
        }
    }
    else
    {
        PIHMprintf(VL_NORMAL, "Output directory %s was created.\n", outputdir);
    }

    sprintf(icdir, "%srestart/", outputdir);
    if (PIHMmkdir(icdir) != 0 && errno != EEXIST)
    {
        PIHMprintf(VL_ERROR,
            "Error creating restart directory %s\n", outputdir);
        PIHMexit(EXIT_FAILURE);
    }
}

void BackupInput(const char *outputdir)
{
    char            project[MAXSTRING];
    char            system_cmd[MAXSTRING];
    char            source_file[MAXSTRING];

#if OBSOLETE
    strcpy(tempname, project);
    if (strstr(tempname, ".") != 0)
    {
        token = strtok(tempname, ".");
        strcpy(project, token);
    }
    else
    {
        strcpy(project, project);
    }
#endif

    /* Save input files into output directory */
    sprintf(source_file, "input/%s/%s.para", project, project);
    if (PIHMaccess(source_file, F_OK) != -1)<commit_msg>Fix #11 Backup input files<commit_after>#include "pihm.h"
#include "optparse.h"

void ParseCmdLineParam(int argc, char *argv[], char *outputdir)
{
    int             option;
    struct optparse options;
    struct optparse_long longopts[] = {
        {"output", 'o', OPTPARSE_REQUIRED},
        {"elevation_correction", 'c', OPTPARSE_NONE},
        {"debug", 'd', OPTPARSE_NONE},
        {"tecplot", 't', OPTPARSE_NONE},
        {"verbose", 'v', OPTPARSE_NONE},
        {"print_version", 'V', OPTPARSE_NONE},
        {0, 0, 0}
    };

    optparse_init(&options, argv);

    while ((option = optparse_long(&options, longopts, NULL)) != -1)
    {
        switch (option)
        {
            case 'o':
                /* Specify output directory */
                sprintf(outputdir, "output/%s/", options.optarg);
                break;
            case 'c':
                /* Surface elevation correction mode */
                corr_mode = 1;
                printf("\tSurface elevation correction mode turned on.\n");
                break;
            case 'd':
                /* Debug mode */
                debug_mode = 1;
                printf("\tDebug mode turned on.\n");
                break;
            case 't':
                /* Tecplot output */
                tecplot = 1;
                printf("\tTecplot output turned on.\n");
                break;
            case 'v':
                /* Verbose mode */
                verbose_mode = 1;
                printf("\tVerbose mode turned on.\n");
                break;
            case 'V':
                /* Print version number */
                printf("\nMM-PIHM Version %s.\n", VERSION);
                PIHMexit(EXIT_SUCCESS);
                break;
            case '?':
                printf("Option not recognisable %s\n", options.errmsg);
                break;
            default:
                break;
        }

        fflush(stdout);
    }

    if (options.optind >= argc)
    {
        fprintf(stderr, "Error:You must specify the name of project!\n");
        fprintf(stderr,
            "Usage: ./pihm [-o output_dir] [-c] [-d] [-v] [-V]"
            " <project name>\n");
        fprintf(stderr, "\t-o Specify output directory\n");
        fprintf(stderr, "\t-c Correct surface elevation\n");
        fprintf(stderr, "\t-d Debug mode\n");
        fprintf(stderr, "\t-v Verbose mode\n");
        fprintf(stderr, "\t-V Version number\n");
        PIHMexit(EXIT_FAILURE);
    }
    else
    {
        /* Parse remaining arguments */
        strcpy(project, optparse_arg(&options));
    }
}

void CreateOutputDir(char *outputdir)
{
    time_t          rawtime;
    struct tm      *timestamp;
    char            str[11];
    char            icdir[MAXSTRING];

    if (0 == (PIHMmkdir("output")))
    {
        PIHMprintf(VL_NORMAL, " Output directory was created.\n\n");
    }

    if (outputdir[0] == '\0')
    {
        /* Create default output directory name based on project and time */
        time(&rawtime);
        timestamp = localtime(&rawtime);
        strftime(str, 11, "%y%m%d%H%M", timestamp);
        sprintf(outputdir, "output/%s.%s/", project, str);
    }

    if (PIHMmkdir(outputdir) != 0)
    {
        if (errno != EEXIST)
        {
            PIHMprintf(VL_ERROR,
                "Error creating output directory %s\n", outputdir);
            PIHMexit(EXIT_FAILURE);
        }
        else
        {
            PIHMprintf(VL_NORMAL,
                "Output directory %s already exists. Overwriting.\n",
                outputdir);
        }
    }
    else
    {
        PIHMprintf(VL_NORMAL, "Output directory %s was created.\n", outputdir);
    }

    sprintf(icdir, "%srestart/", outputdir);
    if (PIHMmkdir(icdir) != 0 && errno != EEXIST)
    {
        PIHMprintf(VL_ERROR,
            "Error creating restart directory %s\n", outputdir);
        PIHMexit(EXIT_FAILURE);
    }
}

void BackupInput(const char *outputdir)
{
    char            system_cmd[MAXSTRING];
    char            source_file[MAXSTRING];

#if OBSOLETE
    strcpy(tempname, project);
    if (strstr(tempname, ".") != 0)
    {
        token = strtok(tempname, ".");
        strcpy(project, token);
    }
    else
    {
        strcpy(project, project);
    }
#endif

    /* Save input files into output directory */
    sprintf(source_file, "input/%s/%s.para", project, project);
    if (PIHMaccess(source_file, F_OK) != -1)
{'fraction_non_alphanumeric': 0.13333333333333333, 'fraction_numerical': 0.0045121263395375075, 'mean_word_length': 1.621525724423418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1017362', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2618, 'n_words': 619}