#!/bin/bash

## Description: Launch a browser with the current site
## Usage: launch [path] [-p|--phpmyadmin] [-m|--mailhog]
## Example: "ddev launch" or "ddev launch /admin/reports/status/php" or "ddev launch phpinfo.php", for PHPMyAdmin "ddev launch -p", MailHog "ddev launch -m"

FULLURL="https://awesome-typo3.test:4434"
HTTPS=""
if [ ${DDEV_PRIMARY_URL%://*} = "https" ]; then HTTPS=true; fi

while :; do
     case $1 in
         -h|-\?|--help)
             show_help
             exit
             ;;
         -p|--phpmyadmin)
            if [ "${HTTPS}" = "" ]; then
                FULLURL="${FULLURL}:${DDEV_PHPMYADMIN_PORT}"
            else
                FULLURL="${FULLURL}:${DDEV_PHPMYADMIN_HTTPS_PORT}"
            fi
             ;;
         -m|--mailhog)
            if [ "${HTTPS}" = "" ]; then
                FULLURL="${FULLURL}:${DDEV_MAILHOG_PORT}"
            else
                FULLURL="${FULLURL}:${DDEV_MAILHOG_HTTPS_PORT}"
            fi
             ;;

         --)              # End of all options.
             shift
             break
             ;;
         -?*)
             printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
             ;;
         *)               # Default case: No more options, so break out of the loop.
             break
     esac

     shift
 done

if [ -n "${1:-}" ] ; then
  if [[ ${1::1} != "/" ]] ; then
    FULLURL="${FULLURL}/";
  fi

  FULLURL="${FULLURL}${1}";
fi

case $OSTYPE in
  linux-gnu)
    xdg-open ${FULLURL}
    ;;
  "darwin"*)
    open ${FULLURL}
    ;;
  "win*"* | "msys"*)
    start ${FULLURL}
    ;;
esac
