#!/bin/sh -e # These should be executable, and take a single parameter editor="/home/cyberleo/.fhs/bin/ee" browser="/home/cyberleo/.fhs/bin/ff" jroot="${1}/root" jipc="${jroot}/tmp/jipc" mkdir -p "$(dirname "${jipc}")" [ ! -p "${jipc}" ] || { echo "jipc already running? ${jipc} exists" >&2; exit 1; } mkfifo -m 666 "${jipc}" trap "rm -f '${jipc}'" EXIT HUP INT TERM KILL echo "Listening on ${jipc} ..." >&2 resolve_path() { path="${1}" echo "${path}" | grep -q '^/' && echo "${jroot}${path}" || zenity --error --text="Only absolute paths are allowed in chroot protocol:\n${path}" } do_editor() { arg="${1}" arg="$(resolve_path "${arg}")" ( "${editor}" "${arg}" || zenity --error --text="Error launching editor:\n${editor} ${arg}" ) & } do_browser() { arg="${1}" case "${arg}" in file://*) arg="${arg##file://}" arg="$(resolve_path "${arg}")" [ "${arg}" ] && arg="file://${arg}" ;; [a-z]*://*) ;; *) arg="$(resolve_path "${arg}")" [ "${arg}" ] && arg="file://${arg}" ;; esac [ "${arg}" ] && ( "${browser}" "${arg}" || zenity --error --text="Error launching browser:\n${browser} ${arg}" ) & } do_gitk() { arg="$(resolve_path "${1}")" ( [ -d "${arg}/.git" -o -f "${arg}/config" ] && ( cd "${arg}"; gitk ) || \ zenity --error --text="Not a git repository:\n${arg}" ) & } do_gitka() { arg="$(resolve_path "${1}")" ( [ -d "${arg}/.git" -o -f "${arg}/config" ] && ( cd "${arg}"; gitk --all ) || \ zenity --error --text="Not a git repository:\n${arg}" ) & } do_error() { cmd="${1}" arg="${2}" zenity --error --text="Unrecognized verb in protocol:\n${cmd} ${arg}" } while true do unset cmd arg [ -p "${jipc}" ] || break read cmd arg 2>&- < "${jipc}" [ "${cmd}" ] || continue cmd="$(echo "${cmd}" | tr '[A-Z]' '[a-z]')" case "${cmd}" in exit|die|kill) kill $$; exit 0 ;; editor) do_editor "${arg}" ;; browser) do_browser "${arg}" ;; gitk) do_gitk "${arg}" ;; gitka) do_gitka "${arg}" ;; *) do_error "${cmd}" "${arg}" ;; esac sleep 0.25 done