scripts

scripts
git clone git://git.wimdupont.com/scripts.git
Log | Files | Refs | README | LICENSE

otp (1394B)


      1 #!/usr/bin/env bash
      2 
      3 source ~/.config/scripts/properties
      4 
      5 shopt -s nullglob globstar
      6 
      7 typeit=0
      8 
      9 while getopts ":t" arg; do
     10   case $arg in
     11     t) typeit=1
     12   esac
     13 done
     14 
     15 shift $((OPTIND-1))
     16 
     17 value=$1
     18 
     19 if [[ -n $WAYLAND_DISPLAY ]]; then
     20 	dmenu=dmenu-wl
     21 	xdotool="ydotool type --file -"
     22 elif [[ -n $DISPLAY ]]; then
     23 	dmenu=dmenu
     24 	xdotool="xdotool type --clearmodifiers --file -"
     25 else
     26 	echo "Error: No Wayland or X11 display detected" >&2
     27 	exit 1
     28 fi
     29 
     30 prefix=${PASSWORD_STORE_DIR-~/.password-store}
     31 password_files=( "$prefix"/otp/**/*.gpg )
     32 password_files=( "${password_files[@]#"$prefix"/}" )
     33 password_files=( "${password_files[@]%.gpg}" )
     34 
     35 function use_arg_file () {
     36 	password=$1
     37 	if [[ $typeit -eq 0 ]]; then
     38 		pass otp -c "$password" 2>/dev/null
     39 	else
     40 		pass otp "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool
     41 	fi
     42 	exit;
     43 }
     44 
     45 if [ -n "${value}" ]; then
     46 	shift
     47 	let i=0
     48 	for file in "${password_files[@]}" 
     49 	do
     50 		if [[ "$file" =~ "${value}" ]] ; then
     51 			argFile="${file}"
     52 			let i++
     53 		fi
     54 	done
     55 
     56 	if [ "$i" == 1 ]; then
     57 		use_arg_file "${argFile}"
     58 	else
     59 		if [ "$i" == 0 ]; then
     60 			echo "Found no otp files for \"${value}\"."
     61 		fi
     62 		if [ "$i" -gt 1 ]; then
     63 			echo "Found more than one possible otp file for \"${value}\"."
     64 		fi
     65 	fi
     66 else
     67 	password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$dmenu_font" -i )
     68 	[[ -n $password ]] || exit
     69 	
     70 	use_arg_file "${password}"
     71 fi