set -e
set -u
-export LC_ALL="en_US.utf8"
-export LANG="en_US.utf8"
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+
+if [[ -f "${BIN_DIR}/functions.rc" ]] ; then
+ . "${BIN_DIR}/functions.rc"
+else
+ echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2
+ exit 5
+fi
-VERBOSE="n"
-DEBUG="n"
-QUIET='n'
DO_BACKUP="n"
-SIMULATE='n'
-
-# console colors:
-RED=""
-YELLOW=""
-GREEN=""
-BLUE=""
-CYAN=""
-NORMAL=""
-
-HAS_TTY='y'
-HAS_COLORS="n"
-
-VERSION="0.8"
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
-
RESOLV_CONF="/etc/resolv.conf"
DATE_TPL="%Y-%m-%d_%H:%M:%S"
EXPECTED_PERMS="644"
-#-------------------------------------------------------------------
-detect_color() {
-
- local safe_term="${TERM//[^[:alnum:]]/?}"
- local match_lhs=""
- local use_color="false"
- local term=
-
- if [[ -f ~/.dir_colors ]] ; then
- match_lhs="${match_lhs}$( cat ~/.dir_colors | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//')"
- fi
- if [[ -f /etc/DIR_COLORS ]] ; then
- match_lhs="${match_lhs}$( cat /etc/DIR_COLORS | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//')"
- fi
- if [[ -z ${match_lhs} ]] ; then
- type -P dircolors >/dev/null && \
- match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//')
- fi
- for term in ${match_lhs} ; do
- if [[ "${safe_term}" == ${term} || "${TERM}" == ${term} ]] ; then
- use_color="true"
- break
- fi
- done
-
- # console colors:
- if [ "${use_color}" = "true" ] ; then
- RED="\033[38;5;196m"
- YELLOW="\033[38;5;226m"
- GREEN="\033[38;5;46m"
- BLUE="\033[38;5;27m"
- CYAN="\033[38;5;36m"
- NORMAL="\033[39m"
- HAS_COLORS="y"
- else
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- CYAN=""
- NORMAL=""
- fi
-
- local my_tty=$(tty)
- if [[ "${my_tty}" =~ 'not a tty' ]] ; then
- my_tty='-'
- fi
-
- if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
- HAS_TTY='n'
- fi
-
-}
detect_color
#------------------------------------------------------------------------------
-description() {
+my_description() {
local nr=
local i=
}
+DESCRIPTION=$( my_description )
+
#------------------------------------------------------------------------------
usage() {
cat <<-EOF
- Usage: ${BASENAME} [-d] [[-v] | [-q]]] [-b] [-s] [-S DOMAIN [-S DOMAIN ...]] [-N IP [-N IP ...]] [-O OPTIONS] [--nocolor]
- ${BASENAME} [-h|--help]
- ${BASENAME} [-V|--version]
+ Usage: ${BASE_NAME} [-b] [-S DOMAIN [-S DOMAIN ...]] [-N IP [-N IP ...]] [-O OPTIONS] ${STD_USAGE}
+ ${BASE_NAME} [-h|--help]
+ ${BASE_NAME} [-V|--version]
Options:
-b|--backup Creates a backup of an existing file
Multiple addresses are allowed (and desired).
-O|--options OPTIONS
Options for internal resolver variables, like timeout, rotate a.s.o.
- -s|--simulate Simulation mode, nothing is really done.
- -d|--debug Debug output (bash -x).
- -v|--verbose Set verbosity on. Mutually exclusive to '--quiet'.
- -q|--quiet Quiet execution, only errors and warnings are shown.
- --nocolor Don't use colors on display.
- -h|--help Show this output and exit.
- -V|--version prints out version number of the script and exit
EOF
+
+ echo "${STD_USAGE_MSG}"
}
#------------------------------------------------------------------------------
get_options() {
local tmp=
- local base_dir=
- local short_options='bS:N:O:sdvqhV'
- local long_options='backup,search:,ns:,nameserver:,options:,simulate,'
- long_options+='debug,verbose,quiet,nocolor,help,version'
+ local short_options="bS:N:O:${STD_SHORT_OPTIONS}"
+ local long_options="backup,search:,ns:,nameserver:,options:,${STD_LONG_OPTIONS}"
set +e
- tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
+ tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" )
if [[ $? != 0 ]] ; then
echo "" >&2
usage >&2
# Note the quotes around `$TEMP': they are essential!
eval set -- "${tmp}"
+ eval_common_options "$@"
+ if [[ "${DEBUG}" == 'y' ]] ; then
+ declare -p REMAINING_OPTS
+ declare -p REMAINING_ARGS
+ fi
- local p=
+ local len="${#REMAINING_OPTS[*]}"
+ local i="0"
+ local j=
+ local arg=
local dom=
local ns=
- while true ; do
- case "$1" in
+ while [[ "$i" -lt "${len}" ]] ; do
+
+ arg="${REMAINING_OPTS[$i]}"
+
+ case "${arg}" in
-b|--backup)
DO_BACKUP="y"
- shift
+ i=$(( $i + 1 ))
;;
-S|--search)
- dom=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- SEARCH_DOMAINS+=("${dom}")
- shift
- shift
+ j=$(( $i + 1 ))
+ dom=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ SEARCH_DOMAINS+=( "${dom}" )
+ i=$(( $i + 2 ))
;;
-N|--ns|--nameserver)
- ns=$( echo "$2" | tr '[:upper:]' '[:lower:]' )
- NAME_SERVERS+=("${ns}")
- shift
- shift
+ j=$(( $i + 1 ))
+ ns=$( echo "${REMAINING_OPTS[$j]}" | tr '[:upper:]' '[:lower:]' )
+ NAME_SERVERS+=( "${ns}" )
+ i=$(( $i + 2 ))
;;
-O|--options)
- RESOLV_OPTIONS=$( echo "$2" | sed -e 's/^[ ]*//' -e 's/[ ]*$//' )
- shift
- shift
- ;;
- -d|--debug)
- DEBUG="y"
- shift
- ;;
- -s|--simulate)
- SIMULATE="y"
- shift
+ j=$(( $i + 1 ))
+ RESOLV_OPTIONS=$( echo "${REMAINING_OPTS[$j]}" | sed -e 's/^[ ]*//' -e 's/[ ]*$//' )
+ i=$(( $i + 2 ))
;;
- -v|--verbose)
- VERBOSE="y"
- shift
- ;;
- -q|--quiet)
- QUIET="y"
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- CYAN=""
- NORMAL=""
- HAS_COLORS="n"
- shift
- ;;
- --nocolor)
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- CYAN=""
- NORMAL=""
- HAS_COLORS="n"
- shift
- ;;
- -h|--help)
- description
- echo
- usage
- exit 0
- ;;
- -V|--version)
- echo "${BASENAME} version: ${VERSION}"
- exit 0
- ;;
- --) shift
- break
- ;;
- *) echo "Internal error!"
+ *) echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!"
exit 1
;;
esac
done
- if [[ "${SIMULATE}" == "y" ]] ; then
- local msg=$( cat <<-EOF
-
- ${CYAN}###############################${NORMAL}
- ${CYAN}#${NORMAL} ${YELLOW}Simulation mode${NORMAL} ${CYAN}#${NORMAL}
- ${CYAN}#${NORMAL} Nothing will be really done ${CYAN}#${NORMAL}
- ${CYAN}###############################${NORMAL}
-
- EOF
- )
- echo -e "${msg}"
- fi
-
- if [[ "${DEBUG}" = "y" ]] ; then
- set -x
- fi
- if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
- error "The parameters '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive."
- usage >&2
- exit 1
- fi
-
- local dom=
if [[ "${#SEARCH_DOMAINS[*]}" == 0 ]] ; then
for dom in "${DEFAULT_SEARCH_DOMAINS[@]}" ; do
SEARCH_DOMAINS+=( "${dom}" )
done
fi
- local ns=
if [[ "${#NAME_SERVERS[*]}" == 0 ]] ; then
for ns in "${DEFAULT_NAME_SERVERS[@]}" ; do
NAME_SERVERS+=( "${ns}" )
}
-#########################################
-# Some often used funktions
-
-#------------------------------------------------------------------------------
-my_date() {
- date --rfc-3339=seconds
-}
-
-#------------------------------------------------------------------------------
-debug() {
- if [[ "${VERBOSE}" != "y" ]] ; then
- return 0
- fi
- echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $@"
-}
-
-#------------------------------------------------------------------------------
-info() {
- if [[ "${QUIET}" == "y" ]] ; then
- return 0
- fi
- echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@"
-}
-
-#------------------------------------------------------------------------------
-warn() {
- echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
-}
-
-#------------------------------------------------------------------------------
-error() {
- echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
-}
-
-#------------------------------------------------------------------------------
-empty_line() {
- if [[ "${QUIET}" == "y" ]] ; then
- return 0
- fi
- echo
-}
-
-#------------------------------------------------------------------------------
-exec_cmd() {
-
- local msg="Executing"
- if [[ "${SIMULATE}" == "y" ]] ; then
- msg="Simulating"
- fi
- if [[ "${SIMULATE}" == "y" || "${VERBOSE}" == "y" ]] ; then
- echo "${msg}: $*"
- fi
- if [[ "${SIMULATE}" != "y" ]] ; then
- eval "$@"
- fi
-}
-
-#------------------------------------------------------------------------------
-CP() {
- local cmd="cp"
- if [[ "${VERBOSE}" == "y" ]] ; then
- cmd+=" --verbose"
- fi
- exec_cmd ${cmd} "$@"
-}
-
-#------------------------------------------------------------------------------
-MV() {
- local cmd="mv"
- if [[ "${VERBOSE}" == "y" ]] ; then
- cmd+=" --verbose"
- fi
- exec_cmd ${cmd} "$@"
-}
-
-#------------------------------------------------------------------------------
-check_for_root() {
-
- debug "Checking for execution as root ..."
- local my_uid=$( id -u )
- if [[ "${my_uid}" != 0 ]] ; then
- empty_line
- error "You must be ${RED}root${NORMAL} to execute this script!"
- empty_line
- if [[ "${SIMULATE}" != "y" ]] ; then
- exit 5
- fi
- fi
-
-}
-
#------------------------------------------------------------------------------
do_backup() {
empty_line
info "Generating a new '${GREEN}${RESOLV_CONF}${NORMAL}' ..."
- local content="# Generated by '${BASENAME}' version '${VERSION}' at: $( date --rfc-3339=seconds )"
+ local content="# Generated by '${BASE_NAME}' version '${VERSION}' at: $( date --rfc-3339=seconds )"
content+="
"
main() {
get_options "$@"
- check_for_root
+ set_locale "en_US.utf8"
- echo
+ empty_line
info "Trying to ${CYAN}configure /etc/resolv.conf${NORMAL} ..."
- echo
+ empty_line
+
+ check_for_root
+
+ empty_line
+ empty_line
do_backup
generate