2020-11-03 12:07:49 -08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
BLOCK_FILE=""
|
|
|
|
BLOCK_NAME='BLOCK'
|
|
|
|
BLOCK_COMMENT_CHAR="#"
|
|
|
|
# a comment added before the block
|
|
|
|
BLOCK_DESCRIPTION=""
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
function __update_block () {
|
2020-11-03 12:07:49 -08:00
|
|
|
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"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
# set block to defaults
|
|
|
|
__update_block
|
2020-11-03 12:07:49 -08:00
|
|
|
# echo -e "default block template follows use: block-set before block-add to change>\n" $BLOCK
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
function set_block () {
|
2020-11-03 12:07:49 -08:00
|
|
|
|
|
|
|
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 ))
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
__update_block
|
2020-11-03 12:07:49 -08:00
|
|
|
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 -------
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
function add_block () {
|
2020-11-03 12:07:49 -08:00
|
|
|
# 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
|
2020-11-06 14:24:10 -08:00
|
|
|
# echo Where $WHERE
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/'"${WHERE}"'/{s/.*/&\n'"${ADD}"'/;h};${x;/^$/{s//'"$ADD"'/;H};x}' "${BLOCK_FILE}"
|
|
|
|
else
|
2020-11-06 14:24:10 -08:00
|
|
|
echo "no file set in which to add block, use: ${RED_COLOR}set-block -f <file path>"
|
2020-11-03 12:07:49 -08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-11-07 10:01:17 -08:00
|
|
|
list_block () {
|
2020-11-06 14:24:10 -08:00
|
|
|
if [[ $1 =~ -?r(aw)? ]]; then
|
|
|
|
echo $BLOCK
|
|
|
|
else
|
|
|
|
local block
|
|
|
|
block=${BLOCK##'\n'}'\n'
|
|
|
|
printf "$block"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_block_file () {
|
|
|
|
[ $BLOCK_FILE ] && echo ${BLOCK_FILE}
|
|
|
|
[[ $1 = -v ]] && echo "no output file set, use set_block -f <file-path>"
|
|
|
|
}
|
|
|
|
|
|
|
|
modify_block () {
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
/'"${BLOCK_BEGIN}"'/b
|
|
|
|
/'"${BLOCK_END}"'/b
|
|
|
|
s/'"${1}"'/'"${2}"'/
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_remove_leading () {
|
|
|
|
printf "%s\n" "$(tac $BLOCK_FILE \
|
|
|
|
| sed '/'"${BLOCK_BEGIN}"'/,/^./{ /^[ \t]*$/d}' | tac)" > $BLOCK_FILE
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_block () {
|
|
|
|
[[ "$BLOCK_DESCRIPTION" ]] || sed -ni '/'"${BLOCK_BEGIN}"'/{x;d;};1h;1!{x;p;};${x;p;}' "${BLOCK_FILE}"
|
|
|
|
block_remove_leading
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
d
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_remove_content () {
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
/'"${BLOCK_BEGIN}"'/b
|
|
|
|
/'"${BLOCK_END}"'/b
|
|
|
|
d
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_add_line () {
|
2020-11-03 12:07:49 -08:00
|
|
|
# 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}"
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_remove_line () {
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
/'"${1}"'/d
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_change_line () {
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
/'"${1}"'/c\'"${2}"'
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
block_modify_line () {
|
2020-11-03 12:07:49 -08:00
|
|
|
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
|
|
|
|
/'"${BLOCK_BEGIN}"'/b
|
|
|
|
/'"${BLOCK_END}"'/b
|
|
|
|
/'"${1}"'/{s/'"${2}"'/'"${3}"'/}
|
|
|
|
}' "${BLOCK_FILE}"
|
|
|
|
}
|