/* 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.3 (02.04.02)";

int main(int argc, char **argv)
{
FILE *fp;
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", argv[0]);
 fprintf(stderr, " © 2000-2002 Fabrizio 'Lanch' Bartoloni \n");
 fprintf(stderr, " lanch@tiscali.it \n");
 exit(1);
 }

fp = fopen(argv[1],"rb");
 if (!fp)
        {
        fprintf(stderr, "Unable to open file: %s\n", argv[1]);
        exit(1);
        }

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", argv[1]);
 exit(1);
                              } /* endif case of unknown sid */
 else {
 strcpy(newname, buf);
 strcat(newname, suffix);
 rename(argv[1], newname);
 exit(0);
      }
} /* endif check header */

 else {
      fprintf(stderr, "Not a real playsid file: %s \n", argv[1]);
      exit(2);
      }

fclose(fp);
return(0);
 }
