148 lines
3.3 KiB
Bash
148 lines
3.3 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
BLOCK_FILE=""
|
||
|
BLOCK_NAME='BLOCK'
|
||
|
BLOCK_COMMENT_CHAR="#"
|
||
|
# a comment added before the block
|
||
|
BLOCK_DESCRIPTION=""
|
||
|
|
||
|
function __update () {
|
||
|
if [ ! -z "$BLOCK_FILE" ]; then
|
||
|
touch $BLOCK_FILE
|
||
|
tail -c1 $BLOCK_FILE | read -r _ || echo >> $BLOCK_FILE
|
||
|
fi
|
||
|
BLOCK_BEGIN="$BLOCK_COMMENT_CHAR --- ${BLOCK_NAME} BEGIN ---"
|
||
|
BLOCK_END="$BLOCK_COMMENT_CHAR --- ${BLOCK_NAME} END ---"
|
||
|
BLOCK="\n$([ -z "$BLOCK_DESCRIPTION" ] || echo "${BLOCK_COMMENT_CHAR} ${BLOCK_DESCRIPTION}\n")${BLOCK_BEGIN}\n${BLOCK_END}"
|
||
|
# for testing
|
||
|
# BLOCK="a line before\n${BLOCK}\nanother line outside"
|
||
|
|
||
|
}
|
||
|
|
||
|
__update
|
||
|
# echo -e "default block template follows use: block-set before block-add to change>\n" $BLOCK
|
||
|
|
||
|
function set-block () {
|
||
|
|
||
|
declare OPTION
|
||
|
declare OPTARG
|
||
|
declare OPTIND
|
||
|
|
||
|
while getopts ':f:c:d:n:' OPTION; do
|
||
|
echo $OPTION $OPTARG
|
||
|
case "$OPTION" in
|
||
|
f)
|
||
|
BLOCK_FILE=$OPTARG
|
||
|
echo File set to $BLOCK_FILE
|
||
|
;;
|
||
|
c)
|
||
|
BLOCK_COMMENT_CHAR=$OPTARG
|
||
|
;;
|
||
|
d)
|
||
|
BLOCK_DESCRIPTION=$OPTARG
|
||
|
;;
|
||
|
n)
|
||
|
BLOCK_NAME=$OPTARG
|
||
|
;;
|
||
|
*)
|
||
|
echo unknown option $OPTION
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
|
||
|
shift $(( OPTIND - 1 ))
|
||
|
|
||
|
__update
|
||
|
echo -e "new block template follows use: block-add >\n" $BLOCK
|
||
|
echo ----
|
||
|
echo $([ -e "$BLOCK_FILE" ] || echo "Warning: No output file set yet, use block-set -f <path>" && echo "block will be placed in $BLOCK_FILE")
|
||
|
echo -------
|
||
|
}
|
||
|
|
||
|
|
||
|
function add-block () {
|
||
|
# only add if it doesn't exist, existance is only based on block begin
|
||
|
local ADD
|
||
|
ADD=$(echo "${BLOCK}" | sed 's/\//\\\//g')
|
||
|
if [ -e "$BLOCK_FILE" ]; then
|
||
|
sed -n '/^[ \t]*'"${BLOCK_BEGIN}"'/{q50}' "${BLOCK_FILE}"
|
||
|
[ $? == 50 ] && return
|
||
|
WHERE=${1-${BLOCK_BEGIN}} # default is end
|
||
|
echo Where $WHERE
|
||
|
sed -i '/'"${WHERE}"'/{s/.*/&\n'"${ADD}"'/;h};${x;/^$/{s//'"$ADD"'/;H};x}' "${BLOCK_FILE}"
|
||
|
else
|
||
|
echo "no file set in which to add block, use: set-block -f <file path>"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
modify-block () {
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
/'"${BLOCK_BEGIN}"'/b
|
||
|
/'"${BLOCK_END}"'/b
|
||
|
s/'"${1}"'/'"${2}"'/
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
remove-block () {
|
||
|
[ -z "$BLOCK_DESCRIPTION" ] || sed -ni '/'"${BLOCK_BEGIN}"'/{x;d;};1h;1!{x;p;};${x;p;}' "${BLOCK_FILE}"
|
||
|
remove-block-leading
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
d
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
remove-block-leading () {
|
||
|
printf "%s\n" "$(tac $BLOCK_FILE \
|
||
|
| sed '/'"${BLOCK_BEGIN}"'/,/^./{ /^[ \t]*$/d}' | tac)" > $BLOCK_FILE
|
||
|
}
|
||
|
|
||
|
block-remove-content () {
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
/'"${BLOCK_BEGIN}"'/b
|
||
|
/'"${BLOCK_END}"'/b
|
||
|
d
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
|
||
|
block-add-line () {
|
||
|
# echo argument count $#, $1, $2, $3
|
||
|
local ADD
|
||
|
ADD="/^${BLOCK_BEGIN}/a\\${1}"
|
||
|
if [ $# -gt 1 ]; then
|
||
|
if [ "$1" == "--end" ];then
|
||
|
ADD="/${BLOCK_END}/i\\${2}"
|
||
|
else
|
||
|
if [ "$1" == "--before" ];then
|
||
|
ADD="/${2}/i\\${3}"
|
||
|
else
|
||
|
ADD="/${1}/a\\${2}"
|
||
|
fi
|
||
|
fi
|
||
|
fi
|
||
|
# echo $ADD
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
'"${ADD}"'
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
block-remove-line () {
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
/'"${1}"'/d
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
block-change-line () {
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
/'"${1}"'/c\'"${2}"'
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|
||
|
|
||
|
block-modify-line () {
|
||
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
||
|
/'"${BLOCK_BEGIN}"'/b
|
||
|
/'"${BLOCK_END}"'/b
|
||
|
/'"${1}"'/{s/'"${2}"'/'"${3}"'/}
|
||
|
}' "${BLOCK_FILE}"
|
||
|
}
|