dotfiles/scripts/change-pass.sh

73 lines
2.5 KiB
Bash

#!/bin/sh
page="$(find "$PASSWORD_STORE_DIR/" -not -path "$PASSWORD_STORE_DIR/.git/*" \
-type f | sed "s|.gpg$||")"
page_relative="$(echo "$page" | sed "s|$PASSWORD_STORE_DIR||" | \
grep -E "^(/[^/]+)(/[^/]+)(/[^/]+)?/?$")"
page_relative="$(echo "$page_relative" | sort | dmenu -l 15 -i)"
[ -z "$page_relative" ] && exit 0
[ -z "$(echo "$page_relative" | grep -E "^(/[^/]+)(/[^/]+)(/[^/]+)?/?$")" ] && \
notify-send -t 11000 "The reason to show first level folders is to make use\
of <TAB> on dmenu and create new password inside them. There should NOT be\
passwords with first level names." && exit 1
page="$PASSWORD_STORE_DIR$page_relative"
info="$(echo "Yes\nNo" | rofi -dmenu -i -p "Change info?")"
[ -z $(echo "$info" | grep -E "(Yes|No)") ] && exit 0
change="$(echo "Yes\nNo" | rofi -dmenu -i -p "Change password?")"
if [ "$change" = "Yes" ]; then
generate="$(echo "Yes\nNo" | rofi -dmenu -i -p "Generate password?")"
[ -z $(echo "$generate" | grep -E "(Yes|No)") ] && exit 0
fi
passw="$(pass show "$page_relative")"
contents_nopass="$(echo "$passw" | tail -n +2)"
passw="$(echo "$passw" | head -n 1)"
if [ "$info" = "Yes" ]; then
file="/tmp/change-pass$(date +%s)"
echo "$contents_nopass" > "$file"
cp "$file" "$file-copy"
$TSCRIPTS/run-on-terminal.sh $EDITOR $file
# [ -z "${TERMINAL##*termite*}" ] && $TERMINAL -e "$EDITOR $file" || \
# $TERMINAL -e $EDITOR $file
[ -z "$(cat $file)" ] && notify-send "Operation CANCELLED" && \
rm "$file" "$file-copy" && exit 1
[ -z "$(diff "$file" "$file-copy")" ] && notify-send "Operation CANCELLED" \
&& rm "$file" "$file-copy" && exit 1
contents_nopass="$(cat "$file")"
rm "$file" "$file-copy"
fi
if [ "$change" = "Yes" ]; then
if [ "$generate" = "Yes" ]; then
contents="$(pwgen -ysBv 15 -N 1)"
else
tries=0
while [ "$tries" -ne 3 ]; do
passwd="$(rofi -i -dmenu -password -p "Type")"
passwdcheck="$(rofi -i -dmenu -password -p "Type Again")"
[ "$passwd" != "$passwdcheck" ] || break \
&& notify-send "Passwords are not equal"
tries="$(( $tries + 1 ))"
done
[ "$passwd" != "$passwdcheck" ] && notify-send "Operation CANCELLED" && \
exit 1
contents="$passwd"
fi
contents="$contents\n$contents_nopass"
else
contents="$passw\n$contents_nopass"
fi
if [ -n "$contents" ]; then
echo "$contents" | pass insert -m "$page_relative" || \
(notify-send "Error: pass insert" && exit 1)
fi