#!/bin/sh
# 
# Tue Sep 26 15:25:35 CEST 2000
# Converts blanks to underscores on multiple files
# and other useful stuff
# 
# (c) 2000 Tilmann Bitterberg <tilmann@bitterberg.de>

VERBOSE=false
BLANKS=false
QUOTES=false
TITLES=false
UNDER=false
CAPITAL=false
ENDINGS=false
INTER=false

USAGE="\
   usage: `basename $0` [ -s ] [ -V ] [ -h ] [ -abceiqtu ]\
   "
USAGELONG="\
usage: `basename $0`\n
   -a, --all\t enable all features\n
   -b, --blanks\t converts ' ' to '_'\n
   -c, --capital\t capitalizes the letters at the beginning\n
   -e, --endings\t converts .M[Pp]3 to .mp3\n
   -i, --iactive\t ask before doing anything\n
   -h, --help\t display this text and exit succesfully\n
   -q, --quotes\t converts \` and ' into nothing\n
   -s, --show\t only test; show what whould be done\n
   -t, --titles\t removes unnessesary title numberings\n
   -u, --under\t insert underscores before capital letters\n
   -V, --verbose\t be a bit more verbose\n 
   \n
   Converts blanks to underscores and more on multiple files\n"


interactive () {
  antwort=n

  if test $INTER = true; then
    echo -n "Execute $@ ? "
    read antwort
    if test x"$antwort" = x"y"; then
      eval ${@}
    fi
  else
    eval ${@}
  fi
}


# Argument checking
if test $# -eq 0; then
  echo $USAGE 1>&2
  exit 1
fi

while test $# -gt 0 ; do
  case "${1}" in
  -h | --help )
    echo -e $USAGELONG 1>&2
    exit 0
  ;;
  -s | --show )
     VIEW=echo
     shift ;;
  -V | --verbose )
     VERBOSE=true
     shift ;;
  -b | --blanks )
     BLANKS=true
     shift ;;
  -q | --quotes )
     QUOTES=true
     shift ;;
  -t | --titles )
     TITLES=true
     shift ;;
  -u | --under )
     UNDER=true
     shift ;;
  -c | --capital )
     CAPITAL=true
     shift ;;
  -i | --interactive )
     INTER=true;
     shift ;;
  -e | --endings )
     ENDINGS=true
     shift ;;
  -a | --all )
     BLANKS=true
     QUOTES=true
     TITLES=true
     UNDER=true
     CAPITAL=true
     ENDINGS=true
     shift ;;
  *)
     echo $USAGE 1>&2
     exit 1
  ;;
  esac
done


if test $BLANKS = true; then
# Converts blanks to underscores
RESULT=$(find . -maxdepth 1 -name '* *')

if [ ! "$RESULT" ]; then
  echo -e "No files with blanks found found\n"$USAGE 1>&2
else
  for i in *\ *; do 
    if test $VERBOSE = true; then
      echo mv "$i" $(echo $i|sed 's/ /_/g')
    fi
    #interactive "$VIEW mv '$i' '$(echo $i|sed s/\ /_/g)'"
    $VIEW mv "$i" $(echo $i|sed 's/ /_/g')
  done
fi
fi # BLANKS = true

# Converts ' and ` in nothing

if test $QUOTES = true; then
for i in *[\'\`]*; do
  if test $VERBOSE = true; then
    echo mv "$i" $(echo $i|sed "s/['\`]//g")
  fi
  $VIEW mv "$i" $(echo $i|sed "s/['\`]//g")
done
fi

# Converts *_-_[01][0-9]_-_* to _-_

if test $TITLES = true; then
for i in *?-?[01][0-9]?-?*; do
  if test $VERBOSE = true; then
    echo mv "$i" $(echo $i|sed 's/.-.[01][0-9].-./_-_/')
  fi
  $VIEW mv "$i" $(echo $i|sed 's/.-.[01][0-9].-./_-_/')
done
fi

# Converts something like BlahBlah_-_FooBar.xxx to Blah_Blah_-_Foo_bar

if test $UNDER = true; then
for i in *[a-z][A-Z]*; do
  if test $VERBOSE = true; then
    echo mv "$i" $(echo $i|sed 's/\(.\)\([A-Z]\)/\1_\2/g;s/__/_/g')
  fi
  $VIEW mv "$i" $(echo $i|sed 's/\(.\)\([A-Z]\)/\1_\2/g;s/__/_/g')
done
fi

# This is pretty cool, it capitalizes the letters at the beginning
# of a line or after a _ or after a -
# It's quite fast although it looks complicated

if test $CAPITAL = true; then
for i in [a-z]*3; do
  if test $VERBOSE = true; then
    echo mv "$i" $(echo $i|sed '
    s/$/|||AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz/
    :loop
      s/^\([a-z]\)\(.*\)\(.\)\1/\3\2\3\1/
      s/\([-_]\)\([a-z]\)\(.*\)\(.\)\2/\1\4\3\4\2/
    t loop
    s/\(.*\)|||.*/\1/
    ')
  fi
  $VIEW mv "$i" $(echo $i|sed '
    s/$/|||AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz/
    :loop
      s/^\([a-z]\)\(.*\)\(.\)\1/\3\2\3\1/
      s/\([-_]\)\([a-z]\)\(.*\)\(.\)\2/\1\4\3\4\2/
    t loop
    s/\(.*\)|||.*/\1/
    ')
done
fi

# Converts 'false endings' to mp3

if test $ENDINGS = true; then
for i in *.M?3; do
  if test $VERBOSE = true; then
    echo mv "$i" $(echo $i|sed 's/\....$/.mp3/')
  fi
  $VIEW mv "$i" $(echo $i|sed 's/\....$/.mp3/g')
done
fi

exit 0
