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