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