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