#!/usr/bin/env bash VERSION="v0.8" red="\033[1;31m" greenbold="\033[1;32m" green="\033[0;32m" yellow="\033[0;33m" yellowbold="\033[1;33m" bold="\033[1m" #bold_under="\033[1;4m" underline="\033[4m" reset="\033[0m" dotenv () { set -a # shellcheck disable=SC1091 [ -f ".env" ] && . ".env" || echo -e "${red}\nNo .env file found ! Could'nt get update from Github.'.${reset}" set +a } dotenv ### ChangeNotes: Minor corrections. Github="https://github.com/bruno21/bashbirds" # Repo normal: #RawUrl="https://raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh" # Repo privé: RawUrl="https://x-access-token:$GITHUB_TOKEN@raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh" ### Variables for self updating ScriptArgs=( "$@" ) ScriptPath="$(readlink -f "$0")" # /Users/bruno/Documents/Scripts/bashbirds/bashbirds.sh ScriptWorkDir="$(dirname "$ScriptPath")" # /Users/bruno/Documents/Scripts/bashbirds ### Check if there's a new release of the script: LatestRelease="$(curl -s -r 0-50 $RawUrl | sed -n "/VERSION/s/VERSION=//p" | tr -d '"')" LatestChanges="$(curl -s -r 0-200 $RawUrl | sed -n "/ChangeNotes/s/### ChangeNotes: //p")" #[[ $LatestRelease != $VERSION ]] && new="($LatestRelease)" || new="" echo -e "${yellowbold}Bashbird${reset} $VERSION\n" req2() { option=$1 case $option in m) ext=".md" ;; w) ext=".html";old_index="_" ;; esac f="liste_oiseaux$ext" if [ -f "./$f" ]; then rm "./$f" fi #read -p "Pause: " choice query2="SELECT * FROM liste;" result2=$(sqlite3 ./birds.db "$query2") #echo "$result2" i=1 index=() array=() while IFS='|' read -ra array; do fr="${array[0]}" aut="${array[1]}" lat="${array[2]}" en="${array[3]}" or="${array[4]}" fa="${array[5]}" ln="${array[6]}" [[ $aut != "" ]] && z="($aut)" || z="" if [ $ext = ".md" ]; then #echo "| $fr $z |" >>"$f" #echo "|--------------------------------------------------|" >>"$f" #echo "| $lat |" >> "$f" #echo "| $en |" >> "$f" #echo "| $or |" >> "$f" #echo "| $fa |" >> "$f" #echo "| $ln |" >> "$f" #echo "" >> "$f" bird+="| $fr $z |\n" bird+="|--------------------------------------------------|\n" bird+="| $lat |\n" bird+="| $en |\n" bird+="| $or |\n" bird+="| $fa |\n" bird+="| $ln |\n" bird+=" \n" elif [ $ext = ".html" ]; then firstletter="${fr:0:1}" if [ $firstletter != $old_index ]; then index+=("$firstletter") #bird+="
to quit): ") read -e -p "$choose" choice re='^[0-9]+$' if [[ $choice == "q" ]] || [[ $choice == "Q" ]]; then exit 0 elif ! [[ $choice =~ $re ]] ; then echo -e "${red}Wrong index !${reset}" else if [ "$choice" -ge 1 ] && [ "$choice" -le "$((cmpt-1))" ]; then if [[ "$OSTYPE" == "linux-gnu" ]] && [ -x "$(command -v xsel)" ]; then xsel -b <<< "${keywords[$((choice-1))]}" elif [[ "$OSTYPE" == "darwin"* ]] && [ -x "$(command -v pbcopy)" ]; then pbcopy <<< "${keywords[$((choice-1))]}" fi else echo -e "${red}Wrong index !${reset}" fi fi else echo -e "\n ${red}No results found!" fi } self_update_curl() { cp "$ScriptPath" "$ScriptPath".bak if [[ $(builtin type -P curl) ]]; then curl -L $RawUrl > "$ScriptPath" ; chmod +x "$ScriptPath" printf "\n%s\n" "--- starting over with the updated version ---" exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments exit 1 # exit the old instance elif [[ $(builtin type -P wget) ]]; then wget $RawUrl -O "$ScriptPath" ; chmod +x "$ScriptPath" printf "\n%s\n" "--- starting over with the updated version ---" exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments exit 1 # exit the old instance else printf "curl/wget not available - download the update manually: %s \n" "$Github" fi } self_update() { cd "$ScriptWorkDir" || { printf "Path error, skipping update.\n" ; return ; } if [[ $(builtin type -P git) ]] && [[ "$(git ls-remote --get-url 2>/dev/null)" =~ .*"mag37/dockcheck".* ]] ; then printf "\n%s\n" "Pulling the latest version." git pull --force || { printf "Git error, manually pull/clone.\n" ; return ; } printf "\n%s\n" "--- starting over with the updated version ---" cd - || { printf "Path error.\n" ; return ; } exec "$ScriptPath" "${ScriptArgs[@]}" # run the new script with old arguments exit 1 # exit the old instance else cd - || { printf "Path error.\n" ; return ; } self_update_curl fi } ### Version check & initiate self update if [[ "$VERSION" != "$LatestRelease" ]] ; then printf "New version available! %b%s%b ⇒ %b%s%b \n Change Notes: %s \n" "$c_yellow" "$VERSION" "$c_reset" "$c_green" "$LatestRelease" "$c_reset" "$LatestChanges" if [[ -z "$AutoUp" ]] ; then read -r -p "Would you like to update? y/[n]: " SelfUpdate [[ "$SelfUpdate" =~ [yY] ]] && self_update fi fi if [ ! -f ./birds.db ]; then echo -e "${red}No database found !${reset}" exit 1 fi ### Help Function: Help() { echo "Syntax: birds.sh [OPTION]" echo "Example: birds.sh -b buse" echo echo "Options:" echo "-b -b, -b all." echo "-h Print this Help." echo "-m Export markdown." echo "-v Prints current version." echo "-w Export html." } while getopts "b:hmw" options; do case "${options}" in b) req1 "${OPTARG}" ;; m|w) req2 "${options}" ;; v) printf "%s\n" "$VERSION" ; exit 0 ;; h|*) Help ; exit 2 ;; esac done shift "$((OPTIND-1))"