]> git.pld-linux.org Git - projects/rc-scripts.git/blob - hwprofile
- cleanups in sysinit + fix for shutting down bridge
[projects/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 # $Id: hwprofile,v 1.1.2.1 2001/09/30 10:19:34 misiek Exp $
5
6 PROFDIR="/etc/sysconfig/hwprofiles"
7 QUIET=0
8
9 [ -z "${EDITOR} " ] && EDITOR=vi
10
11 if [ "$(id -u)" != "0" ]; then
12         echo "Need superuser privileges. Can't continue!"
13         exit 1
14 fi
15
16 if [ ! -d ${PROFDIR} -o ! -d ${PROFDIR}/data ]; then
17         echo "${PROFDIR} or ${PROFDIR}/data not found. Can't continue!"
18         exit 1
19 fi
20
21 myecho()
22 {
23   if [ "${QUIET}" != "1" ]; then
24     echo $@
25   fi
26 }
27
28 showhelp()
29 {
30 echo '
31 no opts         - show help
32 a               - add profile
33 d               - delete profile
34 r foo           - run specified profile or "default" if such exist
35 s               - save/update running profile
36 f               - find proper profile and run it
37 l               - list configured profiles
38 q               - be quiet (used only in few modes)
39
40 Please send all bug reports to:
41 pld-rc-scripts@pld.org.pl.
42 '
43 }
44
45 # This function probably must be enhanced.
46 md5sumgen()
47 {
48   ( \
49   grep -v "MHz" /proc/cpuinfo 2> /dev/null \
50   cat /proc/pci 2> /dev/null \
51   ) | md5sum | awk ' { gsub(/ .*$/,NIL); print $0 } '
52 }
53
54 while getopts adflsqr: opt "$@"; do
55   case "$opt" in
56     a)
57         echo "Adding new profile..."
58         if [ -f /var/run/hwprofile ]; then
59           echo -n "Current profile is: "
60           cat /var/run/hwprofile
61         fi
62         PROFILE=
63         while [ -z "${PROFILE}" ]; do
64           echo -n "Enter profile name: "
65           read PROFILE
66           if [ -f ${PROFDIR}/${PROFILE}.md5 ]; then
67             echo "Profile ${PROFILE} exist. Try other name."
68             PROFILE=
69           fi
70         done
71         rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
72         md5sumgen > ${PROFDIR}/${PROFILE}.md5
73         LANG=C LC_ALL=C date > ${PROFDIR}/${PROFILE}.date
74         mkdir -p ${PROFDIR}/data/${PROFILE}/
75         chmod 700 ${PROFDIR}/data/${PROFILE}/
76         echo -n "Enter profile description (PROFDIR/${PROFILE}.desc): "
77         read DESCRIPTION
78         [ -z "${DESCRIPTION}" ] && DESCRIPTION="${PROFILE}"
79         echo "${DESCRIPTION}" > ${PROFDIR}/${PROFILE}.desc
80         echo "Now, editor will be started and you need to enter list of files"
81         echo "for this profile (later as ${PROFDIR}/${PROFILE}.files)."
82         echo "Press Enter."
83         read
84         ${EDITOR} ${PROFDIR}/${PROFILE}.files
85         FILES=
86         echo "Validating and copying valid files for this profile:"
87         for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
88           if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
89             echo "${file}: invalid, skipping"
90             continue
91           fi
92           bfile=$(basename ${file})
93           echo "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}..."
94           cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
95           FILES="${file}\n${FILES}"
96         done
97         echo -e "${FILES}" > ${PROFDIR}/${PROFILE}.files
98         rm -f ${PROFDIR}/${PROFILE}.*~
99         echo "New profile ready."
100         break
101         ;;
102     d)
103         echo "Deleting existing profile..."
104         PROFILE=
105         while [ -z ${PROFILE} ]; do
106           echo -n "Enter profile name: "
107           read PROFILE
108           if [ ! -f ${PROFDIR}/${PROFILE}.md5 ]; then
109             echo "Profile ${PROFILE} doesn't exist. Try other name."
110             PROFILE=
111           fi
112         done
113         echo -n "Are you sure? [t/N]: "
114         read YN
115         case "${YN}" in
116           y | t )
117                 # ok
118           ;;
119           *)
120                 echo "Exiting then."
121                 exit 1
122           ;;
123         esac
124         rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
125         echo "Done."
126         break
127         ;;
128     r)
129         PROFILE="${OPTARG}"
130         if [ -z "${PROFILE}" -o ! -f ${PROFDIR}/${PROFILE}.files ]; then
131           echo "Can't setup ${PROFILE}. No required data."
132           exit 1
133         fi
134         echo "Setting up profile \"${PROFILE}\"..."
135         for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
136           if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
137             echo "${file}: invalid, skipping"
138             continue
139           fi
140           echo "Copying ${PROFDIR}/data/${PROFILE}/${file} to ${file}..."
141           cp -dp ${PROFDIR}/data/${PROFILE}/${file} ${file}
142         done
143         myecho "Profile \"${PROFILE}\" running."
144         break
145         ;;
146     f)
147         PROFILE=
148         PROFILEMD5=$(md5sumgen)
149         myecho "Trying to find proper profile..."
150         for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
151           prof=$(basename ${prof})
152           CURRENTMD5="$(cat ${PROFDIR}/${prof} 2> /dev/null)"
153           [ -z "${CURRENTMD5}" ] && continue
154           if [ "${CURRENTMD5}" = "${PROFILEMD5}" ]; then
155             PROFILE=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
156             break;
157           fi
158         done
159         if [ -z "${PROFILE}" ]; then
160             if [ ! -f ${PROFDIR}/default.md5 ]; then
161                 echo "Valid profile not found. Starting with current files."
162                 exit 0
163             else
164                 echo "Valid profile not found. Starting with \"default\" profile."
165                 PROFILE=default
166             fi
167         fi
168         myecho "Found profile \"${PROFILE}\" with md5sum \"${PROFILEMD5}\"."
169         $0 -r ${PROFILE}
170         break
171         ;;
172     s)
173         PROFILE=$(cat /var/run/hwprofile 2> /dev/null)
174         if [ -z "${PROFILE}" ]; then
175           echo "No profile currenty running."
176           exit 1
177         fi
178         if [ ! -f ${PROFDIR}/${PROFILE}.files ]; then
179           echo "${PROFILE}.files is missing. Can't continue."
180           exit 1
181         fi
182         echo "Saving/updating current profile..."
183         for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
184           if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
185             echo "${file}: invalid, skipping"
186             continue
187           fi
188           echo "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}"
189           cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
190         done
191         echo "Done."
192         break
193         ;;
194     l)
195         echo "List of configured profiles:"
196         for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
197           prof=$(basename ${prof})
198           profname=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
199           echo    "Name    : ${profname}"
200           echo -n "Desc    : "
201           if [ -f ${PROFDIR}/${profname}.md5 ]; then
202             cat ${PROFDIR}/${profname}.desc
203           else
204             echo "MISSING"
205           fi
206           echo -n "Created : "
207           if [ -f ${PROFDIR}/${profname}.date ]; then
208             cat ${PROFDIR}/${profname}.date
209           else
210             echo "MISSING"
211           fi
212           echo -n "md5sum  : "
213           if [ -f ${PROFDIR}/${profname}.md5 ]; then
214             cat ${PROFDIR}/${profname}.md5
215           else
216             echo "MISSING"
217           fi
218           echo -n "Files   : "
219           if [ -f ${PROFDIR}/${profname}.files ]; then
220             cat ${PROFDIR}/${profname}.files | xargs
221           else
222             echo "MISSING"
223           fi
224           echo
225         done
226         echo "End."
227         break
228         ;;
229     q)
230         QUIET=1
231         ;;
232     *)
233         showhelp
234         exit 1
235         break
236         ;;
237   esac
238 done
239
240 if [ "$#" -le "0" ]; then
241   showhelp
242   exit 1
243 fi
244
245 exit 0
246
247 # This must be last line !
248 # vi:syntax=sh:tw=78:ts=8:sw=2
This page took 0.055952 seconds and 3 git commands to generate.