From e9db9113f538803873571464c84dce5586f9afe1 Mon Sep 17 00:00:00 2001 From: Miquel Lionel Date: Wed, 18 Nov 2020 23:59:16 +0100 Subject: Various modifications of the script. --- randopitons.7.gz | Bin 466 -> 800 bytes randopitons.sh | 117 +++++++++++++++++++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/randopitons.7.gz b/randopitons.7.gz index 7b6feff..c585aeb 100644 Binary files a/randopitons.7.gz and b/randopitons.7.gz differ diff --git a/randopitons.sh b/randopitons.sh index 3cba118..2863c9b 100644 --- a/randopitons.sh +++ b/randopitons.sh @@ -1,42 +1,72 @@ #!/usr/bin/env bash -# Bash3 Boilerplate. Copyright (c) 2014, kvz.io #set -o errexit #set -o pipefail #set -o nounset #set -o xtrace +BOLD='\033[01m' +UNDL='\033[04m' +GREEN='\033[32m' +RED='\033[31m' +STYLE_END='\033[0m' # Set magic variables for current file & dir -__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" -__base="$(basename ${__file} .sh)" -__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app -REGIONS=$(cat ${__dir}/regions.txt) -REGIONFILE="${__dir}/regions.txt" +DATADIR="$HOME/.local/share/randopitons" +COOKIES_FILE="$DATADIR/cookies.txt" +REGIONFILE="$DATADIR/regions.txt" +S256_REGIONFILE="20eb46831f436bb467b850835c7efde1d51742434fcea9addf4754b9545aae47" +MATCHEDFILE="$(mktemp)" MAPTYPE="gpx" RDPUSER= RDPUSERPASS= LOGINOK= + #Filechecks -printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE +mkdir -p $DATADIR +if [ ! -w $REGIONFILE ]; then + printf "\nRegenerating $REGIONFILE...\n" + printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE +fi + +if [ "$S256_REGIONFILE" != "$(sha256sum $REGIONFILE | cut -d' ' -f1)" ]; then + printf "\n$REGIONFILE sha256sum not matching, regenerating...\n" + printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE +fi #https://stackoverflow.com/questions/4813092/how-to-read-entire-line-from-bash #so we don't use a while loop IFS=$'\n' -_download() +_check_connectivity() { + ping -c 1 -q -W 3 example.com > /dev/null 2>&1 || (printf "No internet connectivity. Check your network settings and status.\n" && return 1) +} +_download() +{ + _check_connectivity || exit 1 while read linefromfile; do - mkdir -p ${__dir}/randopfiles/"${linefromfile}" - cd ${__dir}/randopfiles/"${linefromfile}" + if [ ! -s $COOKIES_FILE ]; then + printf "\nCookie file not detected. Re-login using the -u switch to regenerate it.\n" + exit 1 + fi + + DOWNLOAD_DIR=${__dir}/randopfiles/"${linefromfile}"/${MAPTYPE} + printf "Trace files for the ${WEBREGION} with extension '${MAPTYPE}' will be downloaded to ${BOLD}$DOWNLOAD_DIR${STYLE_END}" + + mkdir -p $DOWNLOAD_DIR + cd $DOWNLOAD_DIR WEBREGION=$(echo ${linefromfile} | sed 's/ de /\-/g' | tr '[:upper:]' '[:lower:]') - FILENBRS=$(wget -qO- https://randopitons.re/randonnees/region/${WEBREGION} | grep " $MATCHEDFILE + - if [ -s $MATCHEDFILE ];then - echo "Nothing matched or you entered an empty value." - _help + if ! grep -iE "$1" $REGIONFILE > $MATCHEDFILE; then + printf "No region matched or you entered an empty value.\n" + exit 1 fi - - printf "Your region choice was '$1'\nMatches are:\n" - cat $MATCHEDFILE - _download $MATCHEDFILE + + printf "Argument '$1' matched. Proceeding with download...\n" + if [ "$1" != 'All' ]; then + _download $MATCHEDFILE + else + _download $REGIONFILE + fi ;; -a | --all ) printf "\nThis will download all the hitchiking routes from all regions.\nIf no extension is specified (with -mp or --maptype), it will - default to gpx.\nPausing for 5 seconds before downloading..." - sleep 5 + default to gpx." _download $REGIONFILE ;; - -em | --elevator-music ) - echo "Initiating elevator music" - mpv --no-audio --really-quiet https://www.youtube.com/watch?v=KfNXGY9O5VY || printf "\nmpv is not installed." - ;; - -h | --help ) _help ;; - - * ) - _help + * ) + _help + ;; esac shift done -- cgit v1.2.3-70-g09d2