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