Newer
Older
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2019 Osiris Alejandro Gomez <osiux@osiux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# shellcheck disable=SC2129
# shellcheck disable=SC2002
# shellcheck disable=SC1117
RED="$(tput setaf 196)"
TMP0=$(mktemp)
TMP1=$(mktemp)
TMP2=$(mktemp)
TXT='links.txt'
BAK='links.bak'
[[ -n "$HDR" ]] || HDR="$HOME/blog/header-links.org"
[[ -e "$PWD/header-links.org" ]] && HDR="$PWD/header-links.org"
BIN_BASH="$(command -v bash)"
die()
{
printf "%s\n" "${RED}[ERROR] $1 $NORMAL" && exit 1
}
stderror ()
{
echo >&2 "$1"
}
[[ -z "$1" ]] || TXT="$1"
[[ -z "$2" ]] || ORG="$2"
[[ -z "$3" ]] || HDR="$3"
for FILE in $TXT $HRD
do
[[ ! -e "$FILE" ]] && die "NOT FOUND $FILE"
[[ ! -s "$FILE" ]] && die "EMPTY $FILE"
done
BAK="$(basename "$TXT" .txt).bak"
sed -i 's/blob\/master\/README.md//gI' "$TXT"
sed -i 's/blob\/dev\/README.md//gI' "$TXT"
sort -u "$TXT" | sponge "$TXT"
awk '{print $2}' "$TXT" | sort -u > "$TMP0"
true > "$BAK"
grep -f "$TMP0" "$TXT" | sort -u | sort -k3 | while read -r D U C
do
CATEGORY=$(echo "$C" | tr ' ' '\n' | head -1)
[[ -z "$CATEGORY" ]] && CATEGORY="+unknown"
echo "$D $U $CATEGORY" >> "$BAK"
sort -u "$BAK" > "$TXT"
awk '{print $1}' "$TMP1" | sort -u > "$TMP2"
[[ -e "$ORG" ]] && true > "$ORG"
[[ -e "$HDR" ]] && cat "$HDR" >> "$ORG"
printf "\\n** Categories\\n\\n" >> "$ORG"
cat "$TMP2" \
| tr -d '+' \
| grep -Ev '^$' \
| while read -r C
do
printf "[[file:links.org::#+%s][=+%s=]] \\n" "$C" "$C"
done >> "$ORG"
printf "\\n** Recents links\\n" >> "$ORG"
tail -30 "$TXT" | sort -nr | while read -r DATE U C
do
TITLE="$(get_title "$U")"
if [[ -z "$TITLE" ]]
then
printf "*** %s [[file:links.org::#%s][=%s=]]\\n" "$U" "$C" "$C"
printf "*** [[%s][%s]] [[file:links.org::#%s][=%s=]]\\n" "$U" "$TITLE" "$C" "$C"
fi
done >> "$ORG"
printf "\\n" >> "$ORG"
cat "$TMP2" | while read -r C
do
printf "** =%s=\\n" "$C"
printf " :PROPERTIES:\\n"
printf " :CUSTOM_ID: %s\\n" "$C"
printf " :END:\\n"
REGEXP="^\+${C//+/}"
grep -E "$REGEXP" "$TMP1" | sort -u | while read -r _ U
fi
done
done >> "$ORG"
rm -f "$TMP0"
rm -f "$TMP1"
rm -f "$TMP2"