user-hooks (933B)
1 #!/usr/bin/env sh 2 3 # POSIX compliant version of Dave Eddy's 4 # zzz-user hooks. 5 6 out() { printf '\033[1;36m-> \033[m%s\n' "$@" ;} 7 error() { printf '\033[1;31m-> \033[mError: %s\n' "$@" >&2 ;} 8 die() { error "$1" ; exit 1 ;} 9 10 for sock in /tmp/.X11-unix/X* ; do 11 uid=$(stat -t "$sock" | cut -d ' ' -f 6) 12 user="$(grep ":$uid:" /etc/passwd | cut -d ':' -f 1)" 13 [ "$user" ] || die "User of $sock could not be found" 14 display=${sock##*/} 15 display=":${display#X}" 16 done 17 18 home="$(grep ":$uid:" /etc/passwd | cut -d ':' -f 6 )" 19 [ "$home" ] && [ -d "$home" ] || die "Could not find user's home directory" 20 env="DISPLAY=$display" 21 [ -e "$home/.Xauthority" ] && env="$env XAUTHORITY=$home/.Xauthority" || error "Could not find user's Xauthority file" 22 23 case ${0##*/} in 24 99-onresume) out "Running $home/.onresume" ; su -c "$env $home/.onresume" "$user" ;; 25 99-onsuspend) out "Running $home/.onsuspend" ; su -c "$env $home/.onsuspend" "$user" ;; 26 esac