2 # (C) 2001 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
3 # Hardware Profiles for rc-scripts TOTALNEW.
5 PROFDIR="/etc/sysconfig/hwprofiles"
8 . /etc/rc.d/init.d/functions
9 . /etc/sysconfig/hwprof
11 [ -z "${EDITOR}" ] && EDITOR=vi
13 if [ "$(id -u)" != "0" ]; then
14 echo "Need superuser privileges. Can't continue!"
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."
29 if [ $QUIET -eq 0 ]; then
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)
48 # This function probably must be enhanced.
52 grep -v "MHz" /proc/cpuinfo 2> /dev/null \
53 grep -v "Latency" /proc/pci 2> /dev/null \
54 ) | md5sum | awk ' { gsub(/ .*$/,NIL); print $0 } '
57 while getopts adfhlsqr: opt "$@"; do
62 echo "Adding new profile..."
64 if [ -f /var/run/hwprofile ]; then
65 echo -n "Current profile is: "
66 cat /var/run/hwprofile
70 while [ -z "${PROFILE}" ]; do
71 echo -n "Enter profile name: "
74 if [ -f ${PROFDIR}/${PROFILE}.md5 ]; then
75 echo "Profile ${PROFILE} exist. Try other name."
80 rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
81 md5sumgen > ${PROFDIR}/${PROFILE}.md5
82 LANG=C LC_ALL=C date > ${PROFDIR}/${PROFILE}.date
83 mkdir -p ${PROFDIR}/data/${PROFILE}/
84 chmod 700 ${PROFDIR}/data/${PROFILE}/
86 echo -n "Enter profile description (PROFDIR/${PROFILE}.desc): "
88 [ -z "${DESCRIPTION}" ] && DESCRIPTION="${PROFILE}"
89 echo "${DESCRIPTION}" > ${PROFDIR}/${PROFILE}.desc
90 echo "Now, editor will be started and you need to enter list of files"
91 echo "for this profile (later as ${EDITOR} ${PROFDIR}/${PROFILE}.files)."
95 if [ -f ${PROFDIR}/files -o ! -f ${PROFDIR}/${PROFILE}.files ]; then
96 cat ${PROFDIR}/files > ${PROFDIR}/${PROFILE}.files;
98 ${EDITOR} ${PROFDIR}/${PROFILE}.files
101 echo "Validating and copying valid files for this profile:"
102 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
103 if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
104 echo "${file}: invalid, skipping"
108 bfile=$(basename ${file})
109 echo "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}..."
110 cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
111 FILES="${file}\n${FILES}"
114 echo -e "${FILES}" > ${PROFDIR}/${PROFILE}.files
115 rm -f ${PROFDIR}/${PROFILE}.*~
117 echo "New profile ready."
123 echo "Deleting existing profile..."
126 while [ -z ${PROFILE} ]; do
127 echo -n "Enter profile name: "
130 if [ ! -f ${PROFDIR}/${PROFILE}.md5 ]; then
131 echo "Profile ${PROFILE} doesn't exist. Try other name."
136 echo -n "Are you sure? [y/N]: "
148 rm -rf ${PROFDIR}/${PROFILE}.* ${PROFDIR}/data/${PROFILE}/
158 if [ -z "${PROFILE}" -o ! -f ${PROFDIR}/${PROFILE}.files ]; then
159 echo "Can't setup ${PROFILE}. No required data."
163 echo -n "Setting up profile \"${PROFILE}\" "
166 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
167 if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
168 myecho "${file}: invalid, skipping"
173 myecho "Copying ${PROFDIR}/data/${PROFILE}/${file} to ${file}..."
174 cp -dp ${PROFDIR}/data/${PROFILE}/${file} ${file} 2>/dev/null
177 echo ${PROFILE} > /var/run/hwprofile
179 myecho "Profile \"${PROFILE}\" running."
186 PROFILEMD5=$(md5sumgen)
188 myecho "Trying to find proper profile..."
189 for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
190 prof=$(basename ${prof})
191 CURRENTMD5="$(cat ${PROFDIR}/${prof} 2> /dev/null)"
192 # echo "$prof ${CURRENTMD5} ${PROFILEMD5}";
193 [ -z "${CURRENTMD5}" ] && continue
195 if [ "${CURRENTMD5}" = "${PROFILEMD5}" ]; then
196 PROFILE=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
201 if [ -z "${PROFILE}" ]; then
202 if is_yes "${ASKFORPROFILE}"; then
203 echo "Available profiles:";
204 PS3='Enter profile number: ';
205 LISTA=$(ls ${PROFDIR}/*.desc 2> /dev/null);
206 for i in ${LISTA}; do
207 a=$(basename $i .desc);
208 LISTA1="${a} ${LISTA1}";
210 LISTA1="${LISTA1} Default";
212 select PROFILE in $LISTA1; do
213 if [ -n "${PROFILE}" ]; then
218 if [ "${PROFILE}" = "Default" ]; then
224 if [ -z "${PROFILE}" ]; then
225 if [ ! -f ${PROFDIR}/default.md5 ]; then
226 echo "Valid profile not found. Starting with current files."
229 echo "Valid profile not found. Starting with \"default\" profile."
234 if [ ! -z "${PROFILE}" ]; then
235 myecho "Found profile \"${PROFILE}\" with md5sum \"${PROFILEMD5}\"."
236 if [ $QUIET -eq 0 ]; then
247 PROFILE=$(cat /var/run/hwprofile 2> /dev/null)
249 if [ -z "${PROFILE}" ]; then
250 echo "No profile currenty running."
254 if [ ! -f ${PROFDIR}/${PROFILE}.files ]; then
255 echo "${PROFILE}.files is missing. Can't continue."
259 echo -n "Saving/updating current [${PROFILE}] profile "
260 md5sumgen > ${PROFDIR}/${PROFILE}.md5
262 for file in $(grep -v "^#" ${PROFDIR}/${PROFILE}.files); do
265 if [ ! -f ${file} -o "${file}" = "/etc/sysconfig/hwprof" ]; then
266 myecho "${file}: invalid, skipping"
270 myecho "Copying ${file} to ${PROFDIR}/data/${PROFILE}/${file}"
271 cp -dp --parents ${file} ${PROFDIR}/data/${PROFILE}/
277 echo "List of configured profiles:"
279 for prof in $(ls -1 ${PROFDIR}/*.md5 2> /dev/null); do
280 prof=$(basename ${prof})
281 profname=$(echo ${prof} | awk ' { gsub(/\.md5/,NIL); print $0 } ')
282 echo "Name : ${profname}"
284 if [ -f ${PROFDIR}/${profname}.md5 ]; then
285 cat ${PROFDIR}/${profname}.desc
290 if [ -f ${PROFDIR}/${profname}.date ]; then
291 cat ${PROFDIR}/${profname}.date
296 if [ -f ${PROFDIR}/${profname}.md5 ]; then
297 cat ${PROFDIR}/${profname}.md5
302 if [ -f ${PROFDIR}/${profname}.files ]; then
303 cat ${PROFDIR}/${profname}.files | xargs
313 QUIET=1; export QUIET;
323 if [ "$#" -le "0" -o "$OPTIND" -eq 1 ]; then