#!/bin/bash
lookup_password_gnome() {
    pwd="$(secret-tool lookup "$1" Passwords)"
    if [ $? = 0 ]
    then
        echo "$pwd"
        return 0
    else
        echo "Could not find password \"$1\" in Gnome keyring." >&2
        echo ""
        return 1
    fi
}

lookup_password_kde() {
    pwd="$(kwallet-query -r "$1" kdewallet)"
    if [ $? = 0 ]
    then
        echo "$pwd"
        return 0
    else
        echo "Could not find password \"$1\" in KDE wallet." >&2
        echo ""
        return 1
    fi
}

lookup_password() {
    password="$1"
    which kwallet-query >/dev/null
    has_kde=$?
    which secret-tool >/dev/null
    has_gnome=$?
    if [ $has_kde = 0 ]
    then
        pwd="$(lookup_password_kde "$password")"
    elif [ $has_gnome = 0 ]
    then
        pwd="$(lookup_password_gnome "$password")"
    else
        echo "System does not use either KDE wallet or Gnome keyring" >&2
        return 2;
    fi
    
    if [ $? = 0 ]
    then
        echo "$pwd";
        return 0;
    else
        echo ""
        return 1;
    fi
}

is_screen_on() {
    [ "$(adb shell dumpsys power | grep mHoldingDisplaySuspendBlocker= | grep -oE '(true|false)')" == true ] && return
    false
}

is_unlocked() {
    [ "$(adb shell dumpsys window| awk '/mDreamingLockscreen=/{print $2; exit}' | grep -oE '(true|false)')" == false ] && return
    false
}

unlock() {
    if ! is_screen_on
    then
        adb shell input keyevent 26 # wakeup
    fi
    adb shell input keyevent 82 # get the keyboard ready
    adb shell input keyevent 66;
    adb shell input keyevent 66;
    password_name="android-${ANDROID_SERIAL}"
    sleep 1; # wait a bit to ensure all the inputs have fully processes.
    password="$(lookup_password "$password_name")"
    if [ -z "$password" ]
    then
        echo "No password found. Please submit one under the \"${password_name}\" entry in the OS keyring." >&2
        exit 1
    else
        adb shell input keyboard text "$password"
        adb shell input keyboard keyevent 66;
        echo "OK, should be on now." >&2
    fi
}

lock() {
    adb shell input keyevent 26;
}

list_devices() {
    adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}'
}

first_device() {
    devices="$(adb devices | awk '/(^[[:alnum:]]+[[:blank:]]+device$)/ {print $1}')"
    if [ -z "$devices" ]
    then
        echo ""
        return
    else
        set -- "$devices"
        echo $1
    fi
}

has_devices() {
    [ ! -z "$(list_devices)" ] && return
    false
}

while getopts 'uls:' flag
do
    case "${flag}" in
        u) set_unlock=true ;;
        l) set_lock=true ;;
        s) export ANDROID_SERIAL="${OPTARG}" ;;
        *) print_usage; exit 1 ;;
    esac
done

which adb >/dev/null 2>/dev/null
if [ $? != 0 ]
then
    echo "ADB not installed or not located in $PATH" >&2
    exit 4
fi

if ! has_devices
then
    echo "No Android devices found" >&2
    exit 3
fi

if [ -z "$ANDROID_SERIAL" ]
then
    export ANDROID_SERIAL="$(first_device)"
fi

if is_unlocked
then
    if [ "$set_unlock" != true ]
    then
        echo "Locking screen." >&2
        lock
    else
        echo "Screen is already unlocked." >&2
    fi
else
    if [ "$set_lock" != true ]
    then
        echo "Screen is off. Turning on." >&2
        unlock
    else
        echo "Screen is already locked." >&2
    fi
fi
