#!/usr/bin/env osascript
# Copyright (c) 2014-, Johan Wassberg <johan@rymdvarel.se>
# Copyright (c) 2011-, Simon Lundström <simmel@soy.se>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

on run argv
    set args to argv as text
    launch application "System Events"
    set agent to POSIX file "/System/Applications/Utilities/Terminal.app/Contents/Resources/Terminal.icns"
    set dialog_timeout to 15
    try
        tell application "System Events"
            if args ends with ": " or args ends with ":" then
                if args contains "pass" or args contains "pin" then
                    display dialog args with icon agent default button {get localized string of "OK"} default answer "" with hidden answer
                else
                    display dialog args with icon agent default button {get localized string of "OK"} default answer ""
                end if
                set text_returned to result's text returned
            else
                display dialog args with icon agent default button 1 giving up after dialog_timeout
                if gave up of result then
                    error result
                end if
                set text_returned to ""
            end if
            quit
        end tell
        if text_returned is not "" then
            return text_returned
        else
            return
        end if
    on error e number n
        tell application "System Events" to quit
        error e
    end try
end run
