#!/bin/bash

#Funktioniert: Delete, Swap, Replace, Upcase, Spaces, Umlaute

#Stabil: Umlaute, Upcase, Spaces, Extended, Swap, Replace, Delete, 

NUMBER_OF_STARS=""
TEXT_BEFORE_STAR=""
TEXT_AFTER_STAR=""
verbose="0"

###################
# my regex parser #
###################

#takes one argument (usually third)
function regex_stars {
  temp=""
  POS=`expr index "$1" "*"`
  if [ "$POS" = "0" ]; then
    NUMBER_OF_STARS="0"
  else
    let "POS=$POS+1"
    temp=`expr substr "$1" "$POS" "1000"`
    POS=`expr index "$temp" "*"`
    if [ "$POS" = "0" ]; then
      NUMBER_OF_STARS="1"
    else
      NUMBER_OF_STARS="2"
    fi
  fi
}

#takes two arguments
#1:0 for text before first star,
#1:1 for text after first star,
#1:2 for text after second star
#2: the string
function regex_string {
  if [ "$1" = "0" ]; then
    POS=`expr index "$2" "*"`
    let "POS=$POS-1"
    TEXT_BEFORE_STAR=`expr substr "$2" "1" "$POS"`
  elif [ "$1" = "1" ]; then
    POS=`expr index "$2" "*"`
    let "POS=$POS+1"
    temp=`expr substr "$2" "$POS" "1000"`
    LENGTH=`expr index "$temp" "*"`
    if [ "$LENGTH" = "0" ]; then
      TEXT_AFTER_STAR="$temp"
    else
      let "LENGTH=$LENGTH-1"
      temp2=`expr substr "$temp" "1" "$LENGTH"`
      TEXT_AFTER_STAR="$temp2"
    fi
  fi
}

##################################################
# replace <this> <that> <recursive/dirs> <regex> #
##################################################

function replace {
  if [ "$verbose" = "1" ]; then
    echo "Replacing $1 by $2,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  if [ "$3" = "1" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*$1*$SUFFIX" "#1$PREFIX#2$2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX*$1*$SUFFIX to #1$PREFIX#2$2#3$SUFFIX"
    fi
  elif [ "$3" = "2" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*$1*$SUFFIX" "#1$PREFIX#2$2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*$1*$SUFFIX" "$PREFIX#2$2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX*$1*$SUFFIX to #1$PREFIX#2$2#3$SUFFIX"
      echo "  -r ;$PREFIX*$1*$SUFFIX to $PREFIX#2$2#3$SUFFIX"
    fi
  else
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*$1*$SUFFIX" "$PREFIX#1$2#2$SUFFIX" 2>&1\`; echo $1)`
    done   
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX*$1*$SUFFIX to $PREFIX#1$2#2$SUFFIX"
    fi
  fi
}

function upcase {
  if [ "$verbose" = "1" ]; then
    echo "Upcasing,"
    if [ "$1" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$1" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $2"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$2"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$2"
    regex_string "1" "$2"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  replace " ä" " Ä" "$1" "$2"
  replace " ö" " Ö" "$1" "$2"
  replace " ü" " Ü" "$1" "$2"

  
  if [ "$1" = "1" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX* [a-z]*$SUFFIX" "#1$PREFIX#2 #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=`mmv ";[a-z]*$SUFFIX" "#1#u2#3$SUFFIX" 2>&1`
    OUT=`mmv ";ä*$SUFFIX" "#1Ä#2$SUFFIX" 2>&1`
    OUT=`mmv ";ö*$SUFFIX" "#1Ö#2$SUFFIX" 2>&1`
    OUT=`mmv ";ü*$SUFFIX" "#1Ü#2$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX* [a-z]*$SUFFIX to #1$PREFIX#2 #u3#4$SUFFIX"
      echo "  ;[a-z]*$SUFFIX to #1#u2#3$SUFFIX"
      echo "  ;ä*$SUFFIX to #1Ä#2$SUFFIX"
      echo "  ;ö*$SUFFIX to #1Ö#2$SUFFIX"
      echo "  ;ü*$SUFFIX to #1Ü#2$SUFFIX"
    fi
  elif [ "$1" = "2" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX* [a-z]*$SUFFIX" "#1$PREFIX#2 #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX* [a-z]*$SUFFIX" "$PREFIX#2 #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=`mmv ";[a-z]*$SUFFIX" "#1#u2#3$SUFFIX" 2>&1`
    OUT=`mmv -r ";[a-z]*$SUFFIX" "#u2#3$SUFFIX" 2>&1`
    OUT=`mmv ";Ã¤*$SUFFIX" "#1Ã„#2$SUFFIX" 2>&1`
    OUT=`mmv -r ";Ã¤*$SUFFIX" "Ã„#2$SUFFIX" 2>&1`
    OUT=`mmv ";Ã¶*$SUFFIX" "#1Ã–#2$SUFFIX" 2>&1`
    OUT=`mmv -r ";Ã¶*$SUFFIX" "Ã–#2$SUFFIX" 2>&1`
    OUT=`mmv ";Ã¼*$SUFFIX" "#1Ãœ#2$SUFFIX" 2>&1`
    OUT=`mmv -r ";Ã¼*$SUFFIX" "Ãœ#2$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX* [a-z]*$SUFFIX to #1$PREFIX#2 #u3#4$SUFFIX"
      echo "  -r ;$PREFIX* [a-z]*$SUFFIX to $PREFIX#2 #u3#4$SUFFIX"
      echo "  ;[a-z]*$SUFFIX to #1#u2#3$SUFFIX"
      echo "  -r ;[a-z]*$SUFFIX to #u2#3$SUFFIX"
      echo "  ;Ã¤*$SUFFIX to #1Ã„#2$SUFFIX"
      echo "  -r ;Ã¤*$SUFFIX to Ã„#2$SUFFIX"
      echo "  ;Ã¶*$SUFFIX to #1Ã–#2$SUFFIX"
      echo "  -r ;Ã¶*$SUFFIX to Ã–#2$SUFFIX"
      echo "  ;Ã¼*$SUFFIX to #1Ãœ#2$SUFFIX"
      echo "  -r ;Ã¼*$SUFFIX to Ãœ#2$SUFFIX"
    fi
  else
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX* [a-z]*$SUFFIX" "$PREFIX#1 #u2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=`mmv "[a-z]*$SUFFIX" "#u1#2$SUFFIX" 2>&1`
    OUT=`mmv "Ã¤*$SUFFIX" "Ã„#1$SUFFIX" 2>&1`
    OUT=`mmv "Ã¶*$SUFFIX" "Ã–#1$SUFFIX" 2>&1`
    OUT=`mmv "Ã¼*$SUFFIX" "Ãœ#1$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX* [a-z]*$SUFFIX to $PREFIX#1 #u2#3$SUFFIX"
      echo "  [a-z]*$SUFFIX to #u1#2$SUFFIX"
      echo "  Ã¤*$SUFFIX to Ã„#1$SUFFIX"
      echo "  Ã¶*$SUFFIX to Ã–#1$SUFFIX"
      echo "  Ã¼*$SUFFIX to Ãœ#1$SUFFIX"
    fi
  fi
}

function extended {
  if [ "$verbose" = "1" ]; then
    echo "Applying extended changes,"
    if [ "$1" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$1" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $2"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$2"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$2"
    regex_string "1" "$2"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  replace "%20" " " "$1" "$2"
  replace "_" " " "$1" "$2"
  replace "mc" "Mc" "$1" "$2"
  replace "MC" "Mc" "$1" "$2"
  replace "dj" "DJ" "$1" "$2"
  replace "Dj" "DJ" "$1" "$2"

  if [ "$1" = "1" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*\?*$SUFFIX" "#1$PREFIX#2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*'[A-Z]*" "#1$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*DJ [a-z]*" "#1$PREFIX#2DJ #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*DJ[a-z]*" "#1$PREFIX#2DJ#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*Mc [a-z]*" "#1$PREFIX#2Mc #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*Mc[a-z]*" "#1$PREFIX#2Mc#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*'[A-Z]*" "#1$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX*\?*$SUFFIX to #1$PREFIX#2#3$SUFFIX"
      echo "  ;$PREFIX*'[A-Z]* to #1$PREFIX#2'#l3#4$SUFFIX"
      echo "  ;$PREFIX*DJ [a-z]* to #1$PREFIX#2DJ #u3#4$SUFFIX"
      echo "  ;$PREFIX*DJ[a-z]* to #1$PREFIX#2DJ#u3#4$SUFFIX"
      echo "  ;$PREFIX*Mc [a-z]* to #1$PREFIX#2Mc #u3#4$SUFFIX"
      echo "  ;$PREFIX*Mc[a-z]* to #1$PREFIX#2Mc#u3#4$SUFFIX"
      echo "  ;$PREFIX*'[A-Z]* to #1$PREFIX#2'#l3#4$SUFFIX"
    fi
  elif [ "$1" = "2" ]; then
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*\?*$SUFFIX" "#1$PREFIX#2#3$SUFFIX" 2>&1\`; echo $1)`
    done
        OUT=""
    while [ "$OUT" != "Nothing" ]; do
    OUT=`(set \`mmv -r ";$PREFIX*\?*$SUFFIX" "$PREFIX#2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*'[A-Z]*" "#1$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*'[A-Z]*" "$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*DJ [a-z]*" "#1$PREFIX#2DJ #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*DJ [a-z]*" "$PREFIX#2DJ #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*DJ[a-z]*" "#1$PREFIX#2DJ#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*DJ[a-z]*" "$PREFIX#2DJ#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*Mc [a-z]*" "#1$PREFIX#2Mc #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*Mc [a-z]*" "$PREFIX#2Mc #u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*Mc[a-z]*" "#1$PREFIX#2Mc#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*Mc[a-z]*" "$PREFIX#2Mc#u3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv ";$PREFIX*'[A-Z]*" "#1$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv -r ";$PREFIX*'[A-Z]*" "$PREFIX#2'#l3#4$SUFFIX" 2>&1\`; echo $1)`
    done
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX*\?*$SUFFIX to #1$PREFIX#2#3$SUFFIX"
      echo "  -r ;$PREFIX*\?*$SUFFIX to $PREFIX#2#3$SUFFIX"
      echo "  ;$PREFIX*'[A-Z]* to #1$PREFIX#2'#l3#4$SUFFIX"
      echo "  -r ;$PREFIX*'[A-Z]* to $PREFIX#2'#l3#4$SUFFIX"
      echo "  ;$PREFIX*DJ [a-z]* to #1$PREFIX#2DJ #u3#4$SUFFIX"
      echo "  -r ;$PREFIX*DJ [a-z]* to $PREFIX#2DJ #u3#4$SUFFIX"
      echo "  ;$PREFIX*DJ[a-z]* to #1$PREFIX#2DJ#u3#4$SUFFIX"
      echo "  -r ;$PREFIX*DJ[a-z]* to $PREFIX#2DJ#u3#4$SUFFIX"
      echo "  ;$PREFIX*Mc [a-z]* to #1$PREFIX#2Mc #u3#4$SUFFIX"
      echo "  -r ;$PREFIX*Mc [a-z]* to $PREFIX#2Mc #u3#4$SUFFIX"
      echo "  ;$PREFIX*Mc[a-z]* to #1$PREFIX#2Mc#u3#4$SUFFIX"
      echo "  -r ;$PREFIX*Mc[a-z]* to $PREFIX#2Mc#u3#4$SUFFIX"
      echo "  ;$PREFIX*'[A-Z]* to #1$PREFIX#2'#l3#4$SUFFIX"
      echo "  -r ;$PREFIX*'[A-Z]* to $PREFIX#2'#l3#4$SUFFIX"
    fi
  else
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*\?*$SUFFIX" "$PREFIX#1#2$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*'[A-Z]*" "$PREFIX#1'#l2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*DJ [a-z]*" "$PREFIX#1DJ #u2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*DJ[a-z]*" "$PREFIX#1DJ#u2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*Mc [a-z]*" "$PREFIX#1Mc #u2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*Mc[a-z]*" "$PREFIX#1Mc#u2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    OUT=""
    while [ "$OUT" != "Nothing" ]; do
      OUT=`(set \`mmv "$PREFIX*'[A-Z]*" "$PREFIX#1'#l2#3$SUFFIX" 2>&1\`; echo $1)`
    done
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX*\?*$SUFFIX to $PREFIX#1#2$SUFFIX"
      echo "  $PREFIX*'[A-Z]* to $PREFIX#1'#l2#3$SUFFIX"
      echo "  $PREFIX*DJ [a-z]* to $PREFIX#1DJ #u2#3$SUFFIX"
      echo "  $PREFIX*DJ[a-z]* to $PREFIX#1DJ#u2#3$SUFFIX"
      echo "  $PREFIX*Mc [a-z]* to $PREFIX#1Mc #u2#3$SUFFIX"
      echo "  $PREFIX*Mc[a-z]* to $PREFIX#1Mc#u2#3$SUFFIX"
      echo "  $PREFIX*'[A-Z]* to $PREFIX#1'#l2#3$SUFFIX"
    fi
  fi
}

function remove_spaces {
  if [ "$verbose" = "1" ]; then
    echo "Removing spaces,"
    if [ "$1" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$1" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $2"
  fi
  replace " " "" "$1" "$2"
}

function remove_umlaute {
  if [ "$verbose" = "1" ]; then
    echo "Replacing Umlaute,"
    if [ "$1" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$1" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $2"
  fi
  replace "ä" "ae" "$1" "$2"
  replace "ö" "oe" "$1" "$2"
  replace "ü" "ue" "$1" "$2"
  replace "Ä" "Ae" "$1" "$2"
  replace "Ö" "Oe" "$1" "$2"
  replace "Ü" "Ue" "$1" "$2"
  replace "ß" "ss" "$1" "$2"
}

function swap_number_number {
  if [ "$verbose" = "1" ]; then
    echo "Swapping number and number,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi
  
  if [ "$1" = "1" ]; then
    if [ "$2" = "1" ]; then
      if [ "$3" = "1" ]; then
        OUT=`mmv ";$PREFIX*[0-9]*[0-9]*$SUFFIX" "#1$PREFIX#2#5#4#3#6$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and not with directories; executing"
          echo "  ;$PREFIX*[0-9]*[0-9]*$SUFFIX to #1$PREFIX#2#5#4#3#6$SUFFIX"
        fi
      elif [ "$2" = "2" ]; then
        OUT=`mmv ";$PREFIX*[0-9]*[0-9]*$SUFFIX" "#1$PREFIX#2#5#4#3#6$SUFFIX" 2>&1`
        OUT=`mmv -r ";$PREFIX*[0-9]*[0-9]*$SUFFIX" "$PREFIX#2#5#4#3#6$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and with directories; executing"
          echo "  ;$PREFIX*[0-9]*[0-9]*$SUFFIX to #1$PREFIX#2#5#4#3#6$SUFFIX"
          echo "  -r ;$PREFIX*[0-9]*[0-9]*$SUFFIX to $PREFIX#2#5#4#3#6$SUFFIX"
        fi
      else
        OUT=`mmv "$PREFIX*[0-9]*[0-9]*$SUFFIX" "$PREFIX#1#4#3#2#5$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv not used recursively and not with directories; executing"
          echo "  $PREFIX*[0-9]*[0-9]*$SUFFIX to $PREFIX#1#4#3#2#5$SUFFIX"
        fi
      fi
    else
      if [ "$3" = "1" ]; then
        OUT=`mmv ";$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#5#6#4#3#7$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and not with directories; executing"
          echo "  ;$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#5#6#4#3#7$SUFFIX"
        fi
      elif [ "$2" = "2" ]; then
        OUT=`mmv ";$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#5#6#4#3#7$SUFFIX" 2>&1`
        OUT=`mmv -r ";$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX" "$PREFIX#2#5#6#4#3#7$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and with directories; executing"
          echo "  ;$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#5#6#4#3#7$SUFFIX"
          echo "  -r ;$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX to $PREFIX#2#5#6#4#3#7$SUFFIX"
        fi
      else
        OUT=`mmv "$PREFIX*[0-9]*[0-9][0-9]*$SUFFIX" "$PREFIX#1#4#5#3#2#6$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv not used recursively and not with directories; executing"
          echo "  $PREFIX*[0-9]*[0-9][0-9]*$SUFFIX to $PREFIX#1#4#5#3#2#6$SUFFIX"
        fi
      fi
    fi
  else
    if [ "$2" = "1" ]; then
      if [ "$3" = "1" ]; then
        OUT=`mmv ";$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX" "#1$PREFIX#2#6#5#3#4#7$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and not with directories; executing"
          echo "  ;$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX to #1$PREFIX#2#6#5#3#4#7$SUFFIX"
        fi
      elif [ "$2" = "2" ]; then
        OUT=`mmv ";$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX" "#1$PREFIX#2#6#5#3#4#7$SUFFIX" 2>&1`
        OUT=`mmv -r ";$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX" "$PREFIX#2#6#5#3#4#7$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and with directories; executing"
          echo "  ;$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX to #1$PREFIX#2#6#5#3#4#7$SUFFIX"
          echo "  -r ;$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX to $PREFIX#2#6#5#3#4#7$SUFFIX"
        fi
      else
        OUT=`mmv "$PREFIX*[0-9][0-9]*[0-9]*$SUFFIX" "$PREFIX#1#5#4#2#3#6$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv not used recursively and not with directories; executing"
          echo "  $PREFIX*[0-9][0-9]*[0-9]*$SUFFIX to $PREFIX#1#5#4#2#3#6$SUFFIX"
        fi
      fi
    else
      if [ "$3" = "1" ]; then
        OUT=`mmv ";$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#6#7#5#3#4#8$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and not with directories; executing"
          echo "  ;$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#6#7#5#3#4#8$SUFFIX"
        fi
      elif [ "$2" = "2" ]; then
        OUT=`mmv ";$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#6#7#5#3#4#8$SUFFIX" 2>&1`
        OUT=`mmv -r ";$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX" "$PREFIX#2#6#7#5#3#4#8$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv used recursively and with directories; executing"
          echo "  ;$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#6#7#5#3#4#8$SUFFIX"
          echo "  -r ;$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX to $PREFIX#2#6#7#5#3#4#8$SUFFIX"
        fi
      else
        OUT=`mmv "$PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX" "$PREFIX#1#5#6#4#2#3#7$SUFFIX" 2>&1`
        if [ "$verbose" = "1" ]; then
          echo "mmv not used recursively and not with directories; executing"
          echo "  $PREFIX*[0-9][0-9]*[0-9][0-9]*$SUFFIX to $PREFIX#1#5#6#4#2#3#7$SUFFIX"
        fi
      fi
    fi
  fi
}

function swap_string_number {
  if [ "$verbose" = "1" ]; then
    echo "Swapping string and number,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi
  
  if [ "$2" = "1" ]; then
    if [ "$3" = "1" ]; then
      OUT=`mmv ";$PREFIX*$1*[0-9]*$SUFFIX" "#1$PREFIX#2#4#3$1#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*$1*[0-9]*$SUFFIX to #1$PREFIX#2#4#3$1#5$SUFFIX"
      fi
    elif [ "$3" = "2" ]; then
      OUT=`mmv ";$PREFIX*$1*[0-9]*$SUFFIX" "#1$PREFIX#2#4#3$1#5$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*$1*[0-9]*$SUFFIX" "$PREFIX#2#4#3$1#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*$1*[0-9]*$SUFFIX to #1$PREFIX#2#4#3$1#5$SUFFIX"
        echo "  -r ;$PREFIX*$1*[0-9]*$SUFFIX to $PREFIX#2#4#3$1#5$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*$1*[0-9]*$SUFFIX" "$PREFIX#1#3#2$1#4$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  *$PREFIX$1*[0-9]*$SUFFIX to $PREFIX#1#3#2$1#4$SUFFIX"
      fi
    fi
  elif [ "$2" = "2" ]; then
    if [ "$3" = "1" ]; then
      OUT=`mmv ";$PREFIX*$1*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#4#5#3$1#6$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*$1*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#4#5#3$1#6$SUFFIX"
      fi
    elif [ "$3" = "2" ]; then
      OUT=`mmv ";$PREFIX*$1*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#4#5#3$1#6$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*$1*[0-9][0-9]*$SUFFIX" "$PREFIX#2#4#5#3$1#6$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*$1*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#4#5#3$1#6$SUFFIX"
        echo "  -r ;$PREFIX*$1*[0-9][0-9]*$SUFFIX to $PREFIX#2#4#5#3$1#6$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*$1*[0-9][0-9]*$SUFFIX" "$PREFIX#1#3#4#2$1#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  *$PREFIX$1*[0-9][0-9]*$SUFFIX to $PREFIX#1#3#4#2$1#5$SUFFIX"
      fi
    fi
  fi
}

function swap_number_string {
  if [ "$verbose" = "1" ]; then
    echo "Swapping number and string,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  if [ "$1" = "1" ]; then
    if [ "$3" = "1" ]; then
      OUT=`mmv ";$PREFIX*[0-9]*$2*$SUFFIX" "#1$PREFIX#2$2#4#3#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*[0-9]*$2*$SUFFIX to #1$PREFIX#2$2#4#3#5$SUFFIX"
      fi  
    elif [ "$3" = "2" ]; then
      OUT=`mmv ";$PREFIX*[0-9]*$2*$SUFFIX" "#1$PREFIX#2$2#4#3#5$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*[0-9]*$2*$SUFFIX" "$PREFIX#2$2#4#3#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*[0-9]*$2*$SUFFIX to #1$PREFIX#2$2#4#3#5$SUFFIX"
        echo "  -r ;$PREFIX*[0-9]*$2*$SUFFIX to $PREFIX#2$2#4#3#5$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*[0-9]*$2*$SUFFIX" "$PREFIX#1$2#3#2#4$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  $PREFIX*[0-9]*$2*$SUFFIX to $PREFIX#1$2#3#2#4$SUFFIX"
      fi
    fi
  elif [ "$1" = "2" ]; then
    if [ "$3" = "1" ]; then
      OUT=`mmv ";$PREFIX*[0-9][0-9]*$2*$SUFFIX" "#1$PREFIX#2$2#5#3#4#6$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*[0-9][0-9]*$2*$SUFFIX to #1$PREFIX#2$2#5#3#4#6$SUFFIX"
      fi 
    elif [ "$3" = "2" ]; then
      OUT=`mmv ";$PREFIX*[0-9][0-9]*$2*$SUFFIX" "#1$PREFIX#2$2#5#3#4#6$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*[0-9][0-9]*$2*$SUFFIX" "$PREFIX#2$2#5#3#4#6$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*[0-9][0-9]*$2*$SUFFIX to #1$PREFIX#2$2#5#3#4#6$SUFFIX"
        echo "  -r ;$PREFIX*[0-9][0-9]*$2*$SUFFIX to $PREFIX#2$2#5#3#4#6$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*[0-9][0-9]*$2*$SUFFIX" "$PREFIX#1$2#4#2#3#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  $PREFIX*[0-9][0-9]*$2*$SUFFIX to $PREFIX#1$2#4#2#3#5$SUFFIX"
      fi
    fi
  fi
}

function swap_string_string {
  if [ "$verbose" = "1" ]; then
    echo "Swapping string and string,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  if [ "$3" = "1" ]; then
    OUT=`mmv ";$PREFIX*$1*$2*$SUFFIX" "#1$PREFIX#2$2#3$1#4$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX*$1*$2*$SUFFIX to #1$PREFIX#2$2#3$1#4$SUFFIX"
    fi 
  elif [ "$3" = "2" ]; then
    OUT=`mmv ";$PREFIX*$1*$2*$SUFFIX" "#1$PREFIX#2$2#3$1#4$SUFFIX" 2>&1`
    OUT=`mmv -r ";$PREFIX*$1*$2*$SUFFIX" "$PREFIX#2$2#3$1#4$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX*$1*$2*$SUFFIX to #1$PREFIX#2$2#3$1#4$SUFFIX"
      echo "  -r ;$PREFIX*$1*$2*$SUFFIX to $PREFIX#2$2#3$1#4$SUFFIX"
    fi
  else
    OUT=`mmv "$PREFIX*$1*$2*$SUFFIX" "$PREFIX#1$2#2$1#3$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX*$1*$2*$SUFFIX to $PREFIX#1$2#2$1#3$SUFFIX"
    fi
  fi
}

function delete_number {
  if [ "$verbose" = "1" ]; then
    echo "Deleting number,"
    if [ "$2" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$2" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $3"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  if [ "$1" = "1" ]; then
    if [ "$2" = "1" ]; then
      OUT=`mmv ";$PREFIX*[0-9]*$SUFFIX" "#1$PREFIX#2#4$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*[0-9]*$SUFFIX to #1$PREFIX#2#4$SUFFIX"
      fi 
    elif [ "$2" = "2" ]; then
      OUT=`mmv ";$PREFIX*[0-9]*$SUFFIX" "#1$PREFIX#2#4$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*[0-9]*$SUFFIX" "$PREFIX#2#4$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*[0-9]*$SUFFIX to #1$PREFIX#2#4$SUFFIX"
        echo "  -r ;$PREFIX*[0-9]*$SUFFIX to $PREFIX#2#4$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*[0-9]*$SUFFIX" "$PREFIX#1#3$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  $PREFIX*[0-9]*$SUFFIX to $PREFIX#1#3$SUFFIX"
      fi
    fi
  elif [ "$1" = "2" ]; then
    if [ "$2" = "1" ]; then
      OUT=`mmv ";$PREFIX*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and not with directories; executing"
        echo "  ;$PREFIX*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#5$SUFFIX"
      fi
    elif [ "$2" = "2" ]; then
      OUT=`mmv ";$PREFIX*[0-9][0-9]*$SUFFIX" "#1$PREFIX#2#5$SUFFIX" 2>&1`
      OUT=`mmv -r ";$PREFIX*[0-9][0-9]*$SUFFIX" "$PREFIX#2#5$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv used recursively and with directories; executing"
        echo "  ;$PREFIX*[0-9][0-9]*$SUFFIX to #1$PREFIX#2#5$SUFFIX"
        echo "  -r ;$PREFIX*[0-9][0-9]*$SUFFIX to $PREFIX#2#5$SUFFIX"
      fi
    else
      OUT=`mmv "$PREFIX*[0-9][0-9]*$SUFFIX" "$PREFIX#1#4$SUFFIX" 2>&1`
      if [ "$verbose" = "1" ]; then
        echo "mmv not used recursively and not with directories; executing"
        echo "  $PREFIX*[0-9][0-9]*$SUFFIX to $PREFIX#1#4$SUFFIX"
      fi
    fi
  fi
}

function delete_string  {
  if [ "$verbose" = "1" ]; then
    echo "Deleting string,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi

  INS1=""
  INS2=""
  INS3=""
  INS4=""
  PLACE1=""
  PLACE2=""
  let "PLACE1=$1"
  let "PLACE2=$2"
  if [ "$3" = "0" ]; then
    for ((i=1; i<=PLACE1 ; i++))
    do
      INS1="?$INS1"
      INS2="$INS2#$i"
    done
    let "INS4=$PLACE1+$PLACE2+1"
  else
    let "PLACE1=$PLACE1+1"
    for ((i=2; i<=PLACE1 ; i++))
    do
      INS1="?$INS1"
      INS2="$INS2#$i"
    done
    let "INS4=$PLACE1+$PLACE2+1"
  fi
  for ((i=1; i<=PLACE2 ; i++))
  do
    INS3="?$INS3"
  done
  if [ "$3" = "1" ]; then
    OUT=`mmv ";$PREFIX$INS1$INS3*$SUFFIX" "#1$PREFIX$INS2#$INS4$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX$INS1$INS3*$SUFFIX to #1$PREFIX$INS2#$INS4$SUFFIX"
    fi
  elif [ "$3" = "2" ]; then
    OUT=`mmv ";$PREFIX$INS1$INS3*$SUFFIX" "#1$PREFIX$INS2#$INS4$SUFFIX" 2>&1`
    OUT=`mmv -r ";$PREFIX$INS1$INS3*$SUFFIX" "$PREFIX$INS2#$INS4$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX$INS1$INS3*$SUFFIX to #1$PREFIX$INS2#$INS4$SUFFIX"
      echo "  -r ;$PREFIX$INS1$INS3*$SUFFIX to $PREFIX$INS2#$INS4$SUFFIX"
    fi
  else
    OUT=`mmv "$PREFIX$INS1$INS3*$SUFFIX" "$PREFIX$INS2#$INS4$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX$INS1$INS3*$SUFFIX to $PREFIX$INS2#$INS4$SUFFIX"
    fi
  fi
}

function insert_string {
  if [ "$verbose" = "1" ]; then
    echo "Inserting $1 at position $2,"
    if [ "$3" = "2" ]; then
      echo "  recursively and including directories," 
    elif [ "$3" = "1" ]; then
      echo "  recursively,"
    fi
    echo "  using input $4"
  fi
  NUMBER_OF_STARS=""
  regex_stars "$4"
  TEXT_BEFORE_STAR=""
  TEXT_AFTER_STAR=""
  if [ "$NUMBER_OF_STARS" = "0" ]; then
    PREFIX=""
    SUFFIX=""
  elif [ "$NUMBER_OF_STARS" = "1" ]; then
    regex_string "0" "$4"
    regex_string "1" "$4"
    PREFIX="$TEXT_BEFORE_STAR"
    SUFFIX="$TEXT_AFTER_STAR"
  else
    echo "Wrong input!"
    echo "Please read:"
    usage
    exit 1
  fi
  PLACE=""
  let "PLACE=$2"
  INS1=""
  INS2=""
  i=""
  if [ "$3" = "0" ]; then
    for ((i=1; i<=PLACE ; i++))
    do
      INS1="?$INS1"
      INS2="$INS2#$i"
    done
  else
    let "PLACE+=1"
    for ((i=2; i<=PLACE ; i++))
    do
      INS1="?$INS1"
      INS2="$INS2#$i"
    done
  fi
    
  if [ "$3" = "1" ]; then
    OUT=`mmv ";$PREFIX$INS1*$SUFFIX" "#1$PREFIX$INS2$1#$i$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and not with directories; executing"
      echo "  ;$PREFIX$INS1*$SUFFIX to #1$PREFIX$INS2$1#$i$SUFFIX"
    fi
  elif [ "$3" = "2" ]; then
    OUT=`mmv ";$PREFIX$INS1*$SUFFIX" "#1$PREFIX$INS2$1#$i$SUFFIX" 2>&1`
    OUT=`mmv -r ";$PREFIX$INS1*$SUFFIX" "$PREFIX$INS2$1#$i$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv used recursively and with directories; executing"
      echo "  ;$PREFIX$INS1*$SUFFIX to #1$PREFIX$INS2$1#$i$SUFFIX"
      echo "  -r ;$PREFIX$INS1*$SUFFIX to $PREFIX$INS2$1#$i$SUFFIX"
    fi
  else
    OUT=`mmv "$PREFIX$INS1*$SUFFIX" "$PREFIX$INS2$1#$i$SUFFIX" 2>&1`
    if [ "$verbose" = "1" ]; then
      echo "mmv not used recursively and not with directories; executing"
      echo "  $PREFIX$INS1*$SUFFIX to $PREFIX$INS2$1#$i$SUFFIX"
    fi
  fi
}

function usage {
  echo "  Usage:"
  echo "    renamer [ options ] [ additional strings ] [ input ]"
  echo "   Simple options:"
  echo "     -v --verbose       Give feedback, what is done"
  echo "     -r --recursive     Renames files also in subdirs"
  echo "     -R --directories   Renames also subdirs themselves"
  echo "     -x --extended      'I'M_dj  MC Donalds?' is renamed to"
  echo "                        'I'm DJ McDonalds'"
  echo "     -l --umlaute       Replaces German Umlaute"
  echo "     -u --upcase        'it's only rock'n'roll' is renamed to"
  echo "                        'It's Only Rock'n'roll'"
  echo "     -c --spaces        Removes spaces"
  echo "   Extended options:"
  echo "     -p --replace \"string_1\" \"string_2\""
  echo "                        Replaces string_1 by string_2"
  echo "                        string_2 may be empty"
  echo "     -i --insert \"string\" \"position\""
  echo "                        Inserts string at position"
  echo "                        position is an integer, starting at 0"
  echo "     -s --swap \"argument_1\" \"argument_2\""
  echo "                        Swaps argument_1 and argument_2"
  echo "                        argument 1 and 2 may be"
  echo "                          - a string"
  echo "                          - #   (= a single number)"
  echo "                          - ##  (= a double number)"
  echo "                        in any combination"
  echo "     -d --delete \"argument\""
  echo "                        Deletes a number"
  echo "                        argument may be"
  echo "                          - #   (= a single number)"
  echo "                          - ##  (= a double number)"
  echo "     -d --delete \"position\" \"length\""
  echo "                        Deletes a string"
  echo "                        position is an integer, starting at 0"
  echo "                        length is an integer"
  echo "   Input:"
  echo "                        Valid inputs:"
  echo "                          - \"*\" (= any file)"
  echo "                          - \"some_prefix*\""
  echo "                          - \"*some_suffix\""
  echo "                          - \"some_prefix*some_suffix\""
  echo "                          - \"\" (left empty = any file)"
  echo ""
  echo " Please note:"
  echo "   - Simple options may be combined as desired,"
  echo "     but you may only choose ONE extended option."
  echo "   - If multiple options are used, they will be treated"
  echo "     in the following order:"
  echo "     swap/delete/replace/insert, extended, umlaute, upcase, spaces"
  echo "   - If input contains a prefix, --upcase will cause"
  echo "     --spaces to fail, as the prefix will be upcased, too."
  echo "   - Arguments for --swap need to be in order of appearence"
  echo "     otherwise --swap will fail."
  echo "   - Extended options will fail on prefixes and suffixes."
  echo "     Simply shorten your prefixes and suffixes."
}

function help {
  echo "Interface to mmv"
  echo "(c) by Georg Hennig 2004"
  usage
}

helper="0"
breaker="0"
recursive="0"
directories="0"
extended="0"
umlaute="0"
upcase="0"
spaces="0"
replace="0"
insert="0"
swap="0"
delete="0"
first=""
second=""
third=""
fourth=""

for argument
do
  if [[ $argument == "-h" || $argument == "--help" || $argument == "-?" ]]; then
    let "helper=1"
  elif [[ $argument == "-v" || $argument == "--verbose" ]]; then
    let "verbose=1"
  elif [[ $argument == "-r" || $argument == "--recursive" ]]; then
    let "recursive=1"
  elif [[ $argument == "-R" || $argument == "--directories" ]]; then
    let "directories=1"
  elif [[ $argument == "-x" || $argument == "--extended" ]]; then
    let "extended=1"
  elif [[ $argument == "-l" || $argument == "--umlaute" ]]; then
    let "umlaute=1"
  elif [[ $argument == "-u" || $argument == "--upcase" ]]; then
    let "upcase=1"
  elif [[ $argument == "-c" || $argument == "--spaces" ]]; then
    let "spaces=1"
  elif [[ $argument == "-p" || $argument == "--replace" ]]; then
    let "replace=1"
  elif [[ $argument == "-i" || $argument == "--insert" ]]; then
    let "insert=1"
  elif [[ $argument == "-s" || $argument == "--swap" ]]; then
    let "swap=1"
  elif [[ $argument == "-d" || $argument == "--delete" ]]; then
    let "delete=1"
  elif [[ $first == "" ]]; then
    first="$argument"
  elif [[ $second == "" ]]; then
    second="$argument"
  elif [[ $third == "" ]]; then
    third="$argument"
  elif [[ $fourth == "" ]]; then
    fourth="$argument"
  fi
done

if [[ $helper == "1" ]]; then
  help
  exit 0
fi

if [[ "$directories" = "1" && "$recursive" != "1" ]]; then
  echo "  You are using -R; adding -r..."
  recursive="1"
fi

if [[ "$fourth" != "" ]]; then
  echo "Too many arguments!"
  echo "Please read:"
  usage
  exit 1
fi

if [[ "$swap" = "1" ]]; then
  if [[ "$first" = "" || "$second" = "" ]]; then
    echo "You need to set two things to swap!"
    echo "Please read:"
    usage
    exit 1
  else
    if [[ "$third" = "" ]]; then
      third="*"
    fi
  fi
fi

if [[ "$delete" = "1" ]]; then
  if [[ "$first" != "#" && "$first" != "##" && "$second" = "" ]]; then
    echo "You need to set starting position and length to delete!"
    echo "Please read:"
    usage
    exit 1
  else
    if [[ "$third" = "" ]]; then
      third="*"
    fi
  fi
fi

if [[ "$insert" = "1" ]]; then
  if [[ "$first" = "" || "$second" = "" ]]; then
    echo "You need to set the string and the place to insert!"
    echo "Please read:"
    usage
    exit 1
  else
    if [[ "$third" = "" ]]; then
      third="*"
    fi
  fi
fi

if [[ "$replace" = "1" ]]; then
  if [[ "$first" = "" || "$second" = "" ]]; then
    echo "You need to set the two strings replace!"
    echo "Please read:"
    usage
    exit 1
  else
    if [[ "$third" = "" ]]; then
      third="*"
    fi
  fi
fi

if [[ "$third" == "" ]]; then
  if [[ "$second" != "" ]]; then
    third="$second"
    second=""
  elif [[ "$second" == "" && "$first" != "" ]]; then
    third="$first"
    first=""
  else
    third="*"
  fi  
fi

#--> third will always contain something like *.mp3

if [[ "$helper" = "0" && "$extended" = "0" &&
      "$umlaute" = "0" && "$upcase" = "0" && "$spaces" = "0" && "$replace" = "0" &&
      "$insert" = "0" && "$swap" = "0" && "$delete" = "0" ]]; then
  echo "I don't know what to do!"
  echo "Please read:"
  usage
  exit 1
fi

if [ "$swap" = "1" ]; then
  if [ "$insert" = "1" ]; then
    echo "Error. You are trying to switch and insert at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  elif [ "$delete" = "1" ]; then
    echo "Error. You are trying to switch and delete a string at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  elif [ "$replace" = "1" ]; then
    echo "Error. You are trying to switch and replace a string at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  fi
elif [ "$insert" = "1" ]; then
  if [ "$delete" = "1" ]; then
    echo "Error. You are trying to insert and delete a string at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  elif [ "$replace" = "1" ]; then
    echo "Error. You are trying to insert and replace a string at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  fi
elif [ "$delete" = "1" ]; then
  if [ "$replace" = "1" ]; then
    echo "Error. You are trying to delete and replace a string at a time."
    echo "       That's not possible."
    echo "Please read:"
    usage
    exit 0
  fi
fi

#Fehlerkorrektur fÃ¼r spÃ¤ter: 'mmv "#101blablabla" "..."' funktioniert nicht,
#muss 'mmv "#1\01blablabla" "..." sein
if [ "$swap" = "1" ]; then
  if [ `expr match "$first" '[0-9]'` != "0" ]; then
    first="\\$first"
  fi
fi
if [ "$swap" = "1" ]; then
  if [ `expr match "$second" '[0-9]'` != "0" ]; then
    second="\\$second"
  fi
fi
if [ "$insert" = "1" ]; then
  if [ `expr match "$first" '[0-9]'` != "0" ]; then
    first="\\$first"
  fi
fi

if [ "$swap" = "1" ]; then
  if [[ "$first" == "#" && "$second" == "#" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_number "1" "1" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_number "1" "1" "2" "$third"
    else
       swap_number_number "1" "1" "0" "$third"
    fi
  elif [[ "$first" == "#" && "$second" == "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_number "1" "2" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_number "1" "2" "2" "$third"
    else
       swap_number_number "1" "2" "0" "$third"
    fi
  elif [[ "$first" == "##" && "$second" == "#" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_number "2" "1" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_number "2" "1" "2" "$third"
    else
       swap_number_number "2" "1" "0" "$third"
    fi
  elif [[ "$first" == "##" && "$second" == "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_number "2" "2" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_number "2" "2" "2" "$third"
    else
       swap_number_number "2" "2" "0" "$third"
    fi
  elif [[ "$first" == "#" && "$second" != "#" && "$second" != "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_string "1" "$second" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_string "1" "$second" "2" "$third"
    else
       swap_number_string "1" "$second" "0" "$third"
    fi
  elif [[ "$first" == "##" && "$second" != "#" && "$second" != "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_number_string "2" "$second" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_number_string "2" "$second" "2" "$third"
    else
       swap_number_string "2" "$second" "0" "$third"
    fi
  elif [[ "$second" == "#" && "$first" != "#" && "$first" != "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_str_number "$first" "1" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_string_number "$first" "1" "2" "$third"
    else
       swap_string_number "$first" "1" "0" "$third"
    fi
  elif [[ "$second" == "##" && "$first" != "#" && "$first" != "##" ]]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_string_number "$first" "2" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_string_number "$first" "2" "2" "$third"
    else
       swap_string_number "$first" "2" "0" "$third"
    fi
  else
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
       swap_string_string "$first" "$second" "1" "$third"
    elif [ "$directories" = "1" ]; then
       swap_string_string "$first" "$second" "2" "$third"
    else
       swap_string_string "$first" "$second" "0" "$third"
    fi
  fi
fi

if [ "$delete" = "1" ]; then
  if [ "$first" = "#" ]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
      delete_number "1" "1" "$third"
    elif [ "$directories" = "1" ]; then
      delete_number "1" "2" "$third"
    else
      delete_number "1" "0" "$third"
    fi
  elif [ "$first" = "##" ]; then
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
      delete_number "2" "1" "$third"
    elif [ "$directories" = "1" ]; then
      delete_number "2" "2" "$third"
    else
      delete_number "2" "0" "$third"
    fi
#  elif [[ `expr match "$first" '[0-9]'` != "0" && `expr match "$second" '[0-9]'` != "0" ]]; then
else
    if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
      delete_string "$first" "$second" "1" "$third"
    elif [ "$directories" = "1" ]; then
      delete_string "$first" "$second" "2" "$third"
    else
      delete_string "$first" "$second" "0" "$third"
    fi
#  else
#    echo "Some delete error occured."
#    echo "Did you make a mistake?"
#    echo "Please read:"
#    usage
#    exit 0
  fi
fi

if [ "$replace" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    replace "$first" "$second" "1" "$third"
  elif [ "$directories" = "1" ]; then
    replace "$first" "$second" "2" "$third"
  else
    replace "$first" "$second" "0" "$third"
  fi
fi

if [ "$insert" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    insert_string "$first" "$second" "1" "$third"
  elif [ "$directories" = "1" ]; then
    insert_string "$first" "$second" "2" "$third"
  else
    insert_string "$first" "$second" "0" "$third"
  fi
fi

if [ "$extended" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    extended "1" "$third"
  elif [ "$directories" = "1" ]; then
    extended "2" "$third"
  else
    extended "0" "$third"
  fi
fi

if [ "$umlaute" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    remove_umlaute "1" "$third"
  elif [ "$directories" = "1" ]; then
    remove_umlaute "2" "$third"
  else
    remove_umlaute "0" "$third"
  fi
fi

if [ "$upcase" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    upcase "1" "$third"
  elif [ "$directories" = "1" ]; then
    upcase "2" "$third"
  else
    upcase "0" "$third"
  fi
fi

if [ "$spaces" = "1" ]; then
  if [ "$recursive" = "1" ] && [ "$directories" != "1" ]; then
    remove_spaces "1" "$third"
  elif [ "$directories" = "1" ]; then
    remove_spaces "2" "$third"
  else
    remove_spaces "0" "$third"
  fi
fi
