Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
set -e
# for step by step debug uncomment the below 2 lines:
# set -x
# trap read debug
source installer.bash

# set defaults
export DEBUG=0 # to turn on, set DEBUG=1, more printing happens
export REQUIRED=0
LOCAL_PATH="${HOME}"
SCRIPT_PATH="${HOME}/.irssi/scripts"


usage(){
local long="${1}"
cat << _EOF1_
ABOUT
installs the irssi configuration to show notifications on gnome desktop.

USAGE
${0} [options]

OPTIONS:
--locpath - user writeable path where "bin" folder will be used to put files
[default: ${LOCAL_PATH}]
--scripts - irssi scripts folder
[default: ${SCRIPT_PATH}]

_EOF1_

if [[ -z "${long}" ]]; then
return
fi
cat << _EOF2_
EXAMPLES:
1. Using all the defaults:
${0}

2. Passing only selection of parameters:
${0} \\
--locpath="\$HOME/local"

3. Passing ALL params:
${0} \\
--locpath="\$HOME/local" \\
--scripts="\$HOME/.irssi/plugins/scripts"

_EOF2_

}


get_opts(){
local \
long \
short
local args=""
local options_arr=(
"locpath"
"scripts"
"help"
)
REQUIRED=0
long=$( installer.generate_long_opts "${options_arr[@]}" )
short=$( installer.generate_short_opts "${options_arr[@]}" )
args="$( getopt -o "${short}" -l "${long}" -n "$0" -- "$@" )"
eval set -- "$args";
while true; do
case "$1" in
-l|--locpath)
shift; LOCAL_PATH="${1}"; shift;
;;
-s|--scripts)
shift; SCRIPT_PATH="${1}"; shift;
;;
-h)
usage; installer.die 0 "";
;;
--help)
usage "long"; installer.die 0 "";
;;
--)
shift; break;
;;
# end of options
esac
done
}


main(){
local required_apps
required_apps=(
"pgrep"
"xargs"
"irssi"
"canberra-gtk-play"
)
installer.check_required_apps "${required_apps[@]}"
get_opts "${@}"

installer.run_installer \
"${LOCAL_PATH}/bin/irssi-notifier.sh" \
"${LOCAL_PATH}/bin/notify-listener.py" \
"${SCRIPT_PATH}/notify.pl"
}


if ! [[ "$0" =~ /bash$ ]]; then
main "${@}"
fi
185 changes: 185 additions & 0 deletions installer.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#!/usr/bin/env bash
#set -x
#set -e

#DEBUG=0 # to turn on, set DEBUG=1, more printing happens
# for step by step debug uncomment the below 2 lines:
# set -x
# trap read debug
# set defaults
LOCAL_PATH="${HOME}"
SCRIPT_PATH="${HOME}/.irssi/scripts"
REQUIRED=0

installer.log_info(){
local message_str="${*}"
[[ -n "${message_str}" ]] || return 0
echo \
-en "INFO: ${message_str}"
echo
return 0
}


installer.log_debug(){
local message_str="${*}"
if [[ "${DEBUG}" -eq 0 ]]; then
return 0
fi
[[ -n "${message_str}" ]] || return 0
echo \
-en "DEBUG: ${message_str}"
echo
return 0
}


installer.log_error(){
local message_str="${*}"
[[ -n "${message_str}" ]] || return 0
echo \
-e "ERROR: ${message_str}" \
> /dev/stderr
return $?
}


installer.die(){
local return_code="${1:-1}"
local message_str="${*:2}"
installer.log_error "${message_str}"
exit "${return_code}"
}


installer.check_required_apps(){
# verify required apps are present
local \
apps \
app
apps=("${@}")
installer.log_debug "in installer.check_required_apps(${apps[*]})"
for app in "${apps[@]}"; do
type \
"${app}" \
&> /dev/null
[[ 0 -eq $? ]] \
|| installer.die $? "$0 failed: no ${app} on PATH, please install."
done
}


installer.generate_long_opts(){
# prepares long options for getopts
local \
options_arr \
option \
result

options_arr=("${@}")
result=""
for option in "${options_arr[@]}"; do
[[ -z "${result}" ]] || result+=","
result+="${option}"
result+=":"
if [[ "${REQUIRED}" -eq 0 ]]; then
result+=":"
fi
done
echo "${result}"
return 0
}


installer.generate_short_opts(){
# prepare short options for getopts
local \
options_arr \
option \
result

options_arr=("${@}")
result=""
for option in "${options_arr[@]}"; do
result+="${option:0:1}"
result+=":"
if [[ "$REQUIRED" -eq 0 ]]; then
result+=":"
fi
done
echo "${result}"
return 0
}


installer.make_desktop_entry(){
local \
apppath \
dest_path \
base_path \
desktop_entry_path
installer.log_debug "in installer.make_desktop_entry(${*})"
apppath="${1}"
dest_path="${2:-"$HOME/.config/autostart"}"
[[ -n "${apppath}" ]] \
|| installer.die $? "first param cannot be empty: '${apppath}'"
base_path="${apppath##*/}"
installer.log_debug "base_path=$base_path"
desktop_entry_path="${dest_path}/${base_path%.*}.desktop"
installer.log_debug "desktop_entry_path=$desktop_entry_path"
mkdir -p "${dest_path}"

cat > "${desktop_entry_path}" << _EOF3_
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Name=${base_path}
Comment=Launcher for ${base_path}
TryExec=${apppath}
Exec=${apppath} %F

_EOF3_
chmod +x "${desktop_entry_path}"
installer.log_debug "Created desktop entry: ${desktop_entry_path}"
return 0
}

installer.install_files(){
local \
targets \
target \
base \
folder
targets=("${@}")
installer.log_debug "=> installer.install_files(${targets[*]})"
for target in "${targets[@]}"; do
base="${target##*/}"
folder="${target%/*}"
[[ -d "${folder}" ]] \
|| installer.die $? "${folder} is not a directory"
cmd=(
"install"
"--verbose"
"-D"
"-m" "0755"
"./${base}"
"${target}"
)
installer.log_debug "Running command: '${cmd[*]}'"
"${cmd[@]}"
done
}

installer.run_installer(){
local \
scripts
scripts=("${@}")
installer.log_debug "in installer.run_install(${scripts[*]})"
[[ "${#scripts[@]}" -gt 1 ]] \
|| installer.die $? "argument must contain at least 2 elements"
installer.install_files "${scripts[@]}"
installer.make_desktop_entry \
"${scripts[1]}" \
"$HOME/.config/autostart"
}
19 changes: 13 additions & 6 deletions irssi-notifier.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/bin/bash
NLPID=`pgrep -u $UID notify-listener`
if [ ! -z "$NLPID" ]; then
DSBA=`cat /proc/$NLPID/environ | tr '\0' '\n' | grep DBUS_SESSION_BUS_ADDRESS | cut -d '=' -f2-`
DBUS_SESSION_BUS_ADDRESS=$DSBA exec "$@"
fi
#!/usr/bin/env bash
# get the notify-listener's PID:
NLPID=$(pgrep -u "${UID}" notify-listener) || exit 0
# take its DBUS_SESSION_BUS_ADDRESS expression:
DBUS_SESSION_BUS_ADDRESS_EXPR=$(
xargs \
--null \
--max-args=1 \
echo < "/proc/${NLPID}/environ" \
| grep DBUS_SESSION_BUS_ADDRESS
) || exit 0
# set our DBUS_SESSION_BUS_ADDRESS to the one we've found above:
DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS_EXPR#*=}" exec "$@"
Loading