/*

Corrected 30.5.1997

The first release was a five minute effort,
and it showed :(

This version should be a working one...

-EQU

*/


/*

I did this because I couldn't find the
utility mentioned in several README's
concerning QW skins and because I got
tired of QWCL downloading the same skins
over and over again. This is just a quick
hack to convert filenames to lowercase,
use it at your own risk!

I did this on Linux, should compile on
all BSD 4.X compilent UNIX's. I hope you find
it useful to some level. -EQU

To compile:

gcc -O2 -s -o fixskins fixskins.c

Usage example:

fixskins /usr/local/games/quake/qw/skins/*

*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

void convstring(char *, char *);

int main(int argc, char *argv[])
{
  int i;

  /* We are using stack. No reason for length checks */

  char newfname[1024];

  struct stat st;

  if(argc < 2) {
    printf("Usage: %s file1 file2 ..\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  for(i = 1; i < argc; i++) {
    /* If you get a 'No such file or directory' error message
       it probably means that you have a symbolic link
       which is missing it's destination. Great utility
       for finding out invalid soft links, hehe */

    if(stat(argv[i], &st) < 0) {
      perror(argv[i]);
      exit(EXIT_FAILURE);
    }

    if(S_ISREG(st.st_mode)) {
      convstring(argv[i], newfname);

      /* Just convert the filenames that actually
	 need to be converted */

      if(strcmp(argv[i], newfname) != 0) {
	/* Comment this line out if you want */

	printf("%s -> %s\n", argv[i], newfname);
	
	if(rename(argv[i], newfname) < 0) {
	  printf("Error while renaming %s\n", argv[i]);
	  perror("Reason");
	}
      }
    }
  }
}

void convstring(char *from, char *to)
{
  int i;
  int len;

  strcpy(to, from);

  len = strlen(from);

  /* If a path is given just change the filename, not the path, doh. */

  for(i = len - 1; i > 0 && from[i] != '/'; i--)
    ;;

  /* There is one possibility to go all wrong here, but I wouldn't
     worry about it if I were you... */

  if(i != 0 && from[i] == '/')
    i++;

  for(; i < len; i++) {
    if(isgraph(from[i]) && isupper(from[i]))
      to[i] = tolower(from[i]);
  }
}







