#!/usr/bin/env bash
#---------------------------------------------------------------------------------------
# git askpass via zenity (For Linux)
#
# Author:   lambdalisue <lambdalisue@hashnote.net>
# License:  MIT License
#
# Usage:
#   git config --global core.askpass={path to this script}
#
# Reference:
#   https://help.gnome.org/users/zenity/stable/index.html.ja
#   https://github.com/git/git/blob/35f6318d44379452d8d33e880d8df0267b4a0cd0/prompt.c#L7
#---------------------------------------------------------------------------------------
if ! type zenity >/dev/null 2>&1; then
  (>&2 echo "zenity is not found. Please install it first.")
  exit 1
fi

PROMPT=$1
if [[ -z $PROMPT ]]; then
  (>&2 echo "a first argument is not specified")
  exit 1
fi

if [[ "$PROMPT" =~ Username ]]; then
  zenity --entry \
    --title="Username" \
    --text="$PROMPT" \
    2>/dev/null
else
  zenity --entry \
    --title="Password" \
    --text="$PROMPT" \
    --hide-text \
    2>/dev/null
fi