#include "io.h"
#include "edit.h"

long ti ;

generic_abort()
{
    fprintf(stderr,"fatal error has occured\n") ;
    reset_tty() ;
    exit(-1) ;
}

char *progname ;

Usage()
{
    fprintf(stderr,"\nUse: %s [-p] [-m] [-t title] filename\n",progname) ;
    fprintf(stderr,"-p:   Attach Posting header\n") ;
    fprintf(stderr,"-m:   Attach Mailer header (mutually exclusive of -p)\n") ;
    fprintf(stderr,"-t:   Enter title for header\n") ;
    fprintf(stderr,"also, environments USER, and USERNAME are used\n") ;
    exit(-1) ;
}

int posting = NA ;
int mailing = NA ;
char *title = NULL;
char *userid = NULL;
char *username = NULL;

main(ac,av)
int ac ;
char *av[] ;
{
    char *term, *getenv() ;
    extern int dumb_term ;    /* this comes in from term_init */
    int c ;
    extern char *optarg ;
    extern int optind ;
    char *filename ;

    progname = av[0] ;
    while((c = getopt(ac,av,"pmt:")) != -1) {
        switch (c) {
          case 'p':
            if(mailing)
              Usage() ;
            posting = YEA ;
            break ;
          case 'm':
            if(posting)
              Usage() ;
            mailing = YEA ;
            break ;
          case 't':
            title = optarg ;
            break ;
          case '?':
          default:
            Usage() ;
        }
    }
    if(optind == ac - 1)
      filename = av[optind] ;
    else
      Usage() ;

    userid = getenv("USER") ;
    username = getenv("USERNAME") ;
    if((term = getenv("TERM")) == NULL) {
        fprintf(stderr,"No Terminal environment type!\n") ;
        exit(-1) ;
    }
    get_tty() ;               /* get tty port mode settings */
    init_tty() ;              /* set up mode for NOECHO and RAW */
    term_init(term) ; /* Load up strings used to control terminal type 'term'*/
    if(dumb_term) {
        fprintf(stderr,"Terminal type not smart enough to support editor\n") ;
        reset_tty() ;
        exit(-1) ;
    }
    initscr() ;         /* Initialize screen interface */
    vedit_init() ;      /* Initialize editor */
    vedit(filename) ;   /* Start editor on file, do not save header */
    clear() ;           /* clear screen */
    redoscr() ;         /* make clear() happen */
    reset_tty() ;    
    exit(0) ;
}




