#!/usr/bin/env sh

#     ___ ___      __     __      __
#   /' __` __`\  /'__`\ /'_ `\  /'__`\
#   /\ \/\ \/\ \/\  __//\ \L\ \/\ \L\.\_
#   \ \_\ \_\ \_\ \____\ \____ \ \__/.\_\
#    \/_/\/_/\/_/\/____/\/___L\ \/__/\/_/
#                         /\____/
#                         \_/__/
#
# * MEGAcmd wrapper by ge
#   See more info about megacmd here: https://github.com/meganz/MEGAcmd
#
#   This software is not affiliated with original MEGAcmd developers!
#
# * License: The Unlicense
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>

_mega_help_wrapper() {
    cat << 'EOF'

  MEGAcmd wrapper script
    ___ ___      __     __      __
  /' __` __`\  /'__`\ /'_ `\  /'__`\
  /\ \/\ \/\ \/\  __//\ \L\ \/\ \L\.\_
  \ \_\ \_\ \_\ \____\ \____ \ \__/.\_\
   \/_/\/_/\/_/\/____/\/___L\ \/__/\/_/
                        /\____/
                        \_/__/

Usage: mega command [<options>] [<arguments>]

mega is a megacmd wrapper that allows you to run megacmd commands as 'mega command'
instead of 'mega-command'.

Wrapper commands:
    pid         print mega-cmd-server pid
    ps          show mega-cmd-server process
    tail        'tail -f $HOME/.megaCmd/megacmdserver.log'
    kill        send SIGINT to mega-cmd-server process
    lsof        lsof mega-cmd-server, pass output to PAGER if set
                [default: /usr/bin/less if exist or /usr/bin/more]
Wrapper options:
    -h, -?, --help  print this message, 'mega-help' output and exit.
    -u, --usage     print only wrapper help message amd exit.
Tips:
    mega cmd    enter to MEGA shell
    mega quit   stop MEGA server (mega-cmd-server)

@@@ This software is not affiliated with original MEGAcmd developers! @@@
MEGAcmd User Guide: https://github.com/meganz/MEGAcmd/blob/master/UserGuide.md
EOF
    if [ "$1" ]; then
        echo -e '\nSee available commands below (there is mega-help original output)'
        echo -e '\n---\n'
        mega-help
    else
        echo "Run 'mega help' or 'mega-help' to see available MEGAcmd commands."
    fi
    exit 0
}

# Select pager
if [ -z "$PAGER" ]; then
    if [ -f /usr/bin/less ]; then
        PAGER=/usr/bin/less
    elif [ -f /usr/bin/more ]; then
        PAGER=/usr/bin/more
    else
       echo 'Pager not found! Use PAGER environment variable to set pager' >&2
   fi
fi

[[ "$@" ]] || { _mega_help_wrapper 'with mega-help'; }
# Add some additional commands
case "$1" in
    -h|-\?|--help) _mega_help_wrapper 'with mega-help';;
    -u|--usage)    _mega_help_wrapper;;
    pid) pgrep mega-cmd-server; exit "$?";; # check MEGA server
    ps)  ps u -C mega-cmd-server; exit "$?";; # show MEGA server process
    tail) tail -f $HOME/.megaCmd/megacmdserver.log; exit "$?";;
    kill) killall -2 `pgrep mega-cmd-server`; exit "$?";;
    lsof) if [ "$PAGER" ]; then
              lsof -p `pgrep mega-cmd-server` | "$PAGER"
          else
              lsof -p `pgrep mega-cmd-server`
          fi
          exit "$?";;
    *) : # do nothing for another cases
esac

_command="$1"   # fetch command to execute
shift           # shift arguments to prevent running wrong commands like
                # 'mega-help help'

# Run command with arguments
"mega-${_command}" "$@"
