Dash:
[t2-trunk.git] / misc / tail / formats / tail.in
blobf536826e451a92d6e18a4c20a0e6ad790639b3d4
1 #!/bin/sh
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 #
5 # T2 SDE: misc/tail/formats/tail.in
6 # Copyright (C) 2004 - 2005 The T2 SDE Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
16 # T2 Automated Init Language - Tail
17 # http://wiki.t2-project.org/index.cgi?TailFormat
18 # Alejandro Mery <amery@geeks.cl>
20 tail_read() {
21 local verbose=1 file= output= query=
22 local query= count=2
24 if [ "$1" == "-q" ]; then
25 verbose=
26 shift
29 file="$1"; shift
30 while [ $# -gt 1 ]; do
31 query="${query}$1 "
32 shift;
33 (( count++ ))
34 done
35 query="${query}$1\\( \\|\$\\)"
37 output="$( grep -e "^$query" $file | cut -f$count- )"
38 [ "$verbose" ] && echo "$output"
40 grep -q -e "^$query" $file
43 tail_install() {
44 local style=$1 tailfile="$2"
45 local x= y= z=
46 shift 2
48 case "$style" in
49 runit)
50 local servicedir=$( echo `eval echo $1` )
51 local runprefix=
53 mkdir -vp "$servicedir"
54 rm -f /tmp/tail.$$
56 # run /tailrun/ options
58 eval "`tail_read $tailfile tailrun $style`"
60 # ./env/
62 if tail_read -q $tailfile env; then
63 mkdir -p "$servicedir/env"
64 runprefix="$runprefix${runprefix:+ }envdir ./env"
65 tail_read $tailfile env | while read x y; do
66 echo "TAIL: ./env/$x = $y"
67 echo "$y" | sde_substitute > $servicedir/env/$x
68 done
71 # ./run
73 cat <<-EOT > /tmp/tail.$$
74 #!/bin/sh
75 exec 2>&1
76 EOT
78 if tail_read -q $tailfile input; then
79 echo "exec < $( tail_read $tailfile input )" >> /tmp/tail.$$
82 if tail_read -q $tailfile user; then
83 runprefix="$runprefix${runprefix:+ }envuidgid $( tail_read $tailfile user )"
86 echo "exec $runprefix${runprefix:+ }$( tail_read $tailfile run )" >> /tmp/tail.$$
88 sde_substitute /tmp/tail.$$ > $servicedir/run
89 chmod +x $servicedir/run
91 # ./conffile
93 for x in `tail_read $tailfile conf | cut -f1 | sort -u`; do
94 y="$x"
95 echo "TAIL: $x ..."
96 if [ "${y%/*}" != "$y" ]; then
97 if [ "${y:0:1}" != "/" ]; then
98 y="$servicedir/$y"
100 mkdir -vp ${y%/*}
102 rm -vf $y
103 tail_read $tailfile conf $x | sde_substitute > $y
104 done
106 # ./log/
108 if tail_read -q $tailfile log; then
109 mkdir -vp "$servicedir/log/main"
111 if [ -z "$logdir" ]; then
112 if [ "${localstatedir%$pkg}" != "$localstatedir" ]; then
113 logdir=$localstatedir/log
114 else
115 logdir=$localstatedir/log/$pkg
120 for x in `tail_read $tailfile log | cut -f1 | sort -u`; do
121 echo "TAIL: ./log/$x"
122 mkdir -vp $root/$logdir/$x
123 chown 9:9 $root/$logdir/$x
125 ln -vnfs $logdir/$x $servicedir/log/main/
127 z="$z main/$x"
129 y="`tail_read $tailfile log $x`"
130 [ "$y" ] && echo "$y" > $root/$logdir/$x/config || true
131 done
132 cat <<-EOT > "$servicedir/log/run"
133 #!/bin/sh
134 exec 2>&1
136 exec setuidgid log svlogd -t $z
138 chmod +x "$servicedir/log/run"
143 abort "tail_install: unknown style '$style' for $tailfile."
145 esac