#!/bin/bash
#
# list_loaded_launchjob_ids
#

###
### settings
###

set -e                # exit on any uncaught error
set +o histexpand     # don't expand history expressions
shopt -s nocasematch  # case-insensitive regular expressions

###
### global variables
###

###
### functions
###

launchjob_id_source_1 () {
    if [[ "$EUID" -ne 0 ]]; then
        /usr/bin/sudo -p 'Optionally give your sudo password: ' -- /bin/launchctl list | /usr/bin/cut -f3
    fi
}

launchjob_id_source_2 () {
    /bin/launchctl list | /usr/bin/cut -f3
}

merge_sources () {
    /usr/bin/sort | /usr/bin/uniq
}

clean_sources () {
    /usr/bin/egrep -v '^Label$' |                   \
    /usr/bin/egrep -v '^com\.apple\.' |             \
    /usr/bin/egrep -v '^0x[0-9a-f]+\.anonymous\.' | \
    /usr/bin/egrep -v '^\[0x' |                     \
    /usr/bin/egrep -v '\.[0-9]+$'
}

###
### main
###

_list_loaded_launchjob_ids () {

    {
      launchjob_id_source_1;
      launchjob_id_source_2;
    } |                     \
    clean_sources |         \
    merge_sources

}

# process args
if [[ $1 =~ ^-+h(elp)?$ ]]; then
    printf "list_loaded_launchjob_ids

List IDs for currently-loaded launchctl jobs, which may be useful
in a Cask uninstall stanza, eg

    uninstall :launchctl => 'job.id.goes.here'

If this command is not run as the superuser, you will be prompted
for a password to run a subcommand using 'sudo'.  The password is
not required, but supplying it may reveal additional job ids.  To
skip using the password, press <return> repeatedly.

Launchctl jobs attributed to Apple are excluded from the output.

See CONTRIBUTING.md and 'man launchctl' for more information.

"
    exit
fi

# dispatch main
_list_loaded_launchjob_ids "${@}"

#
