#!/bin/bash
# copyright Kevin Day 2007, under the FLL license
# This is under the FLL license (Featureless Linux Library)

# File size difference example between the three formats:
# 48M test.wav  - best quality without compression
# 34M test.flac - best quality available with compression
# 34M test.wv   - best quality available with compression
# 16M test.ogg  - great quality, great compression
# 8.6M test.mp3 - poor quality, best compression

funkify(){
  echo -e "${color_important}Waiting for cdrom..${color_reset}"
  while (true); do
    RETURN=$(cdparanoia -sQ -d $Cdrom &>/dev/stdout)

    if [[ $? == 0 ]] ; then
      break
    fi

    sleep 8
  done

  # Identify the disk & Total tracks
  local DISC_ID=$(discid $Cdrom | awk '{ print $1 }' | sed -e 's|A|a|g' -e 's|B|b|g' -e 's|C|c|g' -e 's|D|d|g' -e 's|E|e|g' -e 's|F|f|g' -e 's|G|g|g' -e 's|H|h|g' -e 's|I|I|g' -e 's|J|j|g' -e 's|K|k|g' -e 's|L|l|g' -e 's|M|m|g' -e 's|N|n|g' -e 's|O|o|g' -e 's|P|p|g' -e 's|Q|q|g' -e 's|R|r|g' -e 's|S|s|g' -e 's|T|t|g' -e 's|U|u|g' -e 's|V|v|g' -e 's|W|w|g' -e 's|X|x|g' -e 's|Y|y|g' -e 's|Z|z|g' )
  if [[ $DISC_ID == "" ]] ; then echo -e "${color_error}ERROR: discid Failed for some reason${color_reset}" ; eject $Cdrom ; return 100; fi

  local TOTAL_TRACKS=$(discid $Cdrom | awk '{ print $2 }')
  local FILE=/tmp/cddb_text/${DISC_ID}
  local first_host=gnudb.org
  local second_host=freedb.org

  # Gatther all cddb information and have possible prompts
  if [[ -e $FILE && $Cddb_Automation != "file" ]] ; then rm -f $FILE ; fi

  if [[ $Cddb_Automation == "yes" ]] ; then
    cddb.pl -I -T -t -H $first_host -c $Cdrom

    if [[ $? -ne 0 ]] ; then
     Cddb_Automation="ask"
    elif [[ ! -e $FILE ]] ; then
      cddb.pl -I -T -t -H $second_host -c $Cdrom

      if [[ $? -ne 0 || ! -e $FILE ]] ; then
        Cddb_Automation="ask"
      fi
    fi
  elif [[ $Cddb_Automation == "no" ]] ; then
    cddb.pl -T -t -H $first_host -c $Cdrom

    if [[ $? -ne 0 ]] ; then
     Cddb_Automation="ask"
    elif [[ ! -e $FILE ]] ; then
      cddb.pl -T -t -H $second_host -c $Cdrom

      if [[ $? -ne 0 || ! -e $FILE ]] ; then
        Cddb_Automation="ask"
      fi
    fi
  fi

  local ALBUM=
  local ARTIST=
  local GENRE=
  local YEAR=
  local TRACK_FIX_QUESTION=
  local location=
  local Current_Track=
  local Mode_Command=
  local Actual_Track=
  let location=1
  let Current_Track=1
  let Actual_Track=1

  if [[ $Skip == "last" ]] ; then
    let TOTAL_TRACKS=$TOTAL_TRACKS-1
  elif [[ $Skip == "first" ]] ; then
    let location=2
    let Current_Track=2
  elif [[ $Skip == "both" ]] ; then
    let TOTAL_TRACKS=$TOTAL_TRACKS-1
    let location=2
    let Current_Track=2
  fi

  if [[ $Cddb_Automation != "ask" ]] ; then

    if [ ! -e $FILE ] ; then
      echo -e "${color_warning}WARNING: CDDB Failed for some reason (the file '$FILE' does not exist)${color_reset}"
    fi

    # access the file, then read and store all information
    ALBUM=$(grep "^title " $FILE | sed -e "s|^title ||")
    ARTIST=$(grep "^artist " $FILE | sed -e "s|^artist ||")
    GENRE=$(grep "^genre " $FILE | sed -e "s|^genre ||")
    YEAR=$(grep "^year " $FILE | sed -e "s|^year ||")
  else
    echo -e -n "${color_notice}Enter in the ${color_reset}${color_highlight}ALBUM${color_reset}${color_notice}:${color_reset} "
    read ALBUM

    echo -e -n "${color_notice}Enter in the ${color_reset}${color_highlight}ARTIST${color_reset}${color_notice}:${color_reset} "
    read ARTIST

    echo -e -n "${color_notice}Enter in the ${color_reset}${color_highlight}GENRE${color_reset}${color_notice} (ie: Heavy Metal):${color_reset} "
    read GENRE

    echo -e -n "${color_notice}Enter in the ${color_reset}${color_highlight}YEAR${color_reset}${color_notice}:${color_reset} "
    read YEAR

    while [[ $TRACK_FIX_QUESTION != "y" &&  $TRACK_FIX_QUESTION != "n" && $TRACK_FIX_QUESTION != "yes" && $TRACK_FIX_QUESTION != "no" && $Skip == "none" ]] ; do
      echo -e -n "${color_notice}The CD-Rom claims to have ${color_reset}${color_highlight}$TOTAL_TRACKS Tracks${color_reset}${color_notice}, is this correct? (yes/no):${color_reset} "
      read TRACK_FIX_QUESTION

      TRACK_FIX_QUESTION=$(echo $TRACK_FIX_QUESTION | sed -e 's|Y|y|' -e 's|E|e|' -e 's|S|s|' -e 's|N|n|' -e 's|O|o|')
      if [[ $TRACK_FIX_QUESTION == "no" || $TRACK_FIX_QUESTION == "n" ]] ; then
        let TOTAL_TRACKS=$TOTAL_TRACKS-1
      fi
    done

    TRACK_FIX_QUESTION=""

    while [[ $TRACK_FIX_QUESTION != "y" &&  $TRACK_FIX_QUESTION != "n" && $TRACK_FIX_QUESTION != "yes" && $TRACK_FIX_QUESTION != "no" && $Skip == "none" ]] ; do
      echo -e -n "${color_notice}Some Enhanced CD-Roms do not have a 1'st audio track, does this one have a ${color_reset}${color_highlight}valid first audio track${color_reset}${color_notice}? (yes/no):${color_reset} "
      read TRACK_FIX_QUESTION

      TRACK_FIX_QUESTION=$(echo $TRACK_FIX_QUESTION | sed -e 's|Y|y|' -e 's|E|e|' -e 's|S|s|' -e 's|N|n|' -e 's|O|o|')
      if [[ $TRACK_FIX_QUESTION == "no" || $TRACK_FIX_QUESTION == "n" ]] ; then
        let location=2
        let Current_Track=2
      fi
    done

    unset TRACK_FIX_QUESTION

    local TRACK_ARRAY[$TOTAL_TRACKS]=""

    while [ $location -le $TOTAL_TRACKS ] ; do
      echo -e -n "${color_notice}Enter in the ${color_reset}${color_highlight}TRACK #$location${color_reset}${color_notice}:${color_reset} "
      read TRACK_ARRAY[$location]

      let location=$location+1
    done
  fi

  if [[ $ALBUM == "" ]] ; then ALBUM="Unknown-${DISC_ID}" ; fi
  if [[ $ARTIST == "" ]] ; then ARTIST="Unknown" ; fi
  if [[ $GENRE == "" ]] ; then GENRE="Unknown" ; fi
  if [[ $YEAR == "" ]] ; then YEAR="Unknown" ; fi


  # Define the directory order
  local FIRST=
  local SECOND=
  if [[ $Structure == "album" ]] ; then
    FIRST=$ALBUM
    SECOND=$ARTIST
  elif [[ $Structure == "artist" ]] ; then
    FIRST=$ARTIST
    SECOND=$ALBUM
  elif [[ $Structure == "album_only" ]] ; then
    FIRST=$ALBUM
  elif [[ $Structure == "artist_only" ]] ; then
    FIRST=$ARTIST
  fi

  # Settings
  local MUSIC=/home/music/autoripped/$Encode_Format
  local PARENT="$MUSIC/$(echo $FIRST | sed -e 's|/|-|g' -e 's|"||g' -e 's|`||g' -e 's|{||g' -e 's|}||g' -e 's|\[||g' -e 's|]||g' -e 's|\$||g' -e 's|\^||g' -e 's|*||g' -e 's|\\||g' -e 's|<||g' -e 's|>||g' -e 's@|@@g')"
  local DEST=
  local Permissions="o+rX,g+rwX"
  local Group="music"

  if [[ $SECOND != "" ]] ; then
    DEST="$PARENT/$(echo $SECOND | sed -e 's|/|-|g' -e 's|"||g' -e 's|`||g' -e 's|{||g' -e 's|}||g' -e 's|\[||g' -e 's|]||g' -e 's|\$||g' -e 's|\^||g' -e 's|*||g' -e 's|\\||g' -e 's|<||g' -e 's|>||g' -e 's@|@@g')"
  else
    DEST="$PARENT"
  fi

  # Do not allow overwritting
  if [[ $Overwrite == "no" && -d "$DEST" ]] ; then
    echo -e "${color_error}ERROR: Overwrite is set to off and the destination directory already exists, ripping has been cancelled.${color_reset}"
    let TOTAL_TRACKS=-1 ; 
  else
    # Create the Artist & Album Directories
    mkdir -p "$DEST"

    if [[ $Enforce_Permissions == "yes" ]] ; then
      chgrp $Group "$MUSIC" "$DEST" "$PARENT"
      chmod $Permissions "$MUSIC" "$DEST" "$PARENT"
    fi
  fi

  # Handle the Mode & Bitrate data
  if [[ $Encode_Format == "mp3" ]] ; then
    if [[ $Mode == "cbr" ]] ; then
      Mode_Command="--preset cbr $Bitrate"
    elif [[ $Mode == "abr" ]] ; then
      Mode_Command="--preset $Bitrate"
    elif [[ $Mode == "vbr" ]] ; then
      Mode_Command="-v"
    fi
  elif [[ $Encode_Format == "ogg" ]] ; then
    if [[ $Mode == "cbr" ]] ; then
      Mode_Command="--managed -m $Bitrate -M $Bitrate -b $Bitrate"
    elif [[ $Mode == "abr" ]] ; then
      Mode_Command="--managed -b $Bitrate"
    elif [[ $Mode == "vbr" ]] ; then
      Mode_Command="-q 10"
    fi
  elif [[ $Encode_Format == "flac" ]] ; then
    # at this time I am not aware of flac being able to change these modes
    Mode_Command="--best"
  elif [[ $Encode_Format == "wv" ]] ; then
    # at this time I am not aware of wavpack being able to change these modes
    Mode_Command="-h"
  fi

  # Begin Ripping Wavfile
  local Track_Number=
  local TRACK=
  while [ $Current_Track -le $TOTAL_TRACKS ] ; do

    if [[ $Cddb_Automation != "ask" ]] ; then
      TRACK=$(grep "^track_$Current_Track " $FILE | sed -e "s|track_$Current_Track ||")
    else
      TRACK=${TRACK_ARRAY[$Current_Track]}
    fi

    if [[ $Actual_Track -lt 10 ]] ; then
      Track_Number="0$Actual_Track"
    else
      Track_Number="$Actual_Track"
    fi

    if [[ $TRACK == "" ]] ; then TRACK="Track $Track_Number"; fi

    MFILENAME=$DEST/$(echo $ALBUM: $Track_Number - $TRACK | sed -e 's|/|-|g' -e 's|"||g' -e 's|`||g' -e 's|{||g' -e 's|}||g' -e 's|\[||g' -e 's|]||g' -e 's|\$||g' -e 's|\^||g' -e 's|*||g' -e 's|\\||g' -e 's|<||g' -e 's|>||g' -e 's@|@@g')

    cdparanoia -d $Cdrom $Current_Track -z "$MFILENAME.wav"
    if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: cdparanoia failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 101 ; fi

    if [[ $Encode_Format == "ogg" ]] ; then
      oggenc $Mode_Command -d "$YEAR" -l "$ALBUM" -t "$TRACK" -N $Track_Number -a "$ARTIST" -G "$GENRE" "$MFILENAME.wav"
      if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: oggenc failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 102 ; fi
      rm -f "$MFILENAME.wav"
      vorbisgain "$MFILENAME.ogg"
      if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: vorbisgain failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 103 ; fi

      if [[ $Enforce_Permissions == "yes" ]] ; then
        chgrp $Group "$MFILENAME.ogg"
        chmod $Permissions "$MFILENAME.ogg"
      fi
    elif [[ $Encode_Format == "flac" ]] ; then
      flac -f --replay-gain $Mode_Command -T="Title=$TRACK" -T="Artist=$ARTIST" -T="Album=$ALBUM" -T="Genre=$GENRE" -T="Year=$YEAR" -T="Cddb_Id=$DISC_ID" -T="Track=$Track_Number" "$MFILENAME.wav" -o "$MFILENAME.flac"
      if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: flac failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 104 ; fi
      rm -f "$MFILENAME.wav"

      if [[ $Enforce_Permissions == "yes" ]] ; then
        chgrp $Group "$MFILENAME.flac"
        chmod $Permissions "$MFILENAME.flac"
      fi
    elif [[ $Encode_Format == "mp3" ]] ; then
      lame -V2 $Mode_Command -h --replaygain-accurate --tt "$TRACK" --ta "$ARTIST" --tl "$ALBUM" --ty "$YEAR" --tn "$Track_Number" --tg "$GENRE" --id3v2-only "$MFILENAME.wav"
      if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: lame failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 105 ; fi
      rm -f "$MFILENAME.wav"

      if [[ $Enforce_Permissions == "yes" ]] ; then
        chgrp $Group "$MFILENAME.mp3"
        chmod $Permissions "$MFILENAME.mp3"
      fi
    elif [[ $Encode_Format == "wv" ]] ; then
      wavpack -y -d $Mode_Command -w "Title=$TRACK" -w "Artist=$ARTIST" -w "Album=$ALBUM" -w "Genre=$GENRE" -w "Year=$YEAR" -w "Cddb_Id=$DISC_ID" -w "Track=$Track_Number" "$MFILENAME.wav" -o "$MFILENAME.wv"
      if [[ $? -ne 0 ]] ; then echo -e "${color_error}ERROR: wavpack failed for some reason${color_reset}" ; eject $Cdrom ; rm -f $FILE ; return 106 ; fi

      if [[ $Enforce_Permissions == "yes" ]] ; then
        chgrp $Group "$MFILENAME.wv"
        chmod $Permissions "$MFILENAME.wv"
      fi
    fi
    let Current_Track=${Current_Track}+1
    let Actual_Track=${Actual_Track}+1
  done

  # we are done!
  if [[ $? -eq 0 ]] ; then eject $Cdrom ; fi
  if [[ $Cddb_Automation != "file" ]] ; then rm -f $FILE ; fi
}


cdripper_main(){
  # Create Basic Colors
  local color_reset="\\033[00m"
  local color_error="\\033[01m\\033[1;31m"
  local color_warning="\\033[33m"
  local color_title="\\033[01m\\033[1;33m"
  local color_highlight="\\033[01m\\033[1;32m"
  local color_notice="\\033[01m"
  local color_important="\\033[32m"

  # set default options
  local cdripper_version="1.0.5"
  local Encode_Format="ogg"
  local Cddb_Automation="yes"
  local Overwrite="no"
  local Enforce_Permissions="no"
  local Cdrom="/dev/cdrom0"
  local Skip="none"
  local Structure="artist"
  local Mode="vbr"
  local Bitrate="320"

  # create a variable so that that color changes can be stripped out on execute
  local Command_Line=

  # tell the parameter parser that the next value needs to be stored somehow
  local Handle_Parameter=

  # first look for only -l for light color and -n for no color at all
  for i in $@ ; do
    if [[ $i == "-l" || $i == "--light" ]] ; then
      color_error="\\033[01m\\033[1;31m"
      color_warning="\\033[31m"
      color_title="\\033[01m\\033[1;34m"
      color_highlight="\\033[01m\\033[1;34m"
      color_notice="\\033[01m"
      color_important="\\033[34m"

    elif [[ $i == "-n" || $i == "--no_color" ]] ; then
      color_error=
      color_warning=
      color_title=
      color_highlight=
      color_notice=
      color_important=
    else
      # re-construct the command line parameters without the color commands
      Command_Line="$Command_Line$i "
    fi
  done

  for i in $Command_Line ; do
    if [[ $i == "-h" || $i == "--help" ]] ; then
      echo
      echo -e "${color_title}Turtle Kevux CD-Ripper Application$color_reset"
      echo -e "      ${color_notice}Version $cdripper_version$color_reset"
      echo
      echo -e " This Program will automatically rip, query cddb, encode, and eject a standard audio cd-rom"
      echo -e " There are currently 4 available formats to encode into: ${color_important}flac$color_reset, ${color_important}wv (wavpack)$color_reset, ${color_important}ogg$color_reset, and ${color_important}mp3$color_reset"
      echo -e " ${color_important}flac$color_reset = lossless, highest quality, weakest compression (large file size)"
      echo -e " ${color_important}wv$color_reset   = lossless, highest quality, weakest compression (large file size)"
      echo -e " ${color_important}ogg$color_reset  = lossy, great quality, great compression (moderate file size)"
      echo -e " ${color_important}mp3$color_reset  = lossy, lowest quality, best compression (small file size)"
      echo
      echo -e "${color_highlight}Options:$color_reset"
      echo -e " -${color_important}h${color_reset}, --${color_important}help${color_reset}        print this help screen"
      echo -e " -${color_important}v${color_reset}, --${color_important}version${color_reset}     print the version number"
      echo -e " -${color_important}l${color_reset}, --${color_important}light${color_reset}       provides color options that are readable on white/light background"
      echo -e " -${color_important}n${color_reset}, --${color_important}no_color${color_reset}    do not output in color"
      echo -e " -${color_important}f${color_reset}, --${color_important}format${color_reset}      specify encoding format: flac, ogg, or mp3"
      echo -e " -${color_important}a${color_reset}, --${color_important}automate${color_reset}    specify whether or not to ask the user select a cddb query (yes, no, ask, or file)"
      echo -e " -${color_important}o${color_reset}, --${color_important}overwrite${color_reset}   forcefully re-encode an already ripped project"
      echo -e " -${color_important}d${color_reset}, --${color_important}device${color_reset}      specify the desired cdrom device"
      echo -e " -${color_important}s${color_reset}, --${color_important}skip${color_reset}        ignore first or last track on buggy disks: first, last, both"
      echo -e " -${color_important}k${color_reset}, --${color_important}structure${color_reset}   method to store music: album, artist, album_only, artist_only"
      echo -e " -${color_important}m${color_reset}, --${color_important}mode${color_reset}        change bitrate mode to one of the following: cbr, abr, vbr"
      echo -e " -${color_important}b${color_reset}, --${color_important}bitrate${color_reset}     bitrate to use when in cbr or abr mode: 320, 256, 192, 128, 64"
      echo -e " -${color_important}p${color_reset}, --${color_important}permissions${color_reset} force permissions on created files/directories"
      echo -e " +${color_important}p${color_reset}, ++${color_important}permissions${color_reset} do not enforce permissions on created files/directories"
      echo
      echo -e "${color_highlight}Defaults:$color_reset"
      echo -e " Format: ${color_important}ogg$color_reset"
      echo -e " Automation: ${color_important}yes$color_reset"
      echo -e " CD-Rom device: ${color_important}/dev/cdrom0$color_reset"
      echo -e " Structure: ${color_important}artist$color_reset"
      echo -e " Mode: ${color_important}vbr$color_reset"
      echo -e " Bitrate: ${color_important}320$color_reset"
      echo -e " Will not overwrite"
      echo -e " Will not enforce permissions"
      echo
      return 0
    elif [[ $Handle_Parameter == "" ]] ; then
      if [[ $i == "-f" || $i == "--format" ]] ; then
        Handle_Parameter="format"
      elif [[ $i == "-a" || $i == "--automate" ]] ; then
        Handle_Parameter="automate"
      elif [[ $i == "-o" || $i == "--overwrite" ]] ; then
        Overwrite="yes"
      elif [[ $i == "-p" || $i == "--permissions" ]] ; then
        Enforce_Permissions="yes"
      elif [[ $i == "+p" || $i == "++permissions" ]] ; then
        Enforce_Permissions="no"
      elif [[ $i == "-d" || $i == "--device" ]] ; then
        Handle_Parameter="device"
      elif [[ $i == "-s" || $i == "--skip" ]] ; then
        Handle_Parameter="skip"
      elif [[ $i == "-k" || $i == "--structure" ]] ; then
        Handle_Parameter="structure"
      elif [[ $i == "-m" || $i == "--mode" ]] ; then
        Handle_Parameter="mode"
      elif [[ $i == "-b" || $i == "--bitrate" ]] ; then
        Handle_Parameter="bitrate"
      elif [[ $i == "-v" || $i == "--version" ]] ; then
        echo $cdripper_version
        return 0
      fi
    else
      if [[ $Handle_Parameter == "format" ]] ; then
        Encode_Format=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "automate" ]] ; then
        Cddb_Automation=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "device" ]] ; then
        Cdrom=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "skip" ]] ; then
        Skip=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "structure" ]] ; then
        Structure=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "mode" ]] ; then
        Mode=$i
        Handle_Parameter=""
      elif [[ $Handle_Parameter == "bitrate" ]] ; then
        Bitrate=$i
        Handle_Parameter=""
      fi
    fi
  done

  if [[ $Encode_Format == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a format: flac, ogg, or mp3$color_reset"
    return 1
  fi

  if [[ $Encode_Format != "flac" && $Encode_Format != "ogg" && $Encode_Format != "mp3" && $Encode_Format != "wv" ]] ; then
    echo -e "${color_error}ERROR: Please specify a valid encode format: flac, wv, ogg, or mp3$color_reset"
    return 1
  fi

  if [[ $Cddb_Automation == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify an automation: yes, no, ask, or file$color_reset"
    return 2
  fi

  if [[ $Cddb_Automation != "yes" && $Cddb_Automation != "no" && $Cddb_Automation != "ask" && $Cddb_Automation != "file" ]] ; then
    echo -e "${color_error}ERROR: Please specify a valid cddb automation: yes, no, ask, or file$color_reset"
    return 2
  fi

  if [[ $Cdrom == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a CD-Rom Device$color_reset"
    return 3
  fi

  if [[ ! -e $Cdrom ]] ; then
    echo -e "${color_error}ERROR: Unable to find or access the specified CD-Rom Device: $Cdrom$color_reset"
    return 3
  fi

  if [[ $Skip == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a Skip: first, last, or both$color_reset"
    return 4
  fi

  if [[ $Skip == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a valid Skip: first, last, or both$color_reset"
    return 4
  fi

  if [[ $Structure == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a Skip: album, artist, album_only, or artist_only$color_reset"
    return 5
  fi

  if [[ $Structure != "album" && $Structure != "artist" && $Structure != "album_only" && $Structure != "artist_only" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a valid Structure: album, artist, album_only, or artist_only$color_reset"
    return 5
  fi

  if [[ $Mode == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a Mode: cbr, abr, or vbr$color_reset"
    return 6
  fi

  if [[ $Mode != "cbr" && $Mode != "abr" && $Mode != "vbr" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a valid Mode: cbr, abr, or vbr$color_reset"
    return 6
  fi

  if [[ $Bitrate == "" ]] ; then
    echo -e "${color_error}ERROR: You did not specify a Bitrate: 320, 256, 192, 128, or 64$color_reset"
    return 7
  fi

  if [[ $Bitrate != "320" && $Bitrate != "256" && $Bitrate != "192" && $Bitrate != "128" && $Bitrate != "64" ]] ; then
    echo -e "${color_error}ERROR: The bitrate must be one of the following: 320, 256, 192, 128, or 64$color_reset"
    return 7
  fi

  # Repeatedly Execute the function
  while (true); do

    funkify
    sleep 15;
  done
}

# Execute the cdripper_main() function
cdripper_main $@
