]>
Commit | Line | Data |
---|---|---|
763b88c2 | 1 | #!/bin/sh |
fdc764ae | 2 | # (C) 2001 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL> |
763b88c2 | 3 | # Hardware Profiles for rc-scripts TOTALNEW. |
9ccfa537 | 4 | # $Id$ |
763b88c2 AM |
5 | |
6 | PROFDIR="/etc/sysconfig/hwprofiles" | |
7 | QUIET=0 | |
8 | ||
20dda54a AM |
9 | . /etc/rc.d/init.d/functions |
10 | . /etc/sysconfig/hwprof | |
11 | ||
12 | [ -z "${EDITOR}" ] && EDITOR=vi | |
763b88c2 AM |
13 | |
14 | if [ "$(id -u)" != "0" ]; then | |
1050f687 | 15 | echo "Need superuser privileges. Can't continue!" |
763b88c2 AM |
16 | exit 1 |
17 | fi | |
18 | ||
24a4b5bc AM |
19 | checkprofdir() |
20 | { | |
1050f687 JR |
21 | if [ ! -d ${PROFDIR} -o ! -d ${PROFDIR}/data ]; then |
22 | echo "${PROFDIR} or ${PROFDIR}/data not found. Can't continue!" | |
24a4b5bc | 23 | echo "Check /etc/sysconfig/hwprof and hwprofile -h." |
1050f687 JR |
24 | exit 1 |
25 | fi | |
24a4b5bc | 26 | } |
763b88c2 AM |
27 | |
28 | myecho() | |
29 | { | |
1050f687 JR |
30 | if [ $QUIET -eq 0 ]; then |
31 | echo $@ | |
32 | fi | |
763b88c2 AM |
33 | } |
34 | ||
35 | showhelp() | |
36 | { | |
37 | echo ' | |
24a4b5bc AM |
38 | no opts - show help |
39 | -a - add profile | |
40 | -d - delete profile | |
41 | -r foo - run specified profile or "default" if such exist | |
42 | -s - save/update running profile | |
43 | -f - find proper profile and run it | |
44 | -l - list configured profiles | |
45 | -q - be quiet (used only in few modes) | |
763b88c2 AM |
46 | |
47 | Please send all bug reports to: | |
ec8b15cb | 48 | pld-rc-scripts@pld-linux.org. |
763b88c2 AM |
49 | ' |
50 | } | |
51 | ||
52 | # This function probably must be enhanced. | |
53 | md5sumgen() | |
54 | { | |
1050f687 JR |
55 | ( \ |
56 | grep -v "MHz" /proc/cpuinfo 2> /dev/null \ | |
57 | grep -v "Latency" /proc/pci 2> /dev/null \ | |
58 | ) | md5sum | awk ' { gsub(/ .*$/,NIL); print $0 } ' | |
763b88c2 AM |
59 | } |
60 | ||
24a4b5bc AM |
61 | while getopts adfhlsqr: opt "$@"; do |
62 | case "$opt" in | |
1050f687 JR |
63 | a) |
64 | checkprofdir | |
65 | ||
66 | echo "Adding new profile..." | |
67 | ||
68 | if [ -f /var/run/hwprofile ]; then | |
69 | echo -n "Current profile is: " | |
70 | cat /var/run/hwprofile | |
71 | fi | |
72 | ||
73 | PROFILE= | |
74 | while [ -z "${PROFILE}" ]; do | |
75 | echo -n "Enter profile name: " | |
76 | read PROFILE | |
77 | ||
78 | if [ -f ${PROFDIR}/${PROFILE}.md5 ]; then | |
79 | echo "Profile ${PROFILE} exist. Try other name." | |
80 | PROFILE= | |
24a4b5bc | 81 | fi |
1050f687 JR |
82 | done |
83 | ||
84 | rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/ | |
85 | md5sumgen > ${PROFDIR}/${PROFILE}.md5 | |
86 | LANG=C LC_ALL=C date > ${PROFDIR}/${PROFILE}.date | |
87 | mkdir -p ${PROFDIR}/data/${PROFILE}/ | |
88 | chmod 700 ${PROFDIR}/data/${PROFILE}/ | |
89 | ||
90 | echo -n "Enter profile description (PROFDIR/${PROFILE}.desc): " | |
91 | read DESCRIPTION | |
92 | [ -z "${DESCRIPTION}" ] && DESCRIPTION="${PROFILE}" | |
93 | echo "${DESCRIPTION}" > ${PROFDIR}/${PROFILE}.desc | |
94 | echo "Now, editor will be started and you need to enter list of files" | |
95 | echo "for this profile (later as ${EDITOR} ${PROFDIR}/${PROFILE}.files)." | |
96 | echo "Press Enter." | |
97 | read | |
98 | ||
99 | if [ -f ${PROFDIR}/files -o ! -f ${PROFDIR}/${PROFILE}.files ]; then | |
100 | cat ${PROFDIR}/files > ${PROFDIR}/${PROFILE}.files; | |
101 | fi | |
102 | ${EDITOR} ${PROFDIR}/${PROFILE}.files | |
103 | ||
104 | FILES= | |
105 | echo "Validating and copying valid files for this profile:" | |
106 | for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do | |
107 | if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then | |
108 | echo "${file}: invalid, skipping" | |
109 | continue | |
24a4b5bc | 110 | fi |
1050f687 JR |
111 | |
112 | bfile=$(basename ${file}) | |
113 | echo "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}..." | |
114 | cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/ | |
115 | FILES="${file}\n${FILES}" | |
116 | done | |
117 | ||
118 | echo -e "${FILES}" > ${PROFDIR}/${PROFILE}.files | |
119 | rm -f ${PROFDIR}/${PROFILE}.*~ | |
120 | ||
121 | echo "New profile ready." | |
122 | break | |
123 | ;; | |
124 | d) | |
125 | checkprofdir | |
126 | ||
127 | echo "Deleting existing profile..." | |
128 | ||
129 | PROFILE= | |
130 | while [ -z ${PROFILE} ]; do | |
131 | echo -n "Enter profile name: " | |
132 | read PROFILE | |
133 | ||
134 | if [ ! -f ${PROFDIR}/${PROFILE}.md5 ]; then | |
135 | echo "Profile ${PROFILE} doesn't exist. Try other name." | |
136 | PROFILE= | |
137 | fi | |
138 | done | |
139 | ||
140 | echo -n "Are you sure? [y/N]: " | |
141 | read YN | |
142 | case "${YN}" in | |
143 | y | Y | t | T ) | |
144 | # ok | |
24a4b5bc | 145 | ;; |
1050f687 JR |
146 | *) |
147 | echo "Exiting then." | |
148 | exit 1 | |
24a4b5bc | 149 | ;; |
1050f687 JR |
150 | esac |
151 | ||
152 | rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/ | |
153 | echo "Done." | |
154 | break | |
155 | ;; | |
156 | r) | |
157 | ||
158 | checkprofdir | |
159 | ||
160 | PROFILE="${OPTARG}" | |
161 | ||
162 | if [ -z "${PROFILE}" -o ! -f ${PROFDIR}/${PROFILE}.files ]; then | |
163 | echo "Can't setup ${PROFILE}. No required data." | |
164 | exit 1 | |
165 | fi | |
166 | ||
167 | echo -n "Setting up profile \"${PROFILE}\" " | |
168 | myecho ""; | |
169 | ||
170 | for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do | |
171 | if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then | |
172 | myecho "${file}: invalid, skipping" | |
173 | continue | |
24a4b5bc | 174 | fi |
1050f687 JR |
175 | echo -n "."; |
176 | ||
177 | myecho "Copying ${PROFDIR}/data/${PROFILE}/${file} to ${file}..." | |
178 | cp -dp ${PROFDIR}/data/${PROFILE}/${file} ${file} 2>/dev/null | |
179 | done | |
180 | ||
181 | echo ${PROFILE} > /var/run/hwprofile | |
182 | echo " Done."; | |
183 | myecho "Profile \"${PROFILE}\" running." | |
184 | break | |
185 | ;; | |
186 | f) | |
187 | checkprofdir | |
188 | ||
189 | PROFILE= | |
190 | PROFILEMD5=$(md5sumgen) | |
191 | ||
192 | myecho "Trying to find proper profile..." | |
193 | for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do | |
194 | prof=$(basename ${prof}) | |
195 | CURRENTMD5="$(cat ${PROFDIR}/${prof} 2> /dev/null)" | |
196 | # echo "$prof ${CURRENTMD5} ${PROFILEMD5}"; | |
197 | [ -z "${CURRENTMD5}" ] && continue | |
198 | ||
199 | if [ "${CURRENTMD5}" = "${PROFILEMD5}" ]; then | |
200 | PROFILE=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ') | |
201 | break; | |
202 | fi | |
203 | done | |
204 | ||
205 | if [ -z "${PROFILE}" ]; then | |
206 | if is_yes "${ASKFORPROFILE}"; then | |
207 | echo "Available profiles:"; | |
208 | PS3='Enter profile number: '; | |
209 | LISTA=$(ls ${PROFDIR}/*.desc 2> /dev/null); | |
210 | for i in ${LISTA}; do | |
211 | a=$(basename $i .desc); | |
212 | LISTA1="${a} ${LISTA1}"; | |
213 | done | |
214 | LISTA1="${LISTA1} Default"; | |
215 | ||
216 | select PROFILE in $LISTA1; do | |
217 | if [ -n "${PROFILE}" ]; then | |
218 | break | |
24a4b5bc | 219 | fi |
1050f687 JR |
220 | done |
221 | ||
222 | if [ "${PROFILE}" = "Default" ]; then | |
223 | PROFILE= | |
24a4b5bc AM |
224 | fi |
225 | fi | |
1050f687 | 226 | fi |
20dda54a | 227 | |
1050f687 JR |
228 | if [ -z "${PROFILE}" ]; then |
229 | if [ ! -f ${PROFDIR}/default.md5 ]; then | |
230 | echo "Valid profile not found. Starting with current files." | |
231 | exit 0 | |
232 | else | |
233 | echo "Valid profile not found. Starting with \"default\" profile." | |
234 | PROFILE=default | |
24a4b5bc | 235 | fi |
1050f687 JR |
236 | fi |
237 | ||
238 | if [ ! -z "${PROFILE}" ]; then | |
239 | myecho "Found profile \"${PROFILE}\" with md5sum \"${PROFILEMD5}\"." | |
240 | if [ $QUIET -eq 0 ]; then | |
241 | $0 -r ${PROFILE}; | |
242 | else | |
243 | $0 -qr ${PROFILE}; | |
24a4b5bc | 244 | fi |
1050f687 JR |
245 | fi |
246 | break | |
247 | ;; | |
248 | s) | |
249 | checkprofdir | |
250 | ||
251 | PROFILE=$(cat /var/run/hwprofile 2> /dev/null) | |
252 | ||
253 | if [ -z "${PROFILE}" ]; then | |
254 | echo "No profile currenty running." | |
255 | exit 1 | |
256 | fi | |
257 | ||
258 | if [ ! -f ${PROFDIR}/${PROFILE}.files ]; then | |
259 | echo "${PROFILE}.files is missing. Can't continue." | |
260 | exit 1 | |
261 | fi | |
262 | ||
263 | echo -n "Saving/updating current [${PROFILE}] profile " | |
264 | md5sumgen > ${PROFDIR}/${PROFILE}.md5 | |
265 | myecho ""; | |
266 | for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do | |
267 | echo -n "."; | |
268 | ||
269 | if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then | |
270 | myecho "${file}: invalid, skipping" | |
271 | continue | |
24a4b5bc | 272 | fi |
1050f687 JR |
273 | |
274 | myecho "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}" | |
275 | cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/ | |
276 | done | |
277 | echo " Done." | |
278 | break | |
279 | ;; | |
280 | l) | |
281 | echo "List of configured profiles:" | |
282 | ||
283 | for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do | |
284 | prof=$(basename ${prof}) | |
285 | profname=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ') | |
286 | echo "Name : ${profname}" | |
287 | echo -n "Desc : " | |
288 | if [ -f ${PROFDIR}/${profname}.md5 ]; then | |
289 | cat ${PROFDIR}/${profname}.desc | |
290 | else | |
291 | echo "MISSING" | |
24a4b5bc | 292 | fi |
1050f687 JR |
293 | echo -n "Created : " |
294 | if [ -f ${PROFDIR}/${profname}.date ]; then | |
295 | cat ${PROFDIR}/${profname}.date | |
296 | else | |
297 | echo "MISSING" | |
298 | fi | |
299 | echo -n "md5sum : " | |
300 | if [ -f ${PROFDIR}/${profname}.md5 ]; then | |
301 | cat ${PROFDIR}/${profname}.md5 | |
302 | else | |
303 | echo "MISSING" | |
304 | fi | |
305 | echo -n "Files : " | |
306 | if [ -f ${PROFDIR}/${profname}.files ]; then | |
307 | cat ${PROFDIR}/${profname}.files | xargs | |
308 | else | |
309 | echo "MISSING" | |
310 | fi | |
311 | echo | |
312 | done | |
313 | echo "End." | |
314 | break | |
315 | ;; | |
316 | q) | |
317 | QUIET=1; export QUIET; | |
318 | ;; | |
319 | *) | |
320 | showhelp | |
321 | exit 1 | |
322 | break | |
323 | ;; | |
324 | esac | |
763b88c2 AM |
325 | done |
326 | ||
24a4b5bc | 327 | if [ "$#" -le "0" -o "$OPTIND" -eq 1 ]; then |
1050f687 JR |
328 | showhelp |
329 | exit 1 | |
763b88c2 AM |
330 | fi |
331 | ||
332 | exit 0 | |
333 | ||
334 | # This must be last line ! | |
1050f687 | 335 | # vi:syntax=sh |