#!/bin/sh set -e [ -n "$DEBUG" ] && set -x by_pid= by_name= fuzzy= desktop_files= scalable= while getopts n:p:dfsh opt; do case "$opt" in n) by_name="${OPTARG}" ;; p) by_pid="${OPTARG}" ;; f) fuzzy=1 ;; d) desktop_files=1 ;; s) scalable=1 ;; h | ?) printf "usage: %s [-h]\n" "$(basename "$0")" printf "\n" printf " -p \t PID\n" printf " -n \t NAME\n" printf " -f \t enable fuzzy matching (NAME only)\n" printf " -d \t try to match NAME desktop file\n" printf " -s \t only match scalable icons\n" printf "\n" exit 2 ;; esac done shift $((OPTIND - 1)) desktop_dirs="/usr/share/applications/ /usr/local/share/applications/" icon_dirs="/usr/share/icons/ /usr/local/share/icons/" by_name() { name=$1 if [ -n "${desktop_files}" ]; then [ -z "${fuzzy}" ] && name="^${name}$" # try getting .desktop entry for name # shellcheck disable=2086 ident=$(fd -e desktop "${name}" ${desktop_dirs} -x grep Icon= 2>/dev/null || true | cut -d= -f2 | uniq) fi if [ -z "${fuzzy}" ]; then ident="^${ident:-$name}\." else ident="${ident:-$name}" fi filetypes="-e svg" if [ -z "${scalable}" ]; then filetypes="${filetypes} -e png" fi # shellcheck disable=2086 fd -t f ${filetypes} "${ident}" ${icon_dirs} 2>/dev/null || true # avoid selecting svg, can be made to prefer svg if `-v` is removed # svg=$(printf "%s" "${icons}" | grep -v svg) # [ -n "${svg}" ] && icons=${svg} # printf "%s\n" "${icons}" } by_pid() { pid=$1 arg0= while true; do arg0=$(awk '{print $1}' /proc/"${pid}"/comm) case ${arg0} in *sh) pid=$(ps -p "${pid}" -o ppid= | xargs) [ "$pid" = 1 ] && exit 1 ;; *) break ;; esac done by_name "${arg0}" } # main if [ -n "${by_name}" ]; then by_name "${by_name}" elif [ -n "${by_pid}" ]; then by_pid "${by_pid}" fi