#!/bin/bash # ------- # File: lliurex-dmrc # Description: LliureX /etc/skel/.dmrc manager # Author: Luis Garcia Gisbert # # 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 program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e PATH="/usr/sbin:/usr/bin:/sbin:/bin" TARGET_FILE="/etc/skel/.dmrc" TEMPLATE_FILE="/usr/share/lliurex-base-files/skel/.dmrc" REG_EXP="^\([[:alnum:]_]\+\)/\([[:alnum:]_]\+\)=\([-[:alnum:]_]\+\)$" REG_EXP_NOVALUE="^\([[:alnum:]_]\+\)/\([[:alnum:]_]\+\)$" # functions usage() { VERBOSE="Y" CMD_NAME="$(basename "$0")" echo "Usage: $CMD_NAME {create|remove}" >&2 echo " $CMD_NAME add SECTION/VAR=VALUE" >&2 echo " $CMD_NAME del SECTION/VAR" >&2 exit 1 } create_target(){ [ -w "$TARGET_FILE" ] || cat "$TEMPLATE_FILE" |install_target return 0 } install_target(){ mkdir -p "$(dirname "$TARGET_FILE")" TMP_FILE="$(tempfile)" cat - > "$TMP_FILE" chmod 644 "$TMP_FILE" mv -f "$TMP_FILE" "$TARGET_FILE" } change_var(){ SEC_NAME="$1" VAR_NAME="$2" VAR_VALUE="$3" sed -e "/^\[$SEC_NAME\]/,/^\[/{/^$VAR_NAME=/s%=.*$%=$VAR_VALUE%}" "$TARGET_FILE" } add_var(){ SEC_NAME="$1" VAR_NAME="$2" VAR_VALUE="$3" sed -e "/^\[$SEC_NAME\]/a$VAR_NAME=$VAR_VALUE" "$TARGET_FILE" } del_var(){ SEC_NAME="$1" VAR_NAME="$2" sed -e "/^\[$SEC_NAME\]/,/^\[/{/^$VAR_NAME=/d}" "$TARGET_FILE" } add_section(){ SEC_NAME="$1" sed -e "\$a[$SEC_NAME]" "$TARGET_FILE" } test_section(){ SEC_NAME="$1" grep -q "^\[$SEC_NAME\]" "$TARGET_FILE" || return 1 return 0 } test_var(){ SEC_NAME="$1" VAR_NAME="$2" sed -ne "/^\[$SEC_NAME\]/,/^\[/p" "$TARGET_FILE" |grep -q "^$VAR_NAME=" "$TARGET_FILE" || return 1 return 0 } # ---- # main # ---- ACTION="$1" [ "$ACTION" ] || usage echo " add del create remove " |grep -q " $ACTION " || usage shift case "$ACTION" in add) echo "$1" |grep -q "$REG_EXP" || usage SEC_NAME="$(echo "$1" |sed -ne "s%$REG_EXP%\1%p")" VAR_NAME="$(echo "$1" |sed -ne "s%$REG_EXP%\2%p")" VAR_VALUE="$(echo "$1" |sed -ne "s%$REG_EXP%\3%p")" [ "$SEC_NAME" -a "$VAR_NAME" -a "$VAR_VALUE" ] || usage create_target test_section "$SEC_NAME" || add_section "$SEC_NAME" |install_target if ! test_var "$SEC_NAME" "$VAR_NAME" ; then add_var "$SEC_NAME" "$VAR_NAME" "$VAR_VALUE" |install_target else change_var "$SEC_NAME" "$VAR_NAME" "$VAR_VALUE" |install_target fi ;; create) create_target ;; remove) rm -f "$TARGET_FILE" ;; del) echo "$1" |grep -q "$REG_EXP_NOVALUE" || usage SEC_NAME="$(echo "$1" |sed -ne "s%$REG_EXP_NOVALUE%\1%p")" VAR_NAME="$(echo "$1" |sed -ne "s%$REG_EXP_NOVALUE%\2%p")" if test_section "$SEC_NAME" ; then del_var "$SEC_NAME" "$VAR_NAME" |install_target fi ;; esac exit 0