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

#define INLEN 30
#define OUTLEN 128

char str_in[INLEN];
char str_out[OUTLEN];

int lengthlist[] = {30, 30, 30, 4, 30,1};

int main(argc,argv)
     int argc;
     char *argv[];
{
   unsigned char *p, *q, c;
   int i,j;
   char *fname;
   FILE *fp;

   fname = argv[1];

   p = str_out;
   for(i=0;i<OUTLEN;i++){
      *p++ = ' ';
   }
   (void) strcpy(str_out, "TAG");

   p = q = str_out + 3;
   for(i=0; i<5; i++){
      j = 0;
      while((c = getchar()) != '\n') {
	 if (c != '\r'){
	    if (j++< lengthlist[i]) {
	       *p++ = c;
	    }
	 }
      }
      p = q = q + lengthlist[i];
   }
   (void) fscanf(stdin,"%d",&i);
   *p = (unsigned char) i;

   if ((fp = fopen(fname,"r+")) == NULL) {
      (void) fprintf(stderr,"Can't open %s\n",fname);
      exit(1);
   }

   (void) fseek(fp, -OUTLEN, SEEK_END);
   (void) fgets(str_in, 4, fp);
   str_in[3] = '\0';
   if (!strncmp(str_in,"TAG",3)) {
#ifdef DEBUG
      (void) fprintf(stderr,"Found ID3 tag.\n");
#endif
      (void) fseek(fp, -OUTLEN, SEEK_END);
   } else {
#ifdef DEBUG
      (void) fprintf(stderr,"Not found ID3 tag[%s].\n",str_in);
#endif
      (void) fseek(fp, 0, SEEK_END);
   }

   p = str_out;
   for(i=0; i<OUTLEN; i++){
      (void) fputc(*p++, fp);
   }

   return 0;
}
