]> CyberLeo.Net >> Repos - CDN/j.git/blob - ipcd
j: remove stale jipc pipe on startup, if jipc is not running
[CDN/j.git] / ipcd
1 #!/bin/sh -e
2
3 # These should be executable, and take a single parameter
4 editor="/home/cyberleo/.fhs/bin/ee"
5 browser="/home/cyberleo/.fhs/bin/ff"
6
7 jroot="${1}/root"
8 jipc="${jroot}/tmp/jipc"
9 mkdir -p "$(dirname "${jipc}")"
10
11 [ ! -p "${jipc}" ] || { echo "jipc already running? ${jipc} exists" >&2; exit 1; }
12 mkfifo -m 666 "${jipc}"
13 trap "rm -f '${jipc}'" EXIT HUP INT TERM KILL
14 echo "Listening on ${jipc} ..." >&2
15
16 resolve_path() {
17   path="${1}"
18   echo "${path}" | grep -q '^/' && echo "${jroot}${path}" || zenity --error --text="Only absolute paths are allowed in chroot protocol:\n${path}"
19 }
20
21 do_editor() {
22   arg="${1}"
23   arg="$(resolve_path "${arg}")"
24   ( "${editor}" "${arg}" || zenity --error --text="Error launching editor:\n${editor} ${arg}" ) &
25 }
26
27 do_browser() {
28   arg="${1}"
29   case "${arg}" in
30   file://*)
31     arg="${arg##file://}"
32     arg="$(resolve_path "${arg}")"
33     [ "${arg}" ] && arg="file://${arg}"
34     ;;
35   [a-z]*://*)
36     ;;
37   *)
38     arg="$(resolve_path "${arg}")"
39     [ "${arg}" ] && arg="file://${arg}"
40     ;;
41   esac
42   [ "${arg}" ] && ( "${browser}" "${arg}" || zenity --error --text="Error launching browser:\n${browser} ${arg}" ) &
43 }
44
45 do_gitk() {
46   arg="$(resolve_path "${1}")"
47   (
48     [ -d "${arg}/.git" -o -f "${arg}/config" ] && ( cd "${arg}"; gitk ) || \
49       zenity --error --text="Not a git repository:\n${arg}"
50   ) &
51 }
52
53 do_gitka() {
54   arg="$(resolve_path "${1}")"
55   (
56     [ -d "${arg}/.git" -o -f "${arg}/config" ] && ( cd "${arg}"; gitk --all ) || \
57       zenity --error --text="Not a git repository:\n${arg}"
58   ) &
59 }
60
61 do_error() {
62   cmd="${1}"
63   arg="${2}"
64   zenity --error --text="Unrecognized verb in protocol:\n${cmd} ${arg}"
65 }
66
67 while true
68 do
69   unset cmd arg
70   [ -p "${jipc}" ] || break
71   read cmd arg 2>&- < "${jipc}"
72   [ "${cmd}" ] || continue
73   cmd="$(echo "${cmd}" | tr '[A-Z]' '[a-z]')"
74   case "${cmd}" in
75   exit|die|kill) kill $$; exit 0 ;;
76   editor) do_editor "${arg}" ;;
77   browser) do_browser "${arg}" ;;
78   gitk) do_gitk "${arg}" ;;
79   gitka) do_gitka "${arg}" ;;
80   *) do_error "${cmd}" "${arg}" ;;
81   esac
82   sleep 0.25
83 done