
#include <time.h>

#include "bbs.h"
main(int argc,char **argv)
{
  char buf[200];
  boardheader allbrd[MAXBOARD];
  struct stat st; /**** 統計文章數用 ****/
  fileheader fh;
  int bdnum; /**** 討論區文章數量 ***/
  FILE *fptr3;
  int inf,fd,ii,count;
  int i,j,now1,filenum;
  long filesize,postcnt,encnt;
  long numct,numbe,numee,numinn; /*** numct: 12:00 numbe: before expire
                               numee: after expire  numinn: 站內post ****/
  int chkflag;
  /***** 看看參數決定功能 0:查大小 1:記錄outpost 2:記錄expire前
                          3:記錄expire後 4:reset******/
    if (argc==1) chkflag=0;
    else chkflag=argv[1][0]-48;
   /** printf("chkflag=%d\n",chkflag);***/
  /**** 讀進所有的板名 *****/
  inf = open("/home/bbs/.BOARDS", O_RDONLY);
  i=0;
  memset(allbrd, 0, MAXBOARD * sizeof(boardheader));
  while (read(inf, &allbrd[i], sizeof(boardheader)) == sizeof(boardheader))
  {
    if (allbrd[i].brdname[0])
    {
      i++;
    }
  }
  close(inf);
  count=i;
  //printf("\ncont=%d\n",count);
  for (i=0;i<count;i++)
  {
    strcpy(buf,"/home/bbs/boards/");
    strcat(buf,allbrd[i].brdname);
    strcat(buf,"/.DIR");
      /**** 計算版內文章數 ****/
      if ((fd = open(buf, O_RDONLY)) < 0)
         bdnum=0;
      else
      {
         fstat(fd, &st);
         bdnum = st.st_size / sizeof(fh);
         if (bdnum<0) bdnum=0;
      }
      close(fd);

        switch (chkflag)
        {
         case 0: printf( "%s= %ld, %d\n",allbrd[i].brdname,bdnum*sizeof(fh),bdnum);
                 break;
         case 1:
                 /**** 開檔讀取資料 12:00做的事****/
                 strcpy(buf,"/home/bbs/boards/");
                 strcat(buf,allbrd[i].brdname);
                 strcat(buf,"/post_tot");
                 fptr3 = fopen(buf,"r");
                 fscanf(fptr3,"%ld %ld %ld",&numct,&numbe,&numee);
                 /***** 算post總數目 ***/
                 if ((numbe>=numct)&&(bdnum>=numee))
                     postcnt=(numbe-numct)+(bdnum-numee);
                 else
                     postcnt=0;
                 numct=bdnum;
                 fclose(fptr3);
                 fptr3 = fopen(buf,"w");
                 fprintf(fptr3,"%ld %ld %ld\n",numct,numbe,numee);
                 fclose(fptr3);
                 /****讀站內post數目****/
                  strcpy(buf,"/home/bbs/boards/");
                  strcat(buf,allbrd[i].brdname);
                  strcat(buf,"/postnum");
                  fd = open(buf, O_RDONLY);
                    if (ii=read(fd, (char*)(&encnt), 4)>0)
                    {
                       if ((encnt<0)||(encnt>100000))
                           encnt=0;
                    }
                    else encnt=0;
                    numinn=encnt;
                    close(fd);
                 /***** 計算外界post數量 ****/
                 strcpy(buf,"/home/bbs/boards/");
                 strcat(buf,allbrd[i].brdname);
                 strcat(buf,"/outpostnum");
                 fptr3 = fopen(buf,"w");
                 if (numinn>postcnt) postcnt=0;
                 else postcnt=postcnt-numinn;
                 fprintf(fptr3,"%ld\n",postcnt);
                 fclose(fptr3);
                 break;
         case 2:
                 /**** 開檔讀取資料 ****/
                 strcpy(buf,"/home/bbs/boards/");
                 strcat(buf,allbrd[i].brdname);
                 strcat(buf,"/post_tot");
                 fptr3 = fopen(buf,"r");
                 fscanf(fptr3,"%ld %ld %ld",&numct,&numbe,&numee);
                 numbe=bdnum;
                 //rewind(fptr3);
                 fclose(fptr3);
                 fptr3 = fopen(buf,"w");
                 fprintf(fptr3,"%ld %ld %ld\n",numct,numbe,numee);
                 fclose(fptr3);
                 break;
         case 3:
                 /**** 開檔讀取資料 ****/
                 strcpy(buf,"/home/bbs/boards/");
                 strcat(buf,allbrd[i].brdname);
                 strcat(buf,"/post_tot");
                 fptr3 = fopen(buf,"r");
                 fscanf(fptr3,"%ld %ld %ld",&numct,&numbe,&numee);
                 numee=bdnum;
                 fclose(fptr3);
                 fptr3 = fopen(buf,"w");
                 fprintf(fptr3,"%ld %ld %ld\n",numct,numbe,numee);
                 fclose(fptr3);
                 break;
         case 4:
                 /**** 殺掉所有的post_tot ****/
                 strcpy(buf,"/home/bbs/boards/");
                 strcat(buf,allbrd[i].brdname);
                 strcat(buf,"/post_tot");
                 remove(buf);
                 break;
         default: puts("Error!"); break;
        }
  }
}
