fet.sh - dotfiles - These are my dotfiles. There are many like it, but these are mine.
 (HTM) git clone git://jay.scot/dotfiles
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       fet.sh (7119B)
       ---
            1 #!/bin/sh
            2 #
            3 #    fet.sh
            4 #  a fetch in pure POSIX shell
            5 #
            6 
            7 # supress errors
            8 exec 2>/dev/null
            9 set --
           10 eq() {  # equals  |  [ a = b ] with globbing
           11         case $1 in
           12                 $2) ;;
           13                 *) return 1;;
           14         esac
           15 }
           16 
           17 ## DE
           18 wm=$XDG_CURRENT_DESKTOP
           19 [ "$wm" ] || wm=$DESKTOP_SESSION
           20 
           21 ## Distro
           22 # freedesktop.org/software/systemd/man/os-release.html
           23 # a common file that has variables about the distro
           24 for os in /etc/os-release /usr/lib/os-release; do
           25         # some POSIX shells exit when trying to source a file that doesn't exist
           26         [ -f $os ] && . $os && break
           27 done
           28 
           29 if [ -e /proc/$$/comm ]; then
           30         ## Terminal
           31         while [ ! "$term" ]; do
           32                 # loop over lines in /proc/pid/status until it reaches PPid
           33                 # then save that to a variable and exit the file
           34                 while read -r line; do
           35                         eq "$line" 'PPid*' && ppid=${line##*:?} && break
           36                 done < "/proc/${ppid:-$PPID}/status"
           37 
           38                 # Make sure not to do an infinite loop
           39                 [ "$pppid" = "$ppid" ] && break
           40                 pppid=$ppid
           41 
           42                 # get name of binary
           43                 read -r name < "/proc/$ppid/comm"
           44 
           45                 case $name in
           46                         *sh|"${0##*/}") ;;  # skip shells
           47                         *[Ll]ogin*|*init|*systemd*) break;;  # exit when the top is reached
           48                         # anything else can be assumed to be the terminal
           49                         # this has the side affect of catching tmux, but tmux
           50                         # detaches from the terminal and therefore ignoring that
           51                         # will just make the init the term
           52                         *) term=$name
           53                 esac
           54         done
           55 
           56         ## WM/DE
           57         [ "$wm" ] ||
           58                 # loop over all processes and check the binary name
           59                 for i in /proc/*/comm; do
           60                         read -r c < "$i"
           61                         case $c in
           62                                 *bar*|*rc) ;;
           63                                 awesome|xmonad*|qtile|sway|i3|[bfo]*box|*wm*) wm=${c%%-*}; break;;
           64                         esac
           65                 done
           66 
           67         ## Memory
           68         # loop over lines in /proc/meminfo until it reaches MemTotal,
           69         # then convert the amount (second word) from KB to MB
           70         while read -r line; do
           71                 eq "$line" 'MemTotal*' && set -- $line && break
           72         done < /proc/meminfo
           73         mem="$(( $2 / 1000 ))MB"
           74 
           75         ## Processor
           76         while read -r line; do
           77                 case $line in
           78                         vendor_id*) vendor="${line##*: } ";;
           79                         model\ name*) cpu=${line##*: }; break;;
           80                 esac
           81         done < /proc/cpuinfo
           82 
           83         ## Uptime
           84         # the simple math is shamefully stolen from aosync
           85         IFS=. read -r uptime _ < /proc/uptime
           86         d=$((uptime / 60 / 60 / 24))
           87         up=$(printf %02d:%02d $((uptime / 60 / 60 % 24)) $((uptime / 60 % 60)))
           88         [ "$d" -gt 0 ] && up="${d}d $up"
           89 
           90         ## Kernel
           91         read -r _ _ version _ < /proc/version
           92         kernel=${version%%-*}
           93         eq "$version" '*Microsoft*' && ID="fake $ID"
           94 
           95         ## Motherboard // laptop
           96         read -r model < /sys/devices/virtual/dmi/id/product_name
           97         # invalid model handling
           98         case $model in
           99                 # alternate file with slightly different info
          100                 # on my laptop it has the device model (instead of 'hp notebook')
          101                 # on my desktop it has the extended motherboard model
          102                 'System '*|'Default '*|'To Be Filled'*)
          103                         read -r model < /sys/devices/virtual/dmi/id/board_name
          104         esac
          105 
          106         ## Packages
          107         # clean environment, then make every file in the dir an argument,
          108         # then save the argument count to $pkgs
          109         set --
          110         # kiss, arch, debian, void, gentoo
          111         for i in '/var/db/kiss/installed/*'  '/var/lib/pacman/local/[0-9a-z]*' \
          112         '/var/lib/dpkg/info/*.list'  '/var/db/xbps/.*'  '/var/db/pkg/*/*'; do
          113                 set -- $i
          114                 [ $# -gt 1 ] && pkgs=$# && break
          115         done
          116 
          117         read -r host < /proc/sys/kernel/hostname
          118 elif [ -f /var/run/dmesg.boot ]; then
          119         # Both OpenBSD and FreeBSD use this file, however they're formatted differently
          120         read -r bsd < /var/run/dmesg.boot
          121         case $bsd in
          122         Open*)
          123                 ## OpenBSD cpu/mem/name
          124                 while read -r line; do
          125                         case $line in
          126                                 'real mem'*)
          127                                         # use the pre-formatted value which is in brackets
          128                                         mem=${line##*\(}
          129                                         mem=${mem%\)*}
          130                                 ;;
          131                                 # set $cpu to everything before a comma and after the field name
          132                                 cpu0:*)
          133                                         cpu=${line#cpu0: }
          134                                         # Remove excess info after the actual CPU name
          135                                         cpu=${cpu%%,*}
          136                                         # Set the CPU Manufacturer to the first word of the cpu
          137                                         # variable [separated by '(' or ' ']
          138                                         vendor=${cpu%%[\( ]*}
          139                                         # We got all the info we want, stop reading
          140                                         break
          141                                 ;;
          142                                 # First 2 words in the file are OpenBSD <version>
          143                                 *) [ "$ID" ] || { set -- $line; ID="$1 $2"; }
          144                         esac
          145                 done < /var/run/dmesg.boot
          146                 [ -d /var/db/pkg ] && set -- /var/db/pkg/* && pkgs=$#
          147                 read -r host < /etc/myname
          148                 host=${host%.*}
          149         ;;
          150         # Everything else, assume FreeBSD (first line is ---<<BOOT>> or something)
          151         *)
          152                 # shellcheck source=/dev/null
          153                 . /etc/rc.conf
          154                 # shut shellcheck up without disabling the warning
          155                 host=${hostname:-}
          156 
          157                 while read -r line; do
          158                         case $line in
          159                                 # os version
          160                                 FreeBSD*)
          161                                         # If the OS is already set, no need to set it again
          162                                         [ "$ID" ] && continue
          163                                         ID=${line%%-R*}
          164                                 ;;
          165 
          166                                 CPU:*)
          167                                         cpu=${cpu#CPU: }
          168                                         # Remove excess info from after the actual CPU name
          169                                         cpu=${line%\(*}
          170                                 ;;
          171                                 *Origin=*)
          172                                         # CPU Manufacturer
          173                                         vendor=${line#*Origin=\"}
          174                                         vendor="${vendor%%\"*} "
          175                                 ;;
          176 
          177                                 'real memory'*)
          178                                         # Get the pre-formatted amount which is inside brackets
          179                                         mem=${line##*\(}
          180                                         mem=${mem%\)*}
          181                                         # This appears to be the final thing we need from the file,
          182                                         # no need to read it more.
          183                                         break
          184                         esac
          185                 done < /var/run/dmesg.boot
          186         ;;
          187         esac
          188 elif v=/System/Library/CoreServices/SystemVersion.plist; [ -f "$v" ]; then
          189         ## Macos
          190         # make sure this variable is empty as to not break the following loop
          191         temp=
          192         while read -r line; do
          193                 case $line in
          194                         # set a variable so the script knows it's on the correct line
          195                         # (the line after this one is the important one)
          196                         *ProductVersion*) temp=.;;
          197                         *)
          198                                 # check if the script is reading the derired line, if not
          199                                 # don't do anything
          200                                 [ "$temp" ] || continue
          201                                 # Remove everything before and including the first '>'
          202                                 ID=${line#*>}
          203                                 # Remove the other side of the XML tag, and insert the actual OS name
          204                                 ID="MacOS ${ID%<*}"
          205                                 # We got the info we want, end the loop.
          206                                 break
          207                 esac
          208         done < "$v"
          209 fi
          210 
          211 eq "$0" '*fetish' && printf 'Step on me daddy\n' && exit
          212 
          213 # help i dont know if it's a capital consistently
          214 eq "$wm" '*[Gg][Nn][Oo][Mm][Ee]*' && wm='foot DE'
          215 
          216 ## GTK
          217 while read -r line; do
          218         eq "$line" 'gtk-theme*' && gtk=${line##*=} && break
          219 done < "${XDG_CONFIG_HOME:=$HOME/.config}/gtk-3.0/settings.ini"
          220 
          221 # Shorten $cpu and $vendor
          222 # this is so messy due to so many inconsistencies in the model names
          223 vendor=${vendor##*Authentic}
          224 vendor=${vendor##*Genuine}
          225 cpu=${cpu##*) }
          226 cpu=${cpu%% @*}
          227 cpu=${cpu%% CPU}
          228 cpu=${cpu##CPU }
          229 cpu=${cpu##*AMD }
          230 cpu=${cpu%% with*}
          231 cpu=${cpu% *-Core*}
          232 
          233 col() {
          234         printf '  '
          235         for i in 1 2 3 4 5 6; do
          236                 printf '\033[9%sm%s' "$i" "${colourblocks:-▅▅}"
          237         done
          238         printf '\033[0m\n'
          239 }
          240 
          241 print() {
          242         [ "$2" ] && printf '\033[9%sm%6s\033[0m%b%s\n' \
          243                 "${accent:-4}" "$1" "${separator:- ~ }" "$2"
          244 }
          245 
          246 # default value
          247 : "${info:=n user os sh wm up gtk cpu mem host kern pkgs term col n}"
          248 
          249 for i in $info; do
          250         case $i in
          251                 n) echo;;
          252                 os) print os "$ID";;
          253                 sh) print sh "${SHELL##*/}";;
          254                 wm) print wm "${wm##*/}";;
          255                 up) print up "$up";;
          256                 gtk) print gtk "${gtk# }";;
          257                 cpu) print cpu "$vendor$cpu";;
          258                 mem) print mem "$mem";;
          259                 host) print host "$model";;
          260                 kern) print kern "$kernel";;
          261                 pkgs) print pkgs "$pkgs";;
          262                 term) print term "$term";;
          263                 user) printf '%7s@%s\n' "$USER" "$host";;
          264                 col) col;;
          265         esac
          266 done