checkRoot() { [ -n "$DONT_CHECK_ROOT" ] && return if [ "`$ID -u`" != "0" ]; then echo echo "`eval_gettext \"This script must be executed with root privileges.\"`" echo exit 1 fi } envInit(){ if [ -f /usr/xpg4/bin/awk ]; then AWK=/usr/xpg4/bin/awk else AWK=`which awk` [ -x "$AWK" ] || AWK=awk fi if [ -f /usr/xpg4/bin/sed ]; then SED=/usr/xpg4/bin/sed else SED=`which sed` [ -x "$SED" ] || SED=sed fi if [ -f /usr/xpg4/bin/grep ]; then GREP=/usr/xpg4/bin/grep else GREP=`which grep` [ -x "$GREP" ] || GREP=grep fi if [ -f /usr/xpg4/bin/egrep ]; then EGREP=/usr/xpg4/bin/egrep else EGREP=`which egrep` [ -x "$EGREP" ] || EGREP=egrep fi if [ -f /usr/xpg4/bin/id ]; then ID=/usr/xpg4/bin/id else ID=`which id` [ -x "$ID" ] || ID=id fi } BackupFile() { if [ -f "$1" ] ; then if [ -f "$1.drwebsave" ] ; then CONF_DRWEBSAVE="yes" TIMESTAMP=`date "+%Y%m%d-%k_%M_%S"` echo "Info: $1.drwebsave exists. Saving $1 as $1.$TIMESTAMP" cp -p "$1" "$1.$TIMESTAMP" if [ $? -ne 0 ] ; then echo "Error: can't backup $1!" return 3 fi fi if [ "$CONF_DRWEBSAVE" != "yes" ] ; then cp -p "$1" "$1.drwebsave" if [ $? -ne 0 ] ; then echo "Error: can't backup $1!" return 2 else echo "Success: backup file $1 is saved to $1.drwebsave." fi fi else echo "" echo "Error: can't find file $1 for backup!" echo "" return 1 fi return 0; } showyesno() { printf_str="$1" shift retval=0 while true ; do printf "$printf_str (YES/no) " "$*" 2>/dev/null read yesno case "$yesno" in y | yes | Y | Yes | YES | "") retval=0 echo "yes" break ;; n | no | N | No | NO) retval=1 echo "no" break ;; *) echo "`eval_gettext \"Please enter yes or no.\"`" ;; esac done return $retval } shownoyes() { printf_str="$1" shift retval=0 while true ; do printf "$printf_str (yes/NO) " "$*" 2>/dev/null read yesno case "$yesno" in y | yes | Y | Yes | YES) retval=0 echo "yes" break ;; n | no | N | No | NO | "") retval=1 echo "no" break ;; *) echo "`eval_gettext \"Please enter yes or no.\"`" ;; esac done return $retval } # $1 - Message type ("info", "warning" or "error") # $2 - Text (printf string) gui_message() { # Process args. type=$1 printf_str=$2 shift shift message="`printf \"$printf_str\" \"$*\" 2>/dev/null`" # Form window title. StringInList 'info warning error' $type || type=info case "$type" in info) title="`eval_gettext \"Info\"`" ;; warning) title="`eval_gettext \"Warning\"`" ;; error) title="`eval_gettext \"Error\"`" ;; esac # Find some GUI dialog program and form its parameters. if [ -x "`which zenity`" ]; then dialog_program=zenity type_arg="--$type" message_arg="--text \"$message\"" elif [ -x "`which kdialog`" ]; then dialog_program=kdialog case "$type" in info) type_arg='--msgbox' ;; warning) type_arg='--sorry' ;; error) type_arg='--error' ;; esac message_arg="\"$message\"" else dialog_program= fi title_arg="--title $title" # Show the message. echo \* echo \* "$title: $message" echo \* if [ -n "$dialog_program" -a -n "$DISPLAY" ]; then eval "$dialog_program $title_arg $type_arg $message_arg" fi } checkNum() { [ 1 -eq "$1" ] 2>/dev/null return $? } MakeTmpFile() { sc_name=`basename $0` tm_file=`mktemp /tmp/$sc_name.XXXXXXXXXX` chmod 644 $tm_file echo $tm_file } # Returns 0 if there is specified key in specified section # of ini-type config file. Returns 1 otherwise. # # $1 - config file # $2 - section name regexp # $3 - key name regexp IniHaveKey() { perl_script=" \$sect='$2'; \$key='$3'; open FILE, \"$1\"; while () { next unless /^\\s*\[\$sect\]/; while () { last if /^\\s*\[/; exit 0 if /^\\s*\$key\\s*=/; } } exit 1;" perl -e "$perl_script" } # Prints list of values of some key of specified section # of ini-type config file. # # $1 - config file # $2 - section name regexp # $3 - key name regexp IniKeyValues() { perl_script=" \$sect='$2'; \$key='$3'; open FILE, \"$1\"; while () { next unless /^\\s*\[\$sect\]/; while () { last if /^\\s*\[/; (\$val)=(/^\\s*\$key\\s*=\\s*(.*)/); next if !\$val; \$val=~s/\\s*,\\s*/ /g; \$keys.=\" \$val\"; } } \$keys=~s/^\\s*(.*)\\s*\$/\$1/; print \"\$keys\\n\";" perl -e "$perl_script" } # Executes some Perl code for each line of specified section # of ini-type config file. # # $1 - config file # $2 - section name regexp # $3 - perl code IniSectProc() { TR_SP_SCRIPT=" \$sect='$2'; open FILE, \$ARGV[0]; while () { print; next unless /^\\s*\[\$sect\]/; while () { if (/^\\s*\[/) { print; last } $3 print; } }" tmpfile=`MakeTmpFile` perl -e "$TR_SP_SCRIPT" $1 > $tmpfile || return 1 mv -f $tmpfile $1 } AskUpdateConfig() { old_config_file=$1 new_config_file=$2 dont_ask=$3 if diff $old_config_file $new_config_file >/dev/null; then echo "$old_config_file is up-to-date, it is not necessary to modify it." rm -f $new_config_file else if [ -z "$dont_ask" ]; then echo echo "Your $old_config_file is about to be modified." echo "Backup of the original would be kept." echo "Here's the diff:" echo diff -u $old_config_file $new_config_file && true echo showyesno 'Do you agree with these changes?' else true fi if [ "$?" = "0" ]; then BackupFile $old_config_file mv -f $new_config_file $old_config_file echo "Your $old_config_file has been altered by this script." echo "The original has been backed up." else rm -f $new_config_file fi fi } # GNU/Linux have seq. FreeBSD have jot instead of seq. # Solaris have neither seq nor jot. Using Perl as always... # # Usage: Seq Seq() { perl -e "\$,=\" \"; print $1..$2;" } # Checks if string is in the list. # # $1 - list # $2 - string StringInList() { if (echo $1 | grep "^$2[[:space:]]" >/dev/null) || (echo $1 | grep "[[:space:]]$2\$" >/dev/null) || (echo $1 | grep "^$2\$" >/dev/null) || (echo $1 | grep "[[:space:]]$2[[:space:]]" >/dev/null); then return 0 else return 1 fi } envInit checkRoot