/* PSID renamer to fix SID names*/
/* Fabrizio "Lanch" Bartoloni   */
/*   lanch@tiscalinet.it        */
/*

 Developped on Amiga!

*/

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

static char verstr[] = "$VER: PSIDren 1.2 (23.01.02)";

int main(int argc, char **argv)
{
char *progname = argv[0];
FILE *fp;
char *sidsource = argv[1];
char buf[25]; /* here we find the real name */
char newname[25];
char *suffix = ".dat";
char header[5];

if (argc != 2)
 {
 fprintf(stderr, " Usage: %s psidsong.dat \n", progname);
 fprintf(stderr, " © 2000-2002 Fabrizio 'Lanch' Bartoloni \n");
 fprintf(stderr, " lanch@tiscalinet.it \n");
 exit(1);
 }

if (fp = fopen(sidsource,"rb")) {

fgets(header, sizeof(header), fp);
if (strcmp(header, "PSID") == 0)
{
/* We go to 0x17, here is located the real filename */
long offset;

offset = 0x16L;
 fseek(fp,offset,0);
 fgets(buf, sizeof(buf), fp);
 if (strcmp(buf, "???") == 0) {
 fprintf(stderr, "Real name unknown for: %s\n", sidsource);
 exit(1);
                              } /* endif case of unknown sid */
 else {
 strcpy(newname, buf);
 strcat(newname, suffix);
 rename(sidsource, newname);
 exit(0);
      }
} /* endif check header */

 else {
      fprintf(stderr, "Not a real playsid file: %s \n", sidsource);
      exit(2);
      }
                                } /* endif fopen read */
              else
                  {
                  fprintf(stderr, "Unable to open file: %s\n", sidsource);
                  exit(1);
                  }
fclose(fp);
return(0);
 }
