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