#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/config_shlib - # shell library for advanced configfile handling # # Copyright (c) 2001-2003 Ansgar Püster # # Creation: 24.12.2003 ap # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # # This library is used by # # /var/install/bin/edit # /var/install/bin/template-backup-config and all it's links # /var/install/bin/template-restore-bconf and all it's links # /var/install/bin/template-restore-dconf and all it's links # /var/install/bin/master-diff-config and all it's links # # include eislib . /var/install/include/eislib defaultd=/etc/default.d configd=/etc/config.d backupd=/etc/backup.d fmask='*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]' #debug='true' #trace='true' gotoyx () { echo -e "\033[$1;$2H\c"; } # # check_backupdir # =============== # # Parameters: $1 name of backup directory # # Returns: 0 success # 1 failure # check_backupdir() { dir=$1 if [ ! -d $dir ] then mkdir $dir [ $? != 0 ] && return 1 chmod og-rw $dir [ $? != 0 ] && return 1 fi return 0 } # # check_config_files # ================== # # Parameters: $1 package name # # Returns: 0 success # 1 failure # check_config_files() { [ "$debug" = 'true' ] && mecho "call check_config_files: $*" [ "$trace" = 'true' ] && set -x package=$1 # count files fcount=`find $backupd -type f -maxdepth 1 -name "$package.$fmask" 2>/dev/null | wc -l` fcount=`echo $fcount` # verify environment variable MAX_BACKUP_CONFIG [ "$MAX_BACKUP_CONFIG" = '' ] && MAX_BACKUP_CONFIG=10 # check number of files if [ "$fcount" -gt "$MAX_BACKUP_CONFIG" ] then # number of files to remove rmcount=`expr $fcount - $MAX_BACKUP_CONFIG` count=0 # find files in sorted order for file in `find $backupd -type f -maxdepth 1 -name "$package.$fmask" | sort` do # remove obsolete files if [ "$count" -lt "$rmcount" ] then rm $file count=`expr $count + 1` else break fi done mecho -info "Backup directory contained $fcount files for package $package ($rmcount removed)." fi set +x } # # backup_config # ============= # # Parameters: $1 package name # $2 file to backup # $3 individal backup suffix # $4 'quiet' - disable screen output # # Returns: 0 backup O.K. # 1 failure # backup_config() { [ "$debug" = 'true' ] && mecho "call backup_config: $*" [ "$trace" = 'true' ] && set -x package=$1 file=$2 suffix=$3 quiet=$4 # check backup directory if ! check_backupdir $backupd then mecho -error "Error creating backup directory $backupd ..." return 1 fi if [ "$suffix" = "" ] then # timestamp to add to filename stamp=`date +%Y-%m-%d-%H-%M-%S` else # individual suffix to add to filename stamp=$suffix fi # do the backup if [ -f $file ] then cp -p $file $backupd/$package.$stamp chmod og-rw $backupd/$package.$stamp if [ "$quiet" != "quiet" ] then mecho -info "Configuration file was saved as $package.$stamp in $backupd." fi else if [ "$quiet" != "quiet" ] then mecho -warn "File $file not found - no backup done" fi fi # check if files have to be deleted check_config_files $package set +x } activate_hint() { mecho -info "Configuration file $configd/$package was overwritten." mecho -info "You have to activate the configuration in order to" mecho -info "let the changes take effect." mecho -info "For most packages an 'Edit Configuration' without any" mecho -info "changes followed by an answer 'yes' to the question" mecho -info "'Activate the new configuration now (y/n)?' will do" mecho -info "this job." } select_file() { # count files fcount=`find $backupd -type f -maxdepth 1 -name "$package.$fmask" 2>/dev/null | wc -l` fcount=`echo $fcount` [ "$omit_file" != '' ] && fcount=`expr $fcount - 1` selected='false' act_count=1 header if [ $fcount -gt 0 ] then row=4 # find files in sorted order for file in `find $backupd -type f -maxdepth 1 -name "$package.$fmask" | sort` do [ "$file" = "$omit_file" ] && continue eval bfile[$act_count]=\$file gotoyx $row 3 mecho $act_count gotoyx $row 8 mecho $file selected='false' # if [ $row = 21 ] if [ $row = 13 ] then while [ $selected = 'false' ] do gotoyx $row 1 question fnumber="" read fnumber case $fnumber in [0-9]*) if [ $fnumber -gt $act_count -o $fnumber -le 0 ] then mecho -error "wrong number: $fnumber" else selected='true' fi ;; Q|q) selected='quit' break ;; '') header row=4 break ;; *) mecho -error "wrong value: $fnumber" ;; esac done fi if [ $selected != 'false' ]; then break fi row=`expr $row + 1` act_count=`expr $act_count + 1` done if [ $selected = 'false' ]; then if [ $row -gt 4 ]; then while [ $selected = 'false' ] do gotoyx $row 1 question fnumber="" read fnumber case $fnumber in [0-9]*) if [ $fnumber -ge $act_count -o $fnumber -le 0 ]; then mecho -error "wrong number: $fnumber" else selected='true' fi ;; Q|q) break;; '') break;; *) mecho -error "wrong value: $fnumber" ;; esac done fi fi else if [ "$omit_file" = '' ]; then mecho -warn "No files for $package found in $backupd." else mecho -warn "Only one file for $package found in $backupd." fi fi }