#!/usr/bin/env bash

source ~/.rcs/bash_prompt/bw/bw-unlock.sh
#BW_STATUS="`bw status`"

#if [ "`echo $BW_STATUS | grep unauthenticated`" != "" ]
#then
#       bw config server https://bw.sebeka.k12.mn.us
#       bw login
#fi
BW_STATUS="`bw status`"
if [ "`echo $BW_STATUS | grep unlocked`" = "" ]
then
        echo "Bitwarden vault is locked, enter master password"
        `bw unlock | grep export | cut -d "$" -f2 | tr -s " "` && bw sync
else
        #echo "Bitwarden vault is unlocked"
        bw sync
fi
HOSTNAME=`hostname`
ROOT_PW=`bw get password root@${HOSTNAME}`
if [ "$ROOT_PW" != "Not found." ] && [ "$ROOT_PW" != "" ]
then
        echo "Found sudo record"
        export BW_PWD="${ROOT_PW}"
        if [ "$1" != "" ]
        then
                ~/.rcs/bash_prompt/bw/expect_sudo_script $@
        else
                ~/.rcs/bash_prompt/bw/expect_sudo_script -i
        fi
else
        echo "No reound found for root@${HOSTNAME}"
fi
