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