Update lace
[lace] / lacehowlong
1 #!/usr/bin/env bash
2 #Forked from https://gitlab.com/uoou/dotfiles/-/tree/master/stow/bin/home/drew/.local/bin/lace 
3 # Modified to work on Debian Distros running Sed 3.7
4 # https://github.com/frrobert2/lace/blob/main/lacehowlong
5
6 if [ -z "$XDG_CONFIG_HOME" ];then
7         config_place="$HOME/.config/lace"
8 else
9         config_place="$XDG_CONFIG_HOME/lace"
10 fi
11 [[ ! -d "$config_place" ]] && mkdir -p $config_place
12 subsfile="$config_place/subs"
13
14 red="[31m"
15 blue="[34m"
16 nc="[m"
17 esc=$(echo -e "\033")
18 tmpfile=$(mktemp /tmp/lace.XXXXXX)
19 tmpfile2=$(mktemp /tmp/lace.XXXXXX)
20 function reldate() {
21 # stolen from https://unix.stackexchange.com/a/451216
22     local SEC_PER_MINUTE=$((60))
23     local   SEC_PER_HOUR=$((60*60))
24     local    SEC_PER_DAY=$((60*60*24))
25     local  SEC_PER_MONTH=$((60*60*24*30))
26     local   SEC_PER_YEAR=$((60*60*24*365))
27
28     local last_unix="$(date --date="@$1" +%s)"    # convert date to unix timestamp
29         local now_unix="$2"
30     local delta_s=$(( now_unix - last_unix ))
31
32     if (( delta_s <  SEC_PER_MINUTE * 2))
33     then
34         echo ""$((delta_s))" seconds ago"
35     elif (( delta_s <  SEC_PER_HOUR * 2))
36     then
37         echo ""$((delta_s / SEC_PER_MINUTE))" minutes ago"
38     elif (( delta_s <  SEC_PER_DAY * 2))
39     then
40         echo ""$((delta_s / SEC_PER_HOUR))" hours ago"
41     elif (( delta_s <  SEC_PER_MONTH * 2))
42     then
43         echo ""$((delta_s / SEC_PER_DAY))" days ago"
44     elif (( delta_s <  SEC_PER_YEAR * 2))
45     then
46         echo ""$((delta_s / SEC_PER_MONTH))" months ago"
47     else
48         echo ""$((delta_s / SEC_PER_YEAR))" years ago"
49     fi
50 }
51 #export -f reldate
52
53 function dohelp() {
54         echo -e "\nUsage:\n"
55         echo -e "\t$(basename "$0") - read subscriptions\n"
56         echo -e "\t$(basename "$0") nope - read subscriptions without pager\n"
57         echo -e "\t$(basename "$0") subs - list subscriptions\n"
58         echo -e "\t$(basename "$0") sub nickname url - add subscription, e.g.:"
59         echo -e "\t$(basename "$0") sub Drew gemini://friendo.monster/tiny.gmi\n"
60         echo -e "\t$(basename "$0") unsub nickname - remove subscription, e.g.:"
61         echo -e "\t$(basename "$0") unsub Drew\n"
62         echo -e "\t$(basename "$0") update - redownload script to location of current script (might need sudo, depending on location)\n"
63         exit
64 }
65
66 function timeline() {
67         while read f;do
68                 local user=$(awk '{print $2}' <<< "$f")
69                 local url=$(awk '{print $1}' <<< "$f")
70                 if [[ "$url" != *://* ]];then
71                         domain="$url"
72                         url="gemini://$url"
73                 else
74                                 domain=$(sed 's/gemini:\/\///' <<< "$url")
75                         fi
76                         if [[ "$url" != *.*/*.* ]] && [[ $url != */ ]];then
77                                 url="$url/"
78                         fi
79                         domain=$(sed 's/\/.*$//' <<< "$domain")
80                         fav=$(openssl s_client -crlf -quiet -connect "$domain:1965" <<<"gemini://$domain/favicon.txt" 2>/dev/null | sed -e '1d')
81                         if [ ! -z "$fav" ];then
82                                 user="$fav $user"
83                         fi
84                         openssl s_client -crlf -quiet -connect "$domain:1965" <<<"$url" 2>/dev/null | sed -e '1d' | sed -n '/##/,$p' | sed -e "s/^\(## .*\)$/\1\n${esc}$red$user${esc}$nc ($domain)/g" >> $tmpfile
85                         [[ ! -z $(sed -n '$p' $tmpfile) ]] && echo "" >> $tmpfile
86                 done <"$subsfile"
87                 cat $tmpfile | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sed -rn '/^## .*/p' | sed -e 's/\v/\n/g' | sed -e "s/^## \(.*\)/date -d '\1' +%s/e" | sed -r ':r;/(^|\n)$/!{$!{N;br}};s/\n/\v/g' | sort -r | sed -e 's/\v/\n/g'  > $tmpfile
88
89
90         # removed segment of code       | sed "s/^\([0-9]\+\)$/{reldate \1 $now_unix;}/e" | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g"
91
92         while read line; do
93         if [[ "$line" =~ ^[0-9]{10,}$ ]]; then
94                 reldate "$line" "$(date +%s)" >> $tmpfile2
95         else
96            echo "$line" >> $tmpfile2
97         fi
98         done<$tmpfile
99
100         cat $tmpfile2 | sed "s/^\([0-9]* [a-z]* ago\)$/${esc}$blue\1${esc}$nc/g" >$tmpfile
101
102
103
104                 if [[ "$1" == "nope" ]];then
105                         cat $tmpfile && rm -f $tmpfile $tmpfile2
106                 else
107                         less -RisW $tmpfile && rm -f $tmpfile $tmpfile2
108                 fi
109         }
110
111         if [[ "$1" == "help" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-help" ]] || [[ "$1" == "-h" ]];then
112                 dohelp
113         elif [[ "$1" == "sub" ]];then
114                 [[ "$#" -ne 3 ]] && dohelp
115                 echo "$3 $2" >> $subsfile && echo "added $2 to subscriptions"
116 elif [[ "$1" == "unsub" ]];then
117         [[ "$#" -ne 2 ]] && dohelp
118         if grep " $2$" $subsfile 1>/dev/null;then
119                 sed -i "/ $2$/d" $subsfile &&
120                 echo "removed subscription to $2"
121         else
122                 echo "subscription \"$2\" does not exist"
123         fi
124 elif [[ "$1" == "subs" ]];then
125         awk '{print $2" ("$1")"}' $subsfile 
126 elif [[ "$1" == "update" ]];then
127         curl https://raw.githubusercontent.com/frrobert2/lace/main/lacehowlong > "${BASH_SOURCE[0]}"
128 else
129         now_unix="$(date +'%s')"
130         if [[ "$1" == "nope" ]];then
131                 timeline nope
132         else
133                 timeline
134         fi
135 fi