#!/usr/bin/env bash #set -e usage() { cat < "$WORKINGDIR/data/alias.json" fi # getting station if [[ -e "$WORKINGDIR/data/.station" ]] ; then # if file exists source "$WORKINGDIR/data/.station" fi get_station_info() { # sets $BIKES and $DOCKS based on $STATIONID # checks that $STATIONID is set if [[ -z "$STATIONID" ]] ; then printf 'Please set a station with bb -s' >&2 exit 1 fi # uses $UPDATE set based on update check if [ "$UPDATE" = true ] ; then # getting new station info STATION_INFO=$(curl --silent -fL $BB_API | gojq --arg id $STATIONID '.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') EXPIRATION=$(($(date +%s) + $TIMEOUT)) # creating info file printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$WORKINGDIR/data/status/$EXPIRATION" else # grab existing data source $WORKINGDIR/data/status/* fi } check_update() { # sets $UPDATE if needed file_expr=$(ls "$WORKINGDIR/data/status" | grep .json | awk -F . '{print $1}') if [[ $(date +%s) -gt $file_expr ]] ; then # out of date UPDATE=true # cleaning old info rm ${WORKINGDIR}/data/status/*.json 2> /dev/null fi } set_station_alias() { # sets $ALIAS for $STATION_ID if [[ -z "$STATIONID" ]] ; then # no station id printf 'No Station set to change name for!\nPlease run bb -s to set a station' >&2 exit 1 fi #aliases=$(cat "$WORKINGDIR/data/alias.json") # alias json ALIAS_FILE="$WORKINGDIR/data/alias.json" # fmtting FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"')) FMT_STATIONID=$(printf '"%d"' $STATIONID) OUT=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS") echo "$OUT" > "$ALIAS_FILE" # printing change if [[ -z "$ALIAS" ]] ; then printf 'Removing 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 https://gbfs.bluebikes.com/gbfs/en/station_information.json | gojq '.data.stations[]') if [[ -z "$STATIONS" ]] ; then printf 'Error retrieving station info! Check your connection' >&2 exit 1 fi # prompt user to search NEW_STATION=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH") if [[ -z "$NEW_STATION" ]] ; then exit 0 fi # number correlating to selected name STATIONID=$(echo "$STATIONS"| gojq --arg name "$NEW_STATION" '. | select( .name == $name) | .station_id' | tr -d '"') # trim quotes # setting data/.station file output=$(printf 'STATIONID=%s\nSTATION_NAME="%s"\n' "$STATIONID" "$NEW_STATION") echo "$output" > "$WORKINGDIR/data/.station" } 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 get_station_info # sets $DOCKS $BIKES and $STATIONID if [[ -e "$WORKINGDIR/data/alias.json" ]] ; then FMT_STATIONID=$(printf '"%d"' $STATIONID) ALIAS=$(cat "$WORKINGDIR/data/alias.json" | gojq ".$FMT_STATIONID" | tr -d '"') if [[ ! -z "$ALIAS" ]] ; then STATION_NAME="$ALIAS" fi fi DEFAULT="" if [[ "$COLORIZE" = true ]] ; then # adding tmux colors DEFAULT="#[default]" BIKES=$(colorize $BIKES) DOCKS=$(colorize $DOCKS) fi printf '%s  %s  %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" } # always check/clean status check_update # 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 "hsfcr" opt ; do case "$opt" in 'h' ) usage exit 0 ;; 's' ) update_station ;; 'f' ) UPDATE=true ;; 'c' ) COLORIZE=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