/*
    Putting a splitted file together
    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 <stdlib.h>
#include <string.h>

#define TRUE   1
#define FALSE  0

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

int main(int argc, char *argv[])
{
  int files, val;
  char name[84];
  char base[80];
  char buf[84];
  char num[4];
  int c, swap;
  FILE *ifi, *ofi;

  if(argc != 4) {
    printf("Usage: glue first_file number_of_parts swap[s]/noswap[n]\n");
    return(-1);
  }
  if(strcmp(argv[3], "s") == 0)
    swap = TRUE;
  else
    swap = FALSE;
  if((ifi = fopen(argv[1], "rb")) == NULL) {
    printf("Unable to open input file %s\n", argv[1]);
    return(-1);
  }
  fgets(name, 80, ifi);
  name[strlen(name)-1] = '\0';
  if((ofi = fopen(name, "wb")) == NULL) {
    printf("Unable to open output file %s\n", name);
    fclose(ifi);
    return(-2);
  }

  strip_ext(argv[1], base);
  files = atoi(argv[2]);
  val = 0;
  do {
    printf("Adding file number %d\n", val+1);
    if(swap)
      putchar('\n');
    while((c = fgetc(ifi)) != EOF) {
      if(c != EOF)
        fputc(c, ofi);
    }
    fclose(ifi);

    if(val+1 < files) {
      if(swap) {
        printf("Swap disk if necessary and press enter ...");
        getchar();
      }
      sprintf(num, "%u", val+1);
      strcpy(buf, base);
      strcat(buf, ".");
      strcat(buf, num);
      if((ifi = fopen(buf, "rb")) == NULL) {
        printf("Unable to open input file %s\n", buf);
        fclose(ifi);
        return(-1);
      }
    }
    
    val++;
  } while(val < files);

  fclose(ifi);
  fclose(ofi);
  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';
}