Random scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.3 KiB

#!/bin/bash
set -e
DOMAIN="sim.bz"
SYM_LINK_DIR="$HOME/.sites"
UNAME=$(uname)
function read-link {
if [[ "$UNAME" == 'Darwin' ]]; then
echo "$(readlink "$1" -q)"
else
echo "$(readlink -f "$1")"
fi
}
#find current directory in our list of sites
function find-sym-for-current-pwd {
local PWD
local LINK
PWD=$(pwd)
for LINK in $(/bin/ls "$SYM_LINK_DIR")
do
if [[ $PWD = $(read-link "$SYM_LINK_DIR/$LINK") ]]; then
echo "$LINK"
break
fi
done
}
#if this directory is not already a site create it
#$1 - name
function check-or-make-sym {
local NAME
local LINK
LINK=$(find-sym-for-current-pwd)
if [ -z "$LINK" ]; then
NAME=${1:-$(basename "$(pwd)")}
if ! [ -e "$SYM_LINK_DIR/$NAME" ]; then
ln -s "$(pwd)" "$SYM_LINK_DIR/$NAME"
fi
echo "$NAME"
else
echo "$LINK"
fi
}
function remove-link {
local LINK
LINK=$(find-sym-for-current-pwd)
if [ -z "$LINK" ]; then
echo "Nothing to remove."
else
echo "Removed $SYM_LINK_DIR/$LINK"
rm "$SYM_LINK_DIR/$LINK"
fi
}
function with-name-only {
local NAME
NAME=$(check-or-make-sym "$1")
echo "http://$NAME.$DOMAIN"
}
#get ip url
# $1 - name
# $2 - ip
# $3 - device
function with-ip {
local LINK
LINK=$(check-or-make-sym "$1")
echo "http://$LINK.$2.$DOMAIN ($3)"
}
#use icanhazip.com to get your external ip
# $1 - name
function with-external-ip {
local IP
local LINK
IP=$(wget -qO- http://icanhazip.com)
LINK=$(check-or-make-sym "$1")
echo "http://$LINK.$IP.$DOMAIN"
}
function list-per-ip-links {
local IFCONFIG
local DEV_NAME
local IP_ADDR
IFCONFIG=$(ifconfig)
while read -r LINE; do
if [[ $LINE =~ ^([a-z0-9]+):\ flags ]]; then
DEV_NAME=${BASH_REMATCH[1]}
fi
if [[ $LINE =~ inet\ ([0-9.]+) ]]; then
IP_ADDR=${BASH_REMATCH[1]}
fi
if [[ -n $DEV_NAME && -n $IP_ADDR ]]; then
if [[ $IP_ADDR != '127.0.0.1' ]]; then
with-ip "$1" "$IP_ADDR" "$DEV_NAME"
fi
DEV_NAME=""
IP_ADDR=""
fi
done <<< "$IFCONFIG"
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-a|--all)
ALL=1
shift
;;
-i|--ip)
IP=1
shift
;;
-x|--external-ip)
EXTERNAL_IP=1
shift
;;
-r|-rm|--remove)
REMOVE=1
shift
;;
-h|--help)
echo "Usage: sim.bz [-i | -x | -a | -r ] [subdomain]"
echo " Creates sym links in $SYM_LINK_DIR for use with sim.bz dev domain"
echo " If sym link already exists it will print out the url for the"
echo " current directory."
echo " For setup see: http://goodrobot.sim.bz/posts/local-dev.html"
echo "Options:"
echo " subdomain : the string to use for linking to this directory."
echo " Defaults to current directory"
echo " -i : show link using internal ip addresses"
echo " -x : show link using external ip address"
echo " -a : show link for all addresses"
echo " -r : remove link for current directory"
exit 0
;;
*)
NAME="$1"
shift
;;
esac
done
if [[ -n $REMOVE ]]; then
remove-link
elif [[ -n $IP ]]; then
list-per-ip-links "$NAME"
elif [[ -n $EXTERNAL_IP ]]; then
with-external-ip "$NAME"
elif [[ -n $ALL ]]; then
with-name-only "$NAME"
list-per-ip-links "$NAME"
with-external-ip "$NAME"
else
with-name-only "$NAME"
fi