/*--------------------------------*-C-*---------------------------------*
 * File:        twbbs2data.c
 *----------------------------------------------------------------------*
 *
 * All portions of code are copyright by their respective author/s.
 * Copyright (C) 1999 crazykid <kids.bbs@bbs.yzu.edu.tw>
 *
 * Âà´« twbbs ¦Cªí v0.1
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *---------------------------------------------------------------------*/


#include "bbsnet.h"   


struct twbbs
{
 char name[40];
 char domain[32];
 char ip[16];
}twbbs;

int main(int argc,char *argv[])
{

  FILE *fd_in,*fd_out;
  char domain[32];
  char flag=0;
  char TMP_FILE[]="twbbs.tmp";
  int c;
  if(argc<3) {
   printf("\nusage: %s input_file output_file\n",argv[0]);
   printf("         input_file : twbbs list format\n");
   printf("         output_file: bbsnet list format\n\n");
   exit(1);
  }   

  fd_in=fopen(argv[1],"r");
  fd_out=fopen(TMP_FILE,"w");
  while( (c=fgetc(fd_in)) != EOF) {
   if(c==';') flag=1;
   if(c=='\n') flag=0;
   if(flag && c==' ') continue;
   fputc(c,fd_out);
  }  
  fclose(fd_in);
  fclose(fd_out);
  fd_in=fopen(TMP_FILE,"r");

  fd_out=fopen(argv[2],"a+");
  
  while(fscanf(fd_in,"%s %s %s\n",twbbs.domain,twbbs.ip,twbbs.name)!=EOF)
  {
   if(!twbbs.domain[0] || !twbbs.ip[0] || !twbbs.name[0]) {
    fprintf(fd_out,"Warnning!\n");
    continue;
   }
   bzero(domain,sizeof(domain));
   sprintf(domain,"%s.twbbs.org",twbbs.domain);
   if(strlen(twbbs.name)>20) 
    fprintf(fd_out,"#");    
   fprintf(fd_out,"%-19s  %-32s %-16s\n",twbbs.name+1,domain,twbbs.ip);
   bzero(twbbs.name,sizeof(twbbs));
   
  }

  fclose(fd_in);
  fclose(fd_out);
  return 0;
}
