406 lines
11 KiB
Bash
Executable File
406 lines
11 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
VERSION="v1.1.4"
|
|
|
|
### Variables for self updating
|
|
ScriptArgs=( "$@" )
|
|
ScriptPath="$(readlink -f "$0")" # /Users/bruno/Documents/Scripts/bashbirds/bashbirds.sh
|
|
ScriptWorkDir="$(dirname "$ScriptPath")" # /Users/bruno/Documents/Scripts/bashbirds
|
|
|
|
dotenv () {
|
|
set -a
|
|
# shellcheck disable=SC1091
|
|
[ -f "$ScriptWorkDir/.env" ] && . "$ScriptWorkDir/.env" || echo -e "${red}\nNo .env file found ! Could'nt get update from Github.'.${reset}"
|
|
set +a
|
|
}
|
|
|
|
dotenv
|
|
|
|
### ChangeNotes: 1.1.4 curl 2ko
|
|
### ChangeNotes: 1.1.3 Won't display Changes notes
|
|
### ChangeNotes: 1.1.1 Bugfix .env path
|
|
### ChangeNotes: 1.1 Bugfix when script was run with symlink
|
|
### ChangeNotes: 1.0 Minor corrections.
|
|
|
|
Github="https://github.com/bruno21/bashbirds"
|
|
# Public Repo:
|
|
#RawUrl="https://raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh"
|
|
# Private Repo:
|
|
RawUrl="https://x-access-token:$GITHUB_TOKEN@raw.githubusercontent.com/Bruno21/bashbirds/main/bashbirds.sh"
|
|
|
|
### 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-2000 $RawUrl | sed -n "/ChangeNotes/s/# ChangeNotes: //p")"
|
|
LatestChanges="$(curl -s -r 0-2000 $RawUrl | grep "^### ChangeNotes:" | sed 's/### ChangeNotes://g')"
|
|
|
|
#[[ $LatestRelease != $VERSION ]] && new="($LatestRelease)" || new=""
|
|
|
|
#echo "$LatestRelease"
|
|
#echo "$LatestChanges"
|
|
|
|
red="\033[1;31m"
|
|
greenbold="\033[1;32m"
|
|
green="\033[0;32m"
|
|
yellow="\033[0;33m"
|
|
yellowbold="\033[1;33m"
|
|
bold="\033[1m"
|
|
italic="\033[3m"
|
|
#bold_under="\033[1;4m"
|
|
underline="\033[4m"
|
|
reset="\033[0m"
|
|
|
|
|
|
echo -e "${yellowbold}Bashbird${reset} $VERSION\n"
|
|
|
|
|
|
### Help Function:
|
|
Help() {
|
|
echo "Syntax: birds.sh [OPTION]"
|
|
echo "Example: birds.sh -b buse"
|
|
echo
|
|
echo "Options:"
|
|
echo "-b -b <bird>, -b all."
|
|
echo "-h Print this Help."
|
|
echo "-m Export markdown."
|
|
echo "-v Prints current version."
|
|
echo "-w Export html."
|
|
}
|
|
|
|
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 Francais, Autres, Latin, Anglais, Ordre, Famille, Liens FROM liste;"
|
|
result2=$(sqlite3 "$ScriptWorkDir/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"
|
|
|
|
birds+="| $fr | $z |"'\n'
|
|
birds+="|------------------------ | --------------------------|"'\n'
|
|
birds+="| Vernaculaire | $lat |"'\n'
|
|
birds+="| Anglais | $en |"'\n'
|
|
birds+="| Ordre | $or |"'\n'
|
|
birds+="| Famille | $fa |"'\n'
|
|
#birds+="| Liens (oiseaux.net)<br /> | [$fr]($ln)<br /> |"'\n'
|
|
birds+="| Liens (oiseaux.net)<br /> | [$fr]($ln)<br /> |"'\n'
|
|
birds+='\n'
|
|
|
|
elif [ $ext = ".html" ]; then
|
|
|
|
firstletter="${fr:0:1}"
|
|
|
|
if [ $firstletter != $old_index ]; then
|
|
index+=("$firstletter")
|
|
#bird+="<tr class='bird'><td class='bold'><a href='#$firstletter'></a>$fr</td><td>$aut</td><td>$lat</td><td>$en</td></tr>"
|
|
bird+="<tr class='bird'><td class='bold'><a id='$firstletter'></a>$fr</td><td>$aut</td><td>$lat</td><td>$en</td></tr>"
|
|
else
|
|
bird+="<tr class='bird'><td class='bold'>$fr</td><td>$aut</td><td>$lat</td><td>$en</td></tr>"
|
|
fi
|
|
bird+="<tr class='family'><td>$or</td><td>$fa</td><td></td><td><a href='$ln'>Oiseaux.net</a></tr>"
|
|
if [ $firstletter != $old_index ]; then
|
|
bird+="<tr class='family textcenter'><td colspan='4'>PLACEHOLDER</td></tr>"
|
|
else
|
|
bird+="<tr class='family'><td colspan='4'></td></tr>"
|
|
fi
|
|
old_index="$firstletter"
|
|
|
|
fi
|
|
|
|
done <<< "$result2"
|
|
|
|
if [ $ext = ".html" ]; then
|
|
newArr=(); while IFS= read -r -d '' x; do newArr+=("$x"); done < <(printf "%s\0" "${index[@]}" | sort -uz)
|
|
|
|
for val in ${!newArr[@]}
|
|
do
|
|
liens_index+="<a href='#${newArr[$val]}'>${newArr[$val]}</a> | "
|
|
done
|
|
liens_index="${liens_index:0:-3}"
|
|
z=${bird//PLACEHOLDER/$liens_index}
|
|
bird="$z"
|
|
|
|
html
|
|
elif [ $ext = ".md" ]; then
|
|
|
|
echo -e "${yellow}Exporting markdown file ${italic}$ScriptWorkDir/liste_oiseaux$ext !${reset}"
|
|
bird=$(echo "$birds" | sed 's/\
|
|
//g')
|
|
echo -e "$bird" > "liste_oiseaux$ext"
|
|
fi
|
|
|
|
exit 0
|
|
}
|
|
|
|
html() {
|
|
|
|
echo -e "${yellow}Exporting html file ${italic}$ScriptWorkDir/liste_oiseaux.html !${reset}"
|
|
|
|
cat > liste_oiseaux.html << EOF
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta charset="utf-8">
|
|
<title>Liste oiseaux d'Europe...</title>
|
|
<meta name="description" content="">
|
|
<meta name="author" content="">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<style>
|
|
body {
|
|
font-family: Helvetica, Calibri, Arial, sans-serif;
|
|
background: #e0e5b6;
|
|
#font-weight: 300;
|
|
#font-size: 15px;
|
|
#color: #333;
|
|
}
|
|
table {
|
|
width: 100%;
|
|
}
|
|
table, th, td {
|
|
border: 1px solid black;
|
|
border-collapse: collapse;
|
|
padding: 10px;
|
|
}
|
|
caption {
|
|
letter-spacing: 3px;
|
|
font-weight: 600;
|
|
font-size: 28px;
|
|
padding: 16px;
|
|
}
|
|
h2 {
|
|
letter-spacing: 3px;
|
|
}
|
|
a {
|
|
color: #898121;
|
|
}
|
|
a:hover {
|
|
color: #914f1e;
|
|
}
|
|
|
|
.bird {
|
|
background-color: #ccd5ae;
|
|
}
|
|
.link {
|
|
background-color: #e0e5b6;
|
|
}
|
|
.family {
|
|
background-color: #faedce;
|
|
}
|
|
.bold {
|
|
font-weight: bold;
|
|
}
|
|
.textcenter {
|
|
text-align: center;
|
|
}
|
|
.index {
|
|
width: 550px;
|
|
margin: auto;
|
|
}
|
|
.td75 {
|
|
width: 75%;
|
|
background-color: #bbb;
|
|
}
|
|
.td25 {
|
|
width: 25%;
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="">
|
|
<!--[if lt IE 9]>
|
|
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script>
|
|
<script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
|
|
<![endif]-->
|
|
<link rel="shortcut icon" href="">
|
|
</head>
|
|
<body>
|
|
|
|
|
|
<br /><br />
|
|
<table id="anchor-cask">
|
|
<caption>Liste des oiseaux d'Europe.</caption>
|
|
<div class='index'>$liens_index</div>
|
|
$bird
|
|
</table>
|
|
|
|
</body>
|
|
</html>
|
|
EOF
|
|
|
|
}
|
|
|
|
req1() {
|
|
echo -e "${bold}Recherche: <$1>${reset}"
|
|
|
|
request="$1"
|
|
if [[ "${request,,}" == "all" ]]; then
|
|
request="";
|
|
fi
|
|
|
|
request=$(echo "$request" | sed 'y/áàâäçéèêëîïìôöóùúüñÂÀÄÇÉÈÊËÎÏÔÖÙÜÑ/aaaaceeeeiiiooouuunAAACEEEEIIOOUUN/')
|
|
|
|
query1="SELECT * FROM liste WHERE Fra LIKE \"%$request%\" OR Aut LIKE \"%$request%\" OR Latin LIKE \"%$request%\"";
|
|
result1=$(sqlite3 "$ScriptWorkDir/birds.db" "$query1")
|
|
|
|
if [ -n "$result1" ]; then
|
|
array2=()
|
|
keywords=()
|
|
cmpt=1
|
|
while IFS='|' read -ra array2;
|
|
do
|
|
fr2="${array2[0]}"
|
|
aut2="${array2[2]}"
|
|
lat2="${array2[4]}"
|
|
en2="${array2[5]}"
|
|
or2="${array2[6]}"
|
|
fa2="${array2[7]}"
|
|
lnk2="${array2[8]}"
|
|
|
|
tag2="$fr2"
|
|
if [ -n "$aut2" ]; then
|
|
tag2+=",$aut2"
|
|
fi
|
|
tag2+=",$lat2,$en2"
|
|
if [ -n "$or2" ]; then
|
|
tag2+=",$or2"
|
|
fi
|
|
if [ -n "$fa2" ]; then
|
|
tag2+=",$fa2"
|
|
fi
|
|
#echo "${#fr2} ; ${#lat2} ; ${#en2} ; ${#or2} ; ${#fa2} ; ${#lnk2} ; ${#aut2} "
|
|
|
|
echo
|
|
printf "\e[1m| %-3s | %-25s | %-20s | %-20s | %-18s | %-15s | %-20s \e[0m\n" "$cmpt" "$fr2" "$lat2" "$en2" "$or2" "$fa2" "$aut2"
|
|
printf "| %-3s | %-55s \n" "$cmpt" "$lnk2"
|
|
printf "\e[0;34m| %-3s | %-55s \e[0m\n" "$cmpt" "$tag2"
|
|
keywords+=("$tag2")
|
|
cmpt=$((cmpt+1))
|
|
done <<< "$result1"
|
|
|
|
choose=$(echo -e "\nChoose a number to get keywords in your clipboard (<q> 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[@]}" >/dev/null # 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 \nChange Notes:\n%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
|
|
|
|
### Database is present or not ?
|
|
if [ ! -f "$ScriptWorkDir/birds.db" ]; then
|
|
echo -e "${red}No database found !${reset}"
|
|
exit 1
|
|
|
|
else
|
|
query="SELECT COUNT(Francais) FROM liste";
|
|
result5=$(sqlite3 "$ScriptWorkDir/birds.db" "$query")
|
|
|
|
if [ -n "$result5" ]; then
|
|
echo -e "\n${bold}A birds database that return keywords for Lightroom.${reset}"
|
|
echo -e "$result5 birds founds in database...\n"
|
|
Help
|
|
fi
|
|
fi
|
|
|
|
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))"
|