#include "../rss/rss.h"

#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "../tmp/INSTALL.im"

#ifdef SUN
#include <memory.h>
#endif


typedef enum
{
    f_preProcess    =   (1 << 0),       /* command-line action options */
    f_compile       =   (1 << 1),

    f_icmexec       =   (1 << 2),
    f_im            =   (1 << 3),
    f_tmpBim        =   (1 << 4),
                                        /* selects all command-line actions */
    f_commandMask   =   ((1 << 6) - 1),

    f_about         =   (1 << 6),       /* command-line info requests */
    f_help          =   (1 << 7),

    f_rmBim         =   (1 << 8),       /* removal flags        */
    f_rmPim         =   (1 << 9),

    f_doPreProcess  =   (1 << 10),      /* action flags         */
    f_doCompile     =   (1 << 11),
    f_doExec        =   (1 << 12),
    f_doIcmDep      =   (1 << 13),
    f_force         =   (1 << 14),
    f_showFlags     =   (1 << 15)

} FLAGS_;

extern char icm_pre[];
extern char icm_comp[];
extern char icm_exec[];
extern char icm_pp[];

extern char *tmpDir;
extern char *imFile;
extern char *pimFile;
extern char *bimFile;

extern FILE *fdest;
extern FLAGS_ flags;
extern unsigned execArgIdx;
                                                
void noOptions(char **argv);
unsigned dashesIndex(char **begin, char **end);

void optCompile(char **argv);
void optExecute();
void optIm(char **argv);
void optPreProcess(char **argv);
void optTmpBim(char **argv);
void optTmpDir(void);

void options(int argc, char **argv);

void    cleanup(void);                      /* cleanup temp. info */
void    usage(char const *progname);
void    about(char const *program);         /* about icmake */

void    execute(char **argv);
void    preProcess(void);
void    compile(void);
void    icmDep(char **argv);

char   *accessFile(char const *file, char const *ext); /* returns copy of file
                                                or file.ext, or error if 
                                                neither exists */

                                /* if existing, use firstChoice, else use
                                   fallback, using the specified extension */
char   *useFile(char const *firstChoice,        
                char const *fallback, char const *ext);  

void    testCompile(void);

int     spawnlp(int mode, char const *prog, char const *av0, ...);
void    inspectFlags(char const *program, char **argv);

void    setTmpDir(void);
void    showFlags(char **argv);

// int  optChars(char const *program, char const *options, char **argv);
//void stdFiles(char **argv);                 /* also calls testCompile */
//void optDashSeparator(char **argv);
