dotfiles

Dash Eclipse's dotfiles
git clone git://ezup.dev/dotfiles.git
Log | Files | Refs | README | LICENSE

commit e5d446c863e221db6c7375910f248b330ccac210
parent 656207141b464e4042a6e911ed86f675b9c1f074
Author: Dash Eclipse <dash@ezup.dev>
Date:   Fri, 18 Sep 2020 18:46:14 +0000

Add shell scripts to .local/bin

Diffstat:
A.gitignore | 19+++++++++++++++++++
A.local/bin/dwm-helper.sh | 19+++++++++++++++++++
A.local/bin/dwm_update_status.sh | 183+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A.local/bin/tg-noworg.sh | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A.local/bin/webdav-curl-sync.sh | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 407 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,19 @@ +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ diff --git a/.local/bin/dwm-helper.sh b/.local/bin/dwm-helper.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +: "${progname:="${0##*/}"}" + +screenshot() { + sleep 0.2 && scrot -s -e 'xclip -selection clipboard -t "image/png" < $f' "$HOME/Pictures/Screenshot/%Y-%m-%d-\$p_\$wx\$h_scrot.png" +} + +usage() { + echo "Usage: $progname screenshot" +} + +case "$1" in + screenshot) screenshot ;; + * ) usage ;; +esac + +exit 0 + diff --git a/.local/bin/dwm_update_status.sh b/.local/bin/dwm_update_status.sh @@ -0,0 +1,183 @@ +#!/bin/bash + +### Constants +PROC_UPTIME='/proc/uptime' +PROC_STAT='/proc/stat' +PROC_MEMINFO='/proc/meminfo' +#SYS_BAT_CAPACITY='/sys/class/power_supply/sbs-20-000b/capacity' +#SYS_BAT_STATUS='/sys/class/power_supply/sbs-20-000b/status' +#SYS_BAT_2EMPTY='/sys/class/power_supply/sbs-20-000b/time_to_empty_avg' +#SYS_BAT_2FULL='/sys/class/power_supply/sbs-20-000b/time_to_full_avg' +SYS_BRIGHTNESS='/sys/class/backlight/backlight/brightness' +SYS_MAX_BRIGHTNESS='/sys/class/backlight/backlight/max_brightness' +SYS_TEMPERATURE='/sys/class/thermal/thermal_zone3/temp' +PROC_WIRELESS='/proc/net/wireless' +#PROC_DPMS='/sys/devices/platform/display-subsystem/drm/card0/card0-eDP-1/dpms' +PROC_DPMS='/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-HDMI-A-1/dpms' +PREV_TOTAL=0 +PREV_IDLE=0 + +### Functions +function sec2time () { + local T=$1 + local D=$((T/60/60/24)) + local H=$((T/60/60%24)) + local M=$((T/60%60)) + local S=$((T%60)) + #[[ $D > 0 ]] && printf '%d days ' $D + #[[ $H > 0 ]] && printf '%d hours ' $H + #[[ $M > 0 ]] && printf '%d minutes ' $M + #![[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' + #printf '%d second\n' $S + + [[ $D > 0 ]] && printf '%d days ' $D + [[ $H > 0 ]] && printf '%02d:' $H + [[ $M > 0 ]] && printf '%02d:' $M + #[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' + #printf '%d second\n' $S + printf '%02d\n' $S +} +function get-date () { + printf " $(date +'%F  %T')" +} +function get-uptime () { + printf " [$(sec2time $(cat $PROC_UPTIME | cut -d'.' -f1))]" +} +#function get-kbd () { + #printf " $(setxkbmap -query | grep variant | colrm 1 12)" +#} +function get-cpu-average () { + printf " $(grep 'cpu ' $PROC_STAT | awk '{usage=(1000*($2+$4)/($2+$4+$5)+5)/10} END {print int(usage) "%%"}')" +} +function get-cpu-current () { + # Get the total CPU statistics, discarding the 'cpu ' prefix. + #CPU=(`sed -n 's/^cpu\s//p' /proc/stat`) + CPU=(`grep -oP '(?<=cpu ).*' $PROC_STAT`) + IDLE=${CPU[3]} # Just the idle CPU time. + # Calculate the total CPU time. + TOTAL=0 + for VALUE in "${CPU[@]}"; do + let "TOTAL=$TOTAL+$VALUE" + done + # Calculate the CPU usage since we last checked. + let "DIFF_IDLE=$IDLE-$PREV_IDLE" + let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" + let "DIFF_USAGE=(100000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10" + #printf "%0.2f%%\\n" "${DIFF_USAGE}e-2" + # Remember the total and idle CPU times for the next check. + PREV_TOTAL="$TOTAL" + PREV_IDLE="$IDLE" + + printf "%0.2f%%" "${DIFF_USAGE}e-2" +} +function get-ram () { + while IFS=":" read -r a b; do + case "$a" in + "MemTotal") mem_used="$((mem_used+=${b/kB}))"; mem_total="${b/kB}" ;; + "Shmem") mem_used="$((mem_used+=${b/kB}))" ;; + "MemFree" | "Buffers" | "Cached" | "SReclaimable") + mem_used="$((mem_used-=${b/kB}))" + ;; + esac + done < $PROC_MEMINFO + mem_used="$((mem_used / 1024))" + mem_total="$((mem_total / 1024))" + memory="${mem_used}${mem_label:-MiB} / ${mem_total}${mem_label:-MiB}" + #printf " $(bc <<<"scale=1;$mem_used/$mem_total*100" | sed -e 's/^\./0./')%%" + let "mem_usage=(100000*$mem_used/$mem_total+5)/10" + printf " %0.2f%%" "${mem_usage}e-2" +} +function get-storage () { + printf " $(df / | grep -v '^Filesystem' | awk '{ print $5 }')%" +} +function get-battery () { + BAT_PERCENT="$(<$SYS_BAT_CAPACITY)%%" + + if [[ $(<$SYS_BAT_STATUS) == Discharging ]]; then + printf " $BAT_PERCENT [$(sec2time $(<$SYS_BAT_2EMPTY))]" + elif [[ $(<$SYS_BAT_STATUS) == Charging ]]; then + printf " $BAT_PERCENT $(sec2time $(<$SYS_BAT_2FULL))" + elif [[ $(<$SYS_BAT_STATUS) == Full ]]; then + printf "$BAT_PERCENT Full" + else + printf '[?]' + fi +} +function get-brightness () { + printf " $(( $(<$SYS_BRIGHTNESS)*100/$(<$SYS_MAX_BRIGHTNESS) ))%%" +} +function get-temperature () { + #printf "  $(sensors | grep temp1 | awk '{print $2}' | sed 's/+//')" + printf " %.1f°C" "$(<$SYS_TEMPERATURE)e-3" +} +function get-ip () { + printf " $(ip route get 1 2>&1 | awk '{print $7}')" +} +function get-wifi () { + if [[ $(grep wlan0 $PROC_WIRELESS) ]]; then + printf " $(grep wlan0 $PROC_WIRELESS | awk '{ print int($3 * 100 / 70) }')%%" + else + echo -n ' [?]' + fi +} +function update-status () { + #xsetroot -name " $info_date $info_uptime $info_kbd $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " + #xsetroot -name " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " + #echo " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_battery $info_brightness $info_temperature $info_ip $info_wifi " + xsetroot -name " $info_date $info_uptime $info_cpu_average $info_cpu_current $info_ram $info_storage $info_ip" + +} + +### start from here +info_date=$(get-date) +info_uptime=$(get-uptime) +#info_kbd=$(get-kbd) +info_cpu_average=$(get-cpu-average) +info_cpu_current=$(get-cpu-current) +info_ram=$(get-ram) +info_storage=$(get-storage) +#info_battery=$(get-battery) +#info_brightness=$(get-brightness) +#info_temperature=$(get-temperature) +info_ip=$(get-ip) +info_wifi=$(get-wifi) +update-status +sec=0 +while true; do + if [[ $(<$PROC_DPMS) == On ]] + then + # run commands when it's on + #echo On $(get-uptime) && sleep 1 + if [ "$(($sec % 4))" == "0" ]; then + #echo "$(date +%M:%S) 2" + info_date=$(get-date) + info_uptime=$(get-uptime) + info_cpu_current=$(get-cpu-current) + info_ram=$(get-ram) + info_storage=$(get-storage) + #info_battery=$(get-battery) + #info_brightness=$(get-brightness) + #info_temperature=$(get-temperature) + #info_wifi=$(get-wifi) + update-status + fi + + if [ "$(($sec % 40))" == "0" ]; then + info_cpu_average=$(get-cpu-average) + info_ip=$(get-ip) + update-status + + fi + + #if [ "$(($sec % 32))" == "0" ]; then + #info_kbd=$(get-kbd) + #update-status + #fi + + sleep 2; + sec=$(( $sec + 2 )) + else + echo Off + sleep 4 + fi +done diff --git a/.local/bin/tg-noworg.sh b/.local/bin/tg-noworg.sh @@ -0,0 +1,118 @@ +#!/bin/sh + +## $ crontab -l +## * * * * * ~/.local/bin/tg-noworg.sh -u +## 0 * * * * ~/.local/bin/tg-noworg.sh -d + +: "${progname:="${0##*/}"}" + +#CHAT_ID="" # private (message.from.id) +CHAT_ID="" # channel (channel_post.chat.id) +TG_BOT_TOKEN="" +tgbotapi="curl -s -F chat_id=$CHAT_ID https://api.telegram.org/bot${TG_BOT_TOKEN}" + +NOWORG="$HOME/orgmode/now.org" +BASENAME=$(basename $NOWORG) +TEMP="/tmp/tg-noworg" +LAST_CHECKED="${TEMP}/last_checked.txt" +LOG="$HOME/tg-noworg/log.json" + +if [ ! -d "$TEMP" ]; then + mkdir $TEMP && cp $NOWORG $TEMP + date -u +%s >$LAST_CHECKED + echo "make directory: ${TEMP}" + exit 0 +fi + +send_message() { + ${tgbotapi}/sendMessage -F parse_mode="MarkdownV2" --form-string text="$1" | sed -e 's/\(,"text":"\).*/\1"}}\n/' >>$LOG +} + +modify_diff() { + sed -e ' + # remove first two lines, empty address lines + 1,2d + s/^@@ .*$// + + # remove lines which contains orgmode timestamps + /\(SCHEDULED:\|CLOSED:\|:PROPERTIES:\|:LAST_REPEAT:\|:END:\|- State "DONE" from "TODO"\)/d + + # escape for MarkdownV2 + s/\\/\\\\/g + s/\(_\|*\|\[\|\]\|(\|)\|~\|`\|>\|#\|+\|-\|=\||\|{\|}\|\.\|!\)/\\\1/g + + # headlines + /^\(\\\-\|\\+\)\(\\\*\\\* \)/ {s/^\(\\\-\|\\+\)\(\\\*\\\* \)/\1*/; s/$/*/} + /^\(\\-\|\\+\)\\\*\\\*\\\* / {s/^\(\\-\|\\+\)\(\\\*\\\*\\\*\) \(\|TODO\|DONE\)\(\| \)/\1*\\#\3* __*_/; s/$/_*__/} + + # plain lists + /^\(\\\-\|\\+\)\(\|\s\+\)\(\\\-\|\\+\|[0-9]\\\.\|[0-9]\\)\) / {s/^\(\\\-\|\\+\)\(\|\s\+\)\(\\\-\|\\+\|[0-9]\\\.\|[0-9]\\)\)\(\| \)\(\|\\\[ \\\]\|\\\[X\\\]\|\\\[\\\-\\\]\)\(\| \)/\1`\2\3\4\5\6`_/; s/$/_/} + s/\(\\\-\|\\+\)\(\s\+ \)/\1`\2`/ + + # strikethrough for removed lines, remove ^+ for added lines + s/^\\\-/~/; /^~/s/$/~/ + s/^\\+// + ' +} + +update_modified() { + lastcheck=$(cat $LAST_CHECKED) + echo "lastcheck: $(date -u +%FT%TZ -d @${lastcheck})" + lastmod=$(stat -c %Y $NOWORG) + if [ "$lastmod" -gt "$lastcheck" ]; then + TEXT=$(diff -u0 ${TEMP}/$BASENAME $NOWORG | modify_diff) + send_message "$TEXT" + cp $NOWORG ${TEMP}/$BASENAME + date -u +%s >$LAST_CHECKED + echo "update: $BASENAME" + else + echo "skip: $BASENAME" + fi +} + +purge_message() { + sed -i '/^$/d' $LOG + TIME_48H_AGO=$(date -u -d-48hours-5minute +%s) + TIME_40H_AGO=$(date -u -d-40hours +%s) + i=1 + while IFS= read -r json; do + echo "########## Line $i" + json=$(echo "$json" | sed 's/\(,"description":"\).*/\1"}/') + OK=$(echo "$json" | jq .ok) + if [ $OK = true ]; then + MESSAGE_ID=$(echo "$json" | jq .result.message_id) + CHAT_ID=$(echo "$json" | jq .result.chat.id) + JSON_DATE=$(echo "$json" | jq .result.date) + FORMATTED_DATE=$(date -u +%FT%TZ -d@${JSON_DATE}) + DURATION=$(ddiff -i'%s' -f'%H hours %M minutes ago' $JSON_DATE $(date +%s)) + [ $JSON_DATE -lt $TIME_48H_AGO ] && echo "$FORMATTED_DATE ($DURATION) -- CANT BE DELETED" + [ $JSON_DATE -gt $TIME_40H_AGO ] && echo "$FORMATTED_DATE ($DURATION) -- RESERVED" + if [ $JSON_DATE -ge $TIME_48H_AGO ] && [ $JSON_DATE -le $TIME_40H_AGO ]; then + echo "$FORMATTED_DATE ($DURATION) -- WILL BE DELETED" + $tgbotapi/deleteMessage -F message_id=$MESSAGE_ID | sed 's/$/\n/' + sed -i "${i}s/^.*$//" $LOG + sleep 1 + fi + else + ERROR_CODE=$(echo "$json" | jq .error_code) + echo "Contains ERROR CODE: $ERROR_CODE" + fi + i=$(( i + 1 )) + echo + done <$LOG +} + +usage() { + cat <<_EOF +Usage: $progname -u Update modified content to Telegram channel + $progname -d Purge messages which sent between 40 and 48 hours ago +_EOF + exit 1 +} + +case "$1" in + -u) update_modified ;; + -d) purge_message ;; + *) usage ;; +esac +exit 0 diff --git a/.local/bin/webdav-curl-sync.sh b/.local/bin/webdav-curl-sync.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +## $ crontab -l +## @reboot sleep 60 && ~/.local/bin/webdav-curl-sync.sh -d +## 0 */3 * * * ~/.local/bin/webdav-curl-sync.sh -d + +LAST_UPLOADED="" +: "${progname:="${0##*/}"}" + +preview() { + curl -s https://user:password@domain.tld:port/orgmode.org +} + +download() { + for orgmode in file1 file2; do + #HTTPS_PROXY="socks5h://127.0.0.1:1080" + filename="${orgmode}.org" + file="$HOME/orgmode/$filename" + uri="https://user:password@domain.tld:port/${orgmode}.org" + if test -e "$file"; then + zflag="-z $file" + else + zflag= + fi + HTTP_CODE=$(curl -sR --write-out %{http_code} -o "$file" $zflag "$uri") + LAST_MODIFIED=$(date -r $file -u '+%F %T') + echo -n 'Download: ' + case "$HTTP_CODE" in + 304) echo "$LAST_MODIFIED $filename (bypassed)";; + 200) echo "$LAST_MODIFIED $filename (update)";; + 000) echo "$filename (failed)";; + *) echo "$filename ($HTTP_CODE)";; + esac + done +} + +upload() { + dir="$HOME/orgmode" + uri="https://user:password@domain.tld:port/orgmode" + for orgmode in $dir/*.org; do + filename=$(basename $orgmode) + filemod=$(stat -c %Y $orgmode) + if [ "$filemod" -gt "$LAST_UPLOADED" ]; then + HTTP_CODE=$(curl -s -o /dev/null --write-out %{http_code} -T "$orgmode" "$uri/$filename") + if [ "$HTTP_CODE" = "201" ]; then + echo "UPLOAD: $filename" + sed -i "s/^LAST_UPLOADED=.*$/LAST_UPLOADED=\"$(date +%s)\"/" $(realpath $0) + else + echo "UPLOAD: $filename (failed)" + fi + else + echo "UPLOAD: $filename (bypassed)" + fi + done +} + +usage() { + echo "Usage: $progname -p|-u|-d|-a" +} + +case "$1" in + -p) preview ;; + -u) upload ;; + -d) download ;; + -a) download; upload ;; + *) usage ;; +esac +exit 0