# Usage:
# $ mamp
# $ mamp (apache|vhosts|hosts|php|mysql|a|v|h|p|m)
# $ mamp (error|e)
# $ mamp (error|e) (clear|apache|php|mysql|a|p|m)
# e.g. open php.ini: $ mamp php 
#                    $ mamp p 
# e.g. open apache error log: $ mamp error apache
#                             $ mamp e a
# Uses $EDITOR by default.

function mamp()
{
   local phpini defaultconfs local_editor openall textedit is_error e_msg

   # discover phpini path
   phpini=$(php -i | grep 'Load.*Config.*File.*=>' | sed s~'.* => '~''~)

   # mamp config paths
   defaultconfs="\
/Applications/MAMP/conf/my.cnf \
/Applications/MAMP/conf/apache/httpd.conf \
$phpini \
/etc/hosts \
/Applications/MAMP/conf/apache/extra/httpd-ssl.conf \
/Applications/MAMP/conf/apache/extra/httpd-vhosts.conf \
/Applications/MAMP/bin/startApache.sh \
"

   # prefer global $EDITOR
   if [ -n $EDITOR ]; then
      local_editor="$EDITOR"
   fi

   # No $EDITOR? set $local_editor:
   # local_editor=subl
   # local_editor=mate
   # local_editor=youreditorcommand

   if [ "$1" == "error" -o "$1" == "e" ]; then
      is_error='true'
   fi

   # Not an error? For config.
   # No $EDITOR? or $local_editor?
   # Prompt for input:
   if [ -z $is_error ]; then

      if [ -z "$local_editor" ]; then
         
         echo "------------------------------ Select Text Editor ------------------------------"
         echo "e.g. type 'subl' if you've configured SublimeText's command line launcher"
         echo -n "[ENTER for TextEdit]: "
         read input_e

         if [ -z "$input_e" ]; then

            local_editor='open -a TextEdit'
            
            echo ''
            echo 'To avoid this prompt you can set $EDITOR, the global shell variable.'
            echo "On the prompt run, or in .bashrc append:"
            echo "   export EDITOR='$local_editor'"
            echo ''
            echo "============================== launching TextEdit =============================="
            echo "NOTE!!! You'll want to disable smart quotes and dashes:"
            echo "   TextEdit > Preferences - uncheck Smart Quotes & Smart Dashes"
            echo "   then quit and restart TextEdit"
            echo '' 
            sleep 2.2

         else

            local_editor="$input_e"
            export EDITOR="$local_editor"

            local target_width empty_msg lep left_count right_count lepcount message_space

            # customize
            target_width=80
            empty_msg="launching"
            buffer=1

            # maths
            empty_msg_count=${#empty_msg}
            lep=$(which $input_e)
            lepcount=${#lep}
            message_space=$(( $empty_msg_count + $lepcount + ($buffer * 3) ))
            left_count=$(( ($target_width - $message_space) / 2 ))
            extra=$(( ($target_width - $message_space) - ($left_count * 2) ))
            right_count=$(( $left_count + $extra ))
            
            echo ''
            echo "Setting \$EDITOR, to '$local_editor' for this shell session."
            echo "To make your life easier you can make this permanent and append this to .bashrc:"
            echo "   export EDITOR='$local_editor'"
            echo ''

            # draw left side
            for i in `eval echo {1..$left_count}`; do
              echo -n '='
            done

            # message
            echo -n " launching $lep "

            # draw right side
            for i in `eval echo {1..$right_count}`; do
              echo -n '='
            done

            echo
            sleep 1.6
         fi
      fi
   fi

   # open all command
   if [ "$local_editor" == "subl" ]; then
      
      # A) open just defaultconfs
      #openall="subl --new-window $defaultconfs"
      
      # B) open logs folder and defaultconfs
      openall="subl /Applications/MAMP/logs/ $defaultconfs"

   elif [ "$local_editor" == "youreditorcommand" ]; then
      
      # !!! customize for your youreditorcommand
      openall="youreditorcommand --yourflagshere $defaultconfs"

   else
      # default
      openall="$local_editor $defaultconfs"

   fi

   # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   # ::: Parse Arguments
   # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   if [ -z "$1" -a -z "$2" ]; then
      # (no arugments)
      # runs open all
      $openall

   elif [ $is_error ]; then

      if [ "$2" == 'clear' ]; then
         # clear the error logs manually (requires sudo)
         echo "::: log cleared ::: `date` :::" | sudo tee /Applications/MAMP/logs/*.{log,err} >/dev/null

      elif [ $2 ]; then
         # open a specific error log
         if   [ "$2" == 'apache' -o "$2" == 'a' ]; then
            open /Applications/MAMP/logs/apache_error.log

         elif [ "$2" == 'mysql' -o "$2" == 'm' ]; then
            open /Applications/MAMP/logs/mysql_error_log.err

         elif [ "$2" == 'php'   -o "$2" == 'p' ]; then
            open /Applications/MAMP/logs/php_error.log

         fi
      else
         # (no second argument) 
         # open all error logs
         open -a console /Applications/MAMP/logs/*.{log,err}
      fi

   elif [ "$1" == 'pro' ]; then
      $local_editor '/Library/Application Support/appsolute/MAMP PRO/conf'

   elif [ "$1" == 'apache' -o "$1" == 'a' ]; then
      $local_editor /Applications/MAMP/conf/apache/httpd.conf

   elif [ "$1" == 'hosts'  -o "$1" == 'h' ]; then
      $local_editor /etc/hosts
      
   elif [ "$1" == 'mysql'  -o "$1" == 'm' ]; then
      $local_editor /Applications/MAMP/conf/my.cnf

   elif [ "$1" == 'php'    -o "$1" == 'p' ]; then
      $local_editor $phpini

   elif [ "$1" == 'ssl'    -o "$1" == 's' ]; then
      $local_editor /Applications/MAMP/conf/apache/extra/httpd-ssl.conf

   elif [ "$1" == 'vhosts' -o "$1" == 'v' ]; then
      $local_editor /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

   fi
}