Made POSIX and tidied
authorCian Bagshaw <cian@cianb.xyz>
Wed, 17 May 2023 15:41:21 +0000 (16:41 +0100)
committerCian Bagshaw <cian@cianb.xyz>
Wed, 17 May 2023 15:41:21 +0000 (16:41 +0100)
buseir

diff --git a/buseir b/buseir
index 972230c..3558d60 100755 (executable)
--- a/buseir
+++ b/buseir
@@ -5,47 +5,48 @@
 SITE=https://buseireann.ie
 CACHE=$XDG_CACHE_HOME/buseir
 TMP=/tmp/buseir
-MENU="fzf --reverse --height=12"
+
+menu() { fzf --reverse --height=12; }
+
+readCsv() {
+       if [ "$2" = "all" ];
+               then cat "$1"; else grep "$2" "$1"
+       fi | cut -d, -f"$3" | tr -d \"
+}
 
 sync() {
        echo Syncing data...
-       [ ! -d $TMP ] && mkdir -p $TMP
-       rm -f $TMP/*
-       wget -qP $TMP "$SITE" "$SITE/inc/proto/bus_stop_points.php" \
-               || (echo Connection failed! && exit);
+       [ ! -d "$TMP" ] && mkdir -p "$TMP"
+       rm -f "$TMP"/*
+       wget -qP "$TMP" "$SITE" "$SITE"/inc/proto/bus_stop_points.php \
+               || (echo "Connection failed!" && exit);
        xmllint --html --xpath \
-               "//select[@id='form-fare-finder-service']/option/text()" $TMP/index.html 2>/dev/null \
-               | sed 's/\s*:\s*/,/; /Select Service/d' > $TMP/routes.csv
-       for i in $(cut -d, -f1 $TMP/routes.csv); \
-               do echo "$SITE/inc/proto/fareDestinations.php?fare=$i"; done \
+               "//select[@id='form-fare-finder-service']/option/text()" "$TMP"/index.html 2>/dev/null \
+               | sed 's/\s*:\s*/,/; /Select Service/d' > "$TMP"/routes.csv
+       readCsv "$TMP"/routes.csv all 1 | while read -r bus
+               do echo "$SITE"/inc/proto/fareDestinations.php?fare="$bus"; done \
                | wget -q -i - -O - | jq -r '[.fareDestinations[]|.name]|@csv' \
-               | paste -d, $TMP/routes.csv - > $CACHE/routes.csv
-       grep -o "{.*}" $TMP/bus_stop_points.php \
-               | jq -r '.bus_stops[]|[.name,.num,.duid,.lat,.lng]|@csv' > $CACHE/stops.csv
-}
-
-csv() {
-       if [ "$2" == "all" ];
-               then cat "$1"; else grep "$2" "$1"
-       fi | cut -d, -f"$3" | tr -d \"
+               | paste -d, "$TMP"/routes.csv - > "$CACHE"/routes.csv
+       grep -o "{.*}" "$TMP"/bus_stop_points.php \
+               | jq -r '.bus_stops[]|[.name,.num,.duid,.lat,.lng]|@csv' > "$CACHE"/stops.csv
 }
 
 stops() {
-       case $2 in
+       case "$2" in
                name) COL=1;;   num) COL=2;;    duid) COL=3;;
                pos) COL=4-5;;  all) COL=1-;;   *) COL=1;;
        esac 
-       csv $CACHE/stops.csv "$1" "$COL" 
+       readCsv "$CACHE"/stops.csv "$1" "$COL" 
 }
 
 printStop() {
-       tr "," "\n" | sed -e '1s/^/Name:\t/; 2s/^/Number:\t/; 3s/^/DUID:\t/' \
+       sed -e '1s/^/Name:\t/; 2s/^/Number:\t/; 3s/^/DUID:\t/' \
                -e '4{N; s/\n/,/; s/^/Map:\thttps:\/\/www.google.com\/maps\/place\//}'  
 }
 
-[ ! -d $CACHE ] && mkdir -p "$CACHE" && sync
+[ ! -d "$CACHE" ] && mkdir -p "$CACHE" && sync
 
-case $1 in
+case "$1" in
        sync) sync;;
-       stops) stops "\"$(stops all | $MENU)"\" all | printStop;;
+       stops) stops "\"$(stops all | menu)\"" all | tr "," "\n" | printStop;;
 esac