#!/usr/bin/env bash #set -e usage() { cat < "$ALIASES_FILE" fi load_station_info() { # loads the saved info about station # getting station if [[ ! -e "$STATION_FILE" ]] ; then # doesn't exist, create it save_station_info fi # loading info source "$STATION_FILE" } save_station_info() { # saves relevant info to file printf '%s="%s"\n' \ "STATION_ID" "$STATION_ID" \ "BIKES" "$BIKES" \ "DOCKS" "$DOCKS" \ "STATION_NAME" "$STATION_NAME" \ "EXPIRATION" "$EXPIRATION" > "$STATION_FILE" } get_station_info() { # sets $BIKES and $DOCKS based on $STATIONID # checks that $STATIONID is set if [[ -z "$STATION_ID" ]] ; then printf 'Please set a station with bb -s' >&2 exit 1 fi # adding "" id=$(printf '"%d"' $STATION_ID) STATION_INFO=$(curl --silent -fL $BB_API | gojq ".data.stations[] | select( .station_id == $id)" 2>/dev/null) if [[ -z "$STATION_INFO" ]] ; then printf 'Error retrieving station info! Check your connection' >&2 exit 1 fi # set vars BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available') DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available') # updating expiration EXPIRATION=$(($(date +%s) + $TIMEOUT)) # save the new info save_station_info } check_update() { # checks if an update is needed if [[ $(date +%s) -gt $EXPIRATION || "$UPDATE" = true ]] ; then # update info get_station_info fi } set_station_alias() { # sets $ALIAS for $STATION_ID if [[ -z "$STATION_ID" ]] ; then # no station id printf 'No Station set to change name for! Please run bb -s to set a station\n' >&2 exit 1 fi # gojq hates qoutes FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"')) FMT_STATIONID=$(printf '"%d"' $STATION_ID) echo "$(cat "$ALIASES_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS")" > "$ALIASES_FILE" # printing change if [[ -z "$ALIAS" ]] ; then printf 'Removed alias\n' else printf 'Updated alias to %s\n' "$ALIAS" fi } update_station() { # provides a gui to update the station to watch STATIONS=$(curl --silent -fL "$BB_STATION_API" | gojq '.data.stations[]') if [[ -z "$STATIONS" ]] ; then printf 'Error retrieving station info! Check your connection' >&2 exit 1 fi # prompt user to search STATION_NAME=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH") if [[ -z "$STATION_NAME" ]] ; then exit 0 fi # number correlating to selected name STATION_ID=$(echo "$STATIONS"| gojq --arg name "$STATION_NAME" '. | select( .name == $name) | .station_id' | tr -d '"') # force an update UPDATE=true # save new station info save_station_info } colorize() { # colorizes $BIKES and $DOCKS for tmux case $1 in [0-2]) clr='#[fg=color1]' # red ;; [3-6]) clr='#[fg=color184]' # yellow ;; *) clr='#[fg=color34]' # green ;; esac printf '%s%d' "$clr" $1 } print_status() { # prints check_update # check for update # check for alias id=$(printf '"%d"' $STATION_ID) ALIAS=$(cat "$ALIASES_FILE" | gojq ".$id" | tr -d '"') if [[ ! -z "$ALIAS" ]] ; then # if there is an alias STATION_NAME="$ALIAS" fi if [[ "$COLORIZE" = true ]] ; then # adding tmux colors DEFAULT="#[default]" BIKES=$(colorize $BIKES) DOCKS=$(colorize $DOCKS) fi if [[ "$PRETTY" = true ]] ; then # print with symbols printf '%s  %s  %sat %s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" else # print raw printf '%d Bikes %d Docks %sat %s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" fi } # loading info load_station_info # change long form for arg in "$@"; do shift case "$arg" in '--help') set -- "$@" "-h" ;; '--search') set -- "$@" "-s" ;; '--force') set -- "$@" "-f" ;; '--rename') set -- "$@" "-r" ;; '--colorize') set -- "$@" "-c" ;; *) set -- "$@" "$arg" ;; esac done # parsing args while getopts "hsfcrp" opt ; do case "$opt" in 'h' ) usage exit 0 ;; 's' ) update_station ;; 'f' ) UPDATE=true ;; 'c' ) COLORIZE=true ;; 'p' ) PRETTY=true ;; 'r' ) nextopt=${!OPTIND} if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then ALIAS="$nextopt" fi set_station_alias ;; '?' ) usage exit 1 ;; esac done # finish by printing print_status