#!/usr/bin/env bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ ! -f "$DIR/valet.php" ]
then
    OLDPWD="$PWD"
    cd "$DIR/../laravel/valet"
    DIR="$(pwd)"
    cd "$OLDPWD"
fi

if [[ "$1" = "install" ]] || [[ "$1" = "domain" ]] || [[ "$1" = "start" ]] || [[ "$1" = "restart" ]] || [[ "$1" = "stop" ]] || [[ "$1" = "uninstall" ]]
then
    sudo php "$DIR/valet.php" "$@"
elif [[ "$1" = "share" ]]
then
    HOST="${PWD##*/}"
    DOMAIN=$(php "$DIR/valet.php" current-domain)


    for linkname in ~/.valet/Sites/*; do
        RESOLVED_LINK="$(readlink $linkname)"

        if [[ "$RESOLVED_LINK" = "$PWD" ]]
        then
            HOST="${linkname##*/}"
        fi
    done

    # Fetch Ngrok URL In Background...
    bash "$DIR/fetch-share-url.sh" &

    "$DIR/bin/ngrok" http -host-header=rewrite "$HOST.$DOMAIN:80"
else
    php "$DIR/valet.php" $@
fi
