From: Frank Brehm Date: Wed, 8 Dec 2021 17:35:23 +0000 (+0100) Subject: Moving lib/functions.rc -> scripts/functions.rc X-Git-Tag: 0.4.1~4^2~51 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=f630ca4df7e7ad6d07e871cef7d7dde4faa46215;p=pixelpark%2Fpp-admin-tools.git Moving lib/functions.rc -> scripts/functions.rc --- diff --git a/scripts/functions.rc b/scripts/functions.rc new file mode 100644 index 0000000..99a4ee7 --- /dev/null +++ b/scripts/functions.rc @@ -0,0 +1,341 @@ +#!/bin/bash + +HAS_TTY='y' + +# Defining colors +RED="" +YELLOW="" +GREEN="" +BLUE="" +CYAN="" +NORMAL="" + +VERSION="0.2.1" + +STD_SHORT_OPTIONS="sdvhV" +STD_LONG_OPTIONS="simulate,debug,verbose,nocolor,help,version" +STD_USAGE_MSG=$( cat <<-EOF + -s|--simulate Simulation mode - dont apply any changes. + -d|--debug Debug output (bash -x). + -v|--verbose Set verbosity on. + --nocolor Dont use colors on display. + -h|--help Show this output and exit. + -V|--version prints out version number of the script and exit + EOF + ) + +# Standard global variables +VERBOSE="n" +DEBUG="n" +DO_ASK="n" +SIMULATE="n" + +declare -a REMAINING_ARGS=() +declare -a REMAINING_OPTS=() + +NFS_HOMEDIR_PARENT='/mnt/nfs/home' + +DESCRIPTION="${DESCRIPTION:-Failing script description}" + +# LDAP Defaults +LDAP_USR="cn=admin" +LDAP_PWD_FILE="${HOME}/.private/ldap-admin-wonl.txt" +LDAP_BASE="o=isp" +LDAP_HOST="ldap.pixelpark.com" +LDAP_PORT=389 + +LDAP_STD_OPTS_SHORT="D:y:H:P:b:" +LDAP_STD_OPTS_LONG="bind-dn:,password-file:,ldap-host:,ldap-port:,base-dn:" + +LDAP_USAGE_MSG=$( cat <<-EOF + -D|--bind-dn DN + Use this Distinguished Name DN to bind to the LDAP directory. + (Default: '${LDAP_USR}'). + -y|--password-file FILE + Use complete contents of PASSWD_FILE as the password for simple authentication + (Default: '${LDAP_PWD_FILE}'). + -H|--ldap-host HOSTNAME + The hostname or IP address of the LDAP-Server (Default: '${LDAP_HOST}'). + -P|--ldap-port PORT + The port number of the LDAP-Server (Default: ${LDAP_PORT}). + -b|--base-dn SEARCH_BASE + The starting point for the LDAP search (Default: '${LDAP_BASE}') + EOF + ) + +#------------------------------------------------------------------- +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;14m" + NORMAL="\033[39m" + 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 + +} + +#------------------------------------------------------------------------------ +description() { + echo -e "${DESCRIPTION}" +} + +#------------------------------------------------------------------------------ +eval_common_options() { + + REMAINING_ARGS=() + REMAINING_OPTS=() + + if [[ "$#" -gt 0 ]] ; then + while true ; do + case "$1" in + -s|--simulate) + SIMULATE="y" + shift + ;; + -d|--debug) + DEBUG="y" + shift + ;; + -v|--verbose) + VERBOSE="y" + shift + ;; + --nocolor) + RED="" + YELLOW="" + GREEN="" + BLUE="" + CYAN="" + NORMAL="" + shift + ;; + -h|--help) + description + echo + usage + exit 0 + ;; + -V|--version) + echo "${BASE_NAME} version: ${VERSION}" + exit 0 + ;; + --) shift + break + ;; + *) REMAINING_OPTS+=($1) + shift + ;; + esac + done + fi + + if [[ "${DEBUG}" = "y" ]] ; then + set -x + fi + + if [[ "$#" -gt "0" ]] ; then + REMAINING_ARGS=("--") + while [[ "$#" -gt "0" ]] ; do + REMAINING_ARGS+=($1) + shift + done + fi + + if [[ "${SIMULATE}" == "y" ]] ; then + echo + echo -e "${CYAN}Simulation mode!${NORMAL}" + echo "Nothing is really done." + echo + fi + +} + +#------------------------------------------------------------------------------ +eval_ldap_options() { + + REMAINING_ARGS=() + REMAINING_OPTS=() + + if [[ "$#" -gt 0 ]] ; then + while true ; do + case "$1" in + -D|--bind-dn) + LDAP_USR="$2" + shift + shift + ;; + -y|--password-file) + LDAP_PWD_FILE="$2" + shift + shift + ;; + -H|--ldap-host) + LDAP_HOST="$2" + shift + shift + ;; + -P|--ldap-port) + LDAP_PORT="$2" + shift + shift + ;; + -b|--base-dn) + LDAP_BASE="$2" + shift + shift + ;; + --) shift + break + ;; + *) REMAINING_OPTS+=($1) + shift + ;; + esac + done + fi + + if [[ "$#" -gt "0" ]] ; then + REMAINING_ARGS=("--") + while [[ "$#" -gt "0" ]] ; do + REMAINING_ARGS+=($1) + shift + done + fi + + if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then + error "Password file '${RED}${LDAP_PWD_FILE}'${NORMAL} not found." + exit 3 + fi + + if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then + error "Password file '${RED}${LDAP_PWD_FILE}${NORMAL}' not readable." + exit 3 + fi + +} + +#------------------------------------------------------------------------------ +my_date() { + date --rfc-3339=seconds +} + +#------------------------------------------------------------------------------ +debug() { + if [[ "${VERBOSE}" != "y" ]] ; then + return 0 + fi + echo -e " * [$(my_date)] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +info() { + echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +warn() { + echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 +} + +#------------------------------------------------------------------------------ +error() { + echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2 +} + +#------------------------------------------------------------------------------ +RM() { + + if [[ "${SIMULATE}" == "y" ]] ; then + debug "Simulated removing of: $*" + return + fi + if [[ "${VERBOSE}" != "y" ]] ; then + rm "$@" + else + rm --verbose "$@" + fi + +} + +#------------------------------------------------------------------------------ +set_locale() { + + local new_locale="$1" + local loc= + local found="n" + + local oifs="${IFS}" + IFS=" +" + for loc in $( locale -a ); do + if [[ "${loc}" == "${new_locale}" ]] ; then + found="y" + break + fi + done + IFS="${oifs}" + + if [[ "${found}" != "y" ]] ; then + error "Locale '${RED}${new_locale}${NORMAL}' not found." + else + LANG="${new_locale}" + LC_ALL= + LC_CTYPE="${new_locale}" + LC_NUMERIC="${new_locale}" + LC_TIME="${new_locale}" + LC_COLLATE="${new_locale}" + LC_MONETARY="${new_locale}" + LC_MESSAGES="${new_locale}" + LC_PAPER="${new_locale}" + LC_NAME="${new_locale}" + LC_ADDRESS="${new_locale}" + LC_TELEPHONE="${new_locale}" + LC_MEASUREMENT="${new_locale}" + LC_IDENTIFICATION="${new_locale}" + fi + +} + +# vim: filetype=sh ts=4 et list