/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            server.cc
 *
 *  Mon Nov  8 11:35:01 CET 2004
 *  Copyright  2004 Bent Bisballe
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *    This file is part of MIaV.
 *
 *    MIaV 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.
 *
 *    MIaV 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 MIaV; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */

/*
 * $Id$
 */

/*
 * $Log$
 * Revision 1.13  2005/05/09 16:40:20  deva
 *
 * Added optimize yuv conversion code
 *
 * Revision 1.12  2005/05/07 10:56:18  deva
 * Changed print outs
 *
 * Revision 1.11  2005/05/03 08:31:59  deva
 * Removed the error object, and replaced it with a more generic info object.
 *
 * Revision 1.10  2005/05/02 19:47:43  deva
 * Fixed overlapping cpr numbers on the server (now it saves one cpr pr.
 * connection, and ignores any changes sent)
 *
 * Revision 1.9  2005/05/02 18:46:15  deva
 * Files are now saved in a custom folder (defined in miav.conf)
 *
 * Revision 1.8  2005/05/01 09:56:26  deva
 * Added Id and Log tags to all files
 */

#include "server.h"
#include "miav.h"

#include <stdio.h>
#include <stdlib.h>

// For mkdir
#include <sys/stat.h>
#include <sys/types.h>

// For unlink
#include <unistd.h>

#include "miav_config.h"

#include "mov_encoder.h"
#include "img_encoder.h"

#include "server_status.h"

#include "dv.h"

#include "info_console.h"

void saveFrameAsImage(char* cpr, Frame *f)
{
  ImgEncoder imgenc;
  struct tm *ltime;
  time_t t = time(NULL);
  FILE *fp;
  int cnt = 0;
  char fname[256];
  char birthmonth[3];
  char date[9];
  
  string *root = config->readString("server_root");

  // Test for server root writeability
  sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand());
  fp = fopen(fname, "w");
  if(!fp) {
    int r = rand();
    fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str());
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }
  fclose(fp);
  unlink(fname);

  // Check for cpr length correctness
  if(strlen(cpr) != 11) {
    int r = rand();
    fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr));
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Copy the bytes representing the birth month from the cpr
  // [dd][mm][yy]-[nn][nn]
  strncpy(birthmonth, &cpr[2], 2);
  birthmonth[2] = 0;

  // Create folder named birthmonth in server root
  sprintf(fname, "%s/%s", root->c_str(), birthmonth);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Create folder named cpr in serverroot/birthmonth
  sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.jpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
    return;
  }

  // Create date (today) in [yyyy][mm][dd]
  ltime = localtime(&t);
  sprintf(date, "%.4d%.2d%.2d", 
          ltime->tm_year + 1900, 
          ltime->tm_mon, 
          ltime->tm_mday);

  // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg
  sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);

  // test filename-[cnt] for existamce cnt++ until not existing.
  fp = fopen(fname, "r");
  while(fp) {
    fclose(fp);
    cnt++;
    sprintf(fname, "%s/%s/%s/%s-%s-%.3d.jpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
    fp = fopen(fname, "r");
  }

  fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr);
  imgenc.encode(f, fname, 100); // Quality is between 0...100, where 100 is best.
}
/*
struct tm
{
  int tm_sec;                   // Seconds.     [0-60] (1 leap second) 
  int tm_min;                   // Minutes.     [0-59] 
  int tm_hour;                  // Hours.       [0-23] 
  int tm_mday;                  // Day.         [1-31] 
  int tm_mon;                   // Month.       [0-11] 
  int tm_year;                  // Year - 1900.  
  int tm_wday;                  // Day of week. [0-6] 
  int tm_yday;                  // Days in year.[0-365] 
  int tm_isdst;                 // DST.         [-1/0/1]
};
*/

MovEncoder *newMovEncoder(char* cpr)
{
  MovEncoder *enc;
  struct tm *ltime;
  time_t t = time(NULL);
  FILE *fp;
  int cnt = 0;
  char fname[256];
  char birthmonth[3];
  char date[9];
  
  string *root = config->readString("server_root");

  // Test for server root writeability
  sprintf(fname, "%s/miavtemp.tmp%d", (char*)root->c_str(), rand());
  fp = fopen(fname, "w");
  if(!fp) {
    int r = rand();
    fprintf(stderr, "MIaV does not have write access to the server root [%s]\n", root->c_str());
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }
  fclose(fp);
  unlink(fname);

  // Check for cpr length correctness
  if(strlen(cpr) != 11) {
    int r = rand();
    fprintf(stderr, "Illigal CPR, it must have length 11, it had lentgh %d\n", strlen(cpr));
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Copy the bytes representing the birth month from the cpr
  // [dd][mm][yy]-[nn][nn]
  strncpy(birthmonth, &cpr[2], 2);
  birthmonth[2] = 0;

  // Create folder named birthmonth in server root
  sprintf(fname, "%s/%s", root->c_str(), birthmonth);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Create folder named cpr in serverroot/birthmonth
  sprintf(fname, "%s/%s/%s", root->c_str(), birthmonth, cpr);
  if(!mkdir(fname, S_IRWXU) == -1 && errno != EEXIST) {
    int r = rand();
    fprintf(stderr, "Not possible to create subfolder %s\n", fname);
    fprintf(stderr, "Redirecting output to [/tmp/miav-%d.mpg]\n", r);
    sprintf(fname, "/tmp/miav-%d.mpg", r);
    enc = new MovEncoder(fname);
    return enc;
  }

  // Create date (today) in [yyyy][mm][dd]
  ltime = localtime(&t);
  sprintf(date, "%.4d%.2d%.2d", 
          ltime->tm_year + 1900, 
          ltime->tm_mon, 
          ltime->tm_mday);

  // Create filename: [serverroot]/[birthmonth]/[cpr]/[cpr]-[date]-[cnt].mpg
  sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);

  // test filename-[cnt] for existamce cnt++ until not existing.
  fp = fopen(fname, "r");
  while(fp) {
    fclose(fp);
    cnt++;
    sprintf(fname, "%s/%s/%s/%s-%s-%.3d.mpg", root->c_str(), birthmonth, cpr, cpr, date, cnt);
    fp = fopen(fname, "r");
  }

  fprintf(stderr, "Success - using filename: [%s]\n", fname); fflush(stderr);
  enc = new MovEncoder(fname);
  return enc;
}

void newConnection(Socket *socket)
{
  char cpr[256];
  bool hasCpr = false;
  ServerStatus status;
  InfoConsole info;

  n_savestate savestate = LATER;
  n_header h;
  Frame *frame;
  Frame *freeze_frame = NULL;
  MovEncoder *enc = NULL;
  //  unsigned char dvbuf[DVPACKAGE_SIZE];

  frame = new Frame(NULL, DVPACKAGE_SIZE);

  printf("New connection[pid: %d]...\n", getpid());

  Network network = Network(socket, &info);
  while(int ret = network.recvPackage(&h, frame->data, frame->size)) {
    status.checkPoint();
    if(ret == -1) {
      fprintf(stderr, "An error occurred...!\n");
      break;
    }

    if(!hasCpr) {
      sprintf(cpr, h.header.h_data.cpr);
      hasCpr = true;
    }
    
    //    printf("Read: %d bytes ", ret);
    //    printf("typ: %d ", h.header_type);
//    fprintf(stdout, "cpr: %s ", cpr);
//    fprintf(stdout, "frz: %d ", h.header.h_data.freeze);
//    fprintf(stdout, "sht: %d ", h.header.h_data.snapshot);
//    fprintf(stdout, "save: %d ", h.header.h_data.savestate);
//    fflush(stdout);
    
    if(h.header.h_data.snapshot) {
      if(freeze_frame) {
        saveFrameAsImage(cpr, freeze_frame);
        delete freeze_frame;
        freeze_frame = NULL;
      } else {
        saveFrameAsImage(cpr, frame);
      }
    }

    if(h.header.h_data.record) {
      if(!enc) enc = newMovEncoder(cpr);
      enc->encode(frame);
    }

    if(h.header.h_data.savestate) {
      savestate = h.header.h_data.savestate;
    }

    if(h.header.h_data.freeze) {
      if(freeze_frame) delete freeze_frame;
      freeze_frame = frame;
    } else {
      delete frame;
    }

    frame = new Frame(NULL, DVPACKAGE_SIZE);
  }

  // TODO: Use save state

  if(enc) delete enc;

  printf("Connection end[pid: %d]...\n", getpid());
  
}