Debian Codegraph @

src/ezstream-file.sh.in

raw
1 #!/bin/sh
2
3 # ezstream-file.sh
4 # Convenience/wrapper script that uses ezstream to stream one or more files
5 # given on the command line.
6
7 # Copyright (c) 2009, 2015 Moritz Grimm <[email protected]>
8 #
9 # Permission to use, copy, modify, and distribute this software for any
10 # purpose with or without fee is hereby granted, provided that the above
11 # copyright notice and this permission notice appear in all copies.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
21 _myname="$(basename $0)"
22 _filename_placeholder="%FILENAME%"
23
24 _opt_string="hp:qrT:Vv"
25 print_usage()
26 {
27 echo "usage: ${_myname} [-hqrVv] [-T cfg_template] [file ...]" >&2
28 }
29
30 print_usage_help()
31 {
32 cat << __EOT >&2
33
34 -h print this help and exit
35 -p pidfile [ezstream] write PID to pidfile
36 -q [ezstream] suppress STDERR output from external en-/decoders
37 -r [ezstream] show real-time stream information on stdout
38 -T template run ezstream using template for configuration
39 -V print the version number and exit
40 -v [ezstream] increase logging verbosity
41
42 The configuration template must contain the configuration statement
43 <filename>${_filename_placeholder}</filename>
44 inside the <media /> block.
45
46 See the ezstream-file.sh(1) manual for detailed information.
47 __EOT
48 }
49
50 _pidfile=""
51 _pidfile_arg=""
52 _quiet=""
53 _rtstatus=""
54 _verbose=""
55 _cfg_template="${EZSTREAM_TEMPLATE}"
56
57 _args=`getopt ${_opt_string} $*`
58 if [ $? -ne 0 ]; then
59 print_usage
60 exit 2
61 fi
62 set -- ${_args}
63 while [ $# -ge 0 ]
64 do
65 case "$1" in
66 -h)
67 print_usage
68 print_usage_help
69 exit 0
70 ;;
71 -p)
72 _pidfile="-p"
73 _pidfile_arg="$2"
74 shift; shift ;;
75 -q)
76 _quiet="-q"; shift ;;
77 -r)
78 _rtstatus="-r"; shift ;;
79 -T)
80 _cfg_template="$2"; shift; shift ;;
81 -V)
82 echo "${_myname} @PACKAGE_VERSION@"
83 exit 0
84 ;;
85 -v)
86 if [ -z "${_verbose}" ]; then
87 _verbose="-v"
88 else
89 _verbose="${_verbose}v"
90 fi
91 shift ;;
92 --)
93 shift; break ;;
94 esac
95 done
96
97 if [ -z "${_cfg_template}" ]; then
98 echo "${_myname}: No configuration template supplied." >&2
99 echo "Use -T or the EZSTREAM_TEMPLATE environment variable." >&2
100 exit 2
101 fi
102 if [ ! -e "${_cfg_template}" ]; then
103 echo "${_myname}: Configuration template ${_cfg_template} does not exist." >&2
104 exit 2
105 fi
106 if [ -z "$(grep ${_filename_placeholder} ${_cfg_template})" ]; then
107 echo "${_myname}: ${_cfg_template} lacks the ${_filename_placeholder} placeholder." >&2
108 exit 2
109 fi
110
111 test -n "${EZSTREAM}" || EZSTREAM="$(which ezstream)"
112 if [ -z "${EZSTREAM}" ]; then
113 echo "${_myname}: Cannot find ezstream." >&2
114 exit 2
115 fi
116
117 _temp_dir="`mktemp -t -d _ezstream.XXXXXXXXXX`"
118 if [ $? -ne 0 ]; then
119 echo "${_myname}: Unable to create temporary directory." >&2
120 exit 1
121 fi
122 trap 'rm -rf ${_temp_dir}' 0
123 trap 'rm -rf ${_temp_dir}; exit 1' 2 15
124
125 _cfg="${_temp_dir}/config.xml"
126 _playlist="${_temp_dir}/playlist.txt"
127
128 touch "${_cfg}" || exit 1
129 chmod 0600 "${_cfg}" || exit 1
130
131 sed -e "s,${_filename_placeholder},${_playlist},g" \
132 < "${_cfg_template}" \
133 > "${_cfg}"
134 if [ $? -ne 0 ]; then
135 echo "${_myname}: Unable to create configuration." >&2
136 exit 1
137 fi
138
139 if [ -n "$1" ]; then
140 for _file in $*
141 do
142 echo "${_file}" >> "${_playlist}"
143 done
144 else
145 while read _file
146 do
147 echo "${_file}" >> "${_playlist}"
148 done
149 fi
150
151 ${EZSTREAM} ${_quiet} ${_rtstatus} ${_verbose} ${_pidfile} ${_pidfile_arg} \
152 -c "${_cfg}"
153
154 exit $?