/*
 * templ.c
 *
 * Source code templates generator version 0.2
 *
 * Author: Oh Junseon <hollywar@mail.holywar.net>
 *
 * Date  : $Date: 2000/09/12 12:42:53 $ 
 *
 * $Revision: 1.3 $
 *
 */

/* SCSS/RCS Id */
#ifndef lint
static char rcsid[]="@(#)$Id: templ.c,v 1.3 2000/09/12 12:42:53 hollywar Exp $";
#endif /* lint */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>

#include "templ.h"

void show_help(void) ;
void show_info(struct templ_info *t_info) ;
struct templ_info *input_information (void) ;
struct templ_info *get_information (int argc , char **argv) ;
void error_exit (void);


int main (int argc , char *argv[]) 
{
    char *buffer ;
    char c ;
    char *ptr ;
    char buf[100] ;
    int file_size = 0;

    struct stat *sb ;
    struct templ_info *info ;
    struct templ_info *info_buf ;
    FILE *fp ;


    if (argc < 2) { 
       info = input_information() ;
    } else {
       info = get_information(argc,argv) ;
    }

    // get file size //
    sprintf(buf,"%s/t.%s",TEMPL_DIR,SEP[info->filetype]) ;
    if ( !(fp = fopen(buf,"rt"))) error_exit() ;
    // while ( (c = fgetc(fp)) != EOF ) file_size++ ;
    fseek(fp, 1 , SEEK_END) ;
    file_size = ftell(fp) ;

    buffer = (char *) calloc(sizeof(char),file_size+1); 
    rewind(fp) ;
    fread(buffer,1,sizeof(char)*file_size,fp);
    fclose(fp) ;

    if (fp = fopen(info->filename,"rt")) {
	printf(ERROR_COLOR"File already exits\n"NO_COLOR);
	fclose(fp) ;
        printf("Overwrite it.? [ y / n ] : ");
        c = fgetc(stdin);
        if (toupper(c) == 'N') {
	    printf(ERROR_COLOR"Cancled\r\n"NO_COLOR); exit(0) ;
        }
    }
    if ( !(fp = fopen(info->filename,"wt"))) error_exit() ;

    show_info(info) ;

    for (ptr = buffer ; *ptr != '\0' ; ptr ++ ) {
	if (!strncmp(ptr,S_FILE,strlen(S_FILE))) {
	    fputs(info->filename,fp);
	    ptr += strlen(S_FILE) - 1;
	    continue ;
	} else if (!strncmp(ptr,S_NAME,strlen(S_NAME))) {
	    fputs(info->author,fp);
	    ptr += strlen(S_NAME) - 1;
	    continue ;
	} else if (!strncmp(ptr,S_EMAIL,strlen(S_EMAIL))) {
	    fputs(info->email,fp);
	    ptr += strlen(S_EMAIL) - 1;
	    continue ;
	} else if (!strncmp(ptr,S_DES,strlen(S_DES))) {
	    fputs(info->description,fp);
	    ptr += strlen(S_DES) - 1;
	    continue ;
	} else if (*ptr == '\%') {
	    fputc('$',fp) ;
	    continue ;
	}
	
        fputc(*ptr,fp) ;
    }
 
    fclose(fp) ;

    printf(WORD_COLOR"\"%s\" file successfully created.\r\n"NO_COLOR,info->filename) ;

    return 1;

}

void show_help(void)
{
    int i ;

    printf("Usage : templ [Filetype] [\"Author\"] [\"Email\"] [\"Description\"] [filename]\r\n\r\n");
    printf("  "WORD_COLOR"%-10s"NO_COLOR"   %-24s  \r\n","File type","Description");
    for (i=0 ; SEP[i][0] != '\0' ; i++) {
	printf("[ "WORD_COLOR"%-10s"NO_COLOR" : %-26s ]\r\n",SEP[i],NAME[i]);
    }
    puts(""); 

    exit(0);

}
struct templ_info *get_information (int argc , char **argv)
{
    char buf[100] ;
    struct templ_info *t_info ;
    int i,check=0 ;

    t_info = (struct templ_info*)malloc(sizeof(struct templ_info)*1) ;
    if (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) {
        printf(TITLE_COLOR"Source code templates generator version %s\r\n"NO_COLOR,VERSION);
	show_help();
    }
    if (argc != 6) {
        printf(ERROR_COLOR"Illegal useage.\r\n\r\n"NO_COLOR); show_help();
    }

    check=0 ;
    for (i=0 ; SEP[i][0] != '\0' ; i++) {
        if ( !strcmp(argv[1],SEP[i])) check=i+1;
    }

    if (!check) {
	printf("templ: "ERROR_COLOR"%s is a wrong file type.\r\n\r\n"NO_COLOR,argv[1]) ;
	show_help();
    }

    t_info->filetype = check - 1 ;
    strcpy(t_info->author,argv[2]) ;
    strcpy(t_info->email,argv[3]) ;
    strcpy(t_info->description,argv[4]) ;
    strcpy(t_info->filename,argv[5]) ;

    return t_info ;
}


struct templ_info *input_information (void)
{
    char buf[200] ;
    int i ;
    int check=0 ;
    struct templ_info *t_info ;

    t_info = (struct templ_info*)malloc(sizeof(struct templ_info)*1) ;
   

    printf(TITLE_COLOR"Source code templates generator version %s\r\n"NO_COLOR,VERSION);
    printf("  "WORD_COLOR"%-10s"NO_COLOR"   %-24s  \r\n","File type","Description");
    for (i=0 ; SEP[i][0] != '\0' ; i++) {
	printf("[ "WORD_COLOR"%-10s"NO_COLOR" : %-24s ]\r\n",SEP[i],NAME[i]);
    }

   
    // get file type //
    check=0 ;
    while(1) {
        printf("Select file type : ");
	memset(buf,0,200) ;
        fgets(buf,200,stdin) ;
	if (buf[0] == 4 || buf[0] == 0) exit(0) ;
	buf[strlen(buf)-1] = '\0' ;
        for (i=0 ; SEP[i][0] != '\0' ; i++) {
            if ( !strcmp(buf,SEP[i])) check=i+1; 
        }
        if (check) break;
        puts(ERROR_COLOR"File type was wrong"NO_COLOR);
    }
    t_info->filetype = check-1;

    // get author //
    check=0;
    printf("Author : ");
    memset(buf,0,200) ;
    fgets(buf,200,stdin) ;
    if (buf[0] == 4 || buf[0] == 0) exit(0) ;
    buf[strlen(buf)-1] = '\0' ;
    strcpy(t_info->author,buf);

    // get email //
    check=0;
    printf("Email : ");
    memset(buf,0,200) ;
    fgets(buf,200,stdin) ;
    if (buf[0] == 4 || buf[0] == 0) exit(0) ;
    buf[strlen(buf)-1] = '\0' ;
    strcpy(t_info->email,buf);

    // get description//
    check=0;
    printf("Description: ");
    memset(buf,0,200) ;
    fgets(buf,200,stdin) ;
    if (buf[0] == 4 || buf[0] == 0) exit(0) ;
    buf[strlen(buf)-1] = '\0' ;
    strcpy(t_info->description,buf);

    // get filename//
    check=0;
    while(1) {
        printf("filename : ");
	memset(buf,0,200) ;
        fgets(buf,200,stdin) ;
	if (buf[0] == 4 || buf[0] == 0) exit(0) ;
	buf[strlen(buf)-1] = '\0' ;
        if ( strcmp(buf,"")) check=1; 
        if (check) break;
        puts(ERROR_COLOR"File name is wrong"NO_COLOR);
    }
    strcpy(t_info->filename,buf);

    return t_info ;
}

void show_info(struct templ_info *t_info) 
{
    printf(TITLE_COLOR"%37s\r\n"NO_COLOR,"File information");

    printf("[ "WORD_COLOR"%-12s"NO_COLOR" : %-40s ]\r\n","File type",SEP[t_info->filetype]);
    printf("[ "WORD_COLOR"%-12s"NO_COLOR" : %-40s ]\r\n","Author",t_info->author);
    printf("[ "WORD_COLOR"%-12s"NO_COLOR" : %-40s ]\r\n","Email",t_info->email);
    printf("[ "WORD_COLOR"%-12s"NO_COLOR" : %-40s ]\r\n","Description",t_info->description);

    return ;
}


void error_exit (void) 
{
    printf("%s\r\n",strerror(errno)) ;
    exit(0) ;
}

/*
 | $Id: templ.c,v 1.3 2000/09/12 12:42:53 hollywar Exp $
 |
 | Local Variables:
 | mode: c
 | mode: font-lock
 | version-control: t
 | delete-old-versions: t
 | End:
 |
 | -*- End-Of-File -*-
 */


syntax highlighted by Code2HTML, v. 0.9.1