/*
    Splitting a file into pieces
    Copyright (C) 2002  Wiesner Thomas^M
^M
    This program is free software; you can redistribute it and/or modify^M
    it under the terms of the GNU General Public License as published by^M
    the Free Software Foundation; either version 2 of the License, or^M
    (at your option) any later version.^M
^M
    This program is distributed in the hope that it will be useful,^M
    but WITHOUT ANY WARRANTY; without even the implied warranty of^M
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the^M
    GNU General Public License for more details.^M
^M
    You should have received a copy of the GNU General Public License^M
    along with this program; if not, write to the Free Software^M
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
    FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN^M
    OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES^M
    PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED^M
    OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF^M
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS^M
    TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE^M
    PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,^M
    REPAIR OR CORRECTION.

*/

#include <stdio.h>
#include <string.h>

#define KBYTE  1024
#define TRUE   1
#define FALSE  0

void strip_ext(char *fne, char *fn);

int main(int argc, char *argv[])
{
  unsigned long count, size;
  int fnum, fixed, c = 0;
  char name[80];
  char buf[84];
  char num[4];
  FILE *ifi, *ofi;

  if(argc != 2) {
    printf("Usage: split file\n");
    return(-1);
  }

  if((ifi = fopen(argv[1], "rb")) == NULL) {
    printf("Unable to open file %s\n", argv[1]);
    return(-1);
  }

  printf("Fixed size [f]\nVariable size [v]\n");
  scanf("%c", &c);
  if(c == 'F' || c == 'f') {
    printf("File size (kB): ");
    fixed = TRUE;
  } else {
    printf("Size of file 1 (kB): ");
    fixed = FALSE;
  }
  scanf("%lu", &size);
  size *= KBYTE;
  strip_ext(argv[1], name);

  fnum = 0; count = 0;

  while((c = fgetc(ifi)) != EOF) {
    if(count >= size || fnum == 0) {      // If new file or first file
      count = 0;
      if(fnum)       // Don't close file if it is the first file
        fclose(ofi);
      strcpy(buf, name);
      sprintf(num, "%u", fnum);
      strcat(buf, ".");
      strcat(buf, num);
      if((ofi = fopen(buf, "wb")) == NULL) {
        fclose(ifi);
        printf("Unable to open output file %s\n", buf);
        return(-1);
      }
      if(!fixed) {
        printf("Size of file %d (kB): ", fnum+2);
        scanf("%lu", &size);
        size *= KBYTE;
      }
      fnum++;
    }
    if(fnum == 1 && count == 0) {
      fputs(argv[1], ofi);
      fputc('\n', ofi);
    }
    if(c != EOF)
      fputc(c, ofi);
    count++;
  }
  fclose(ifi);
  fclose(ofi);
  printf("File splitted into %u pieces.\n", fnum);
  return(0);
}


void strip_ext(char *fne, char *fn)
{
  int len, cnt;

  len = strlen(fne);

  for(cnt = 0; cnt < len && fne[cnt] != '.'; cnt++) {
    fn[cnt] = fne[cnt];
  }
  fn[cnt] = '\0';
}