bashctrl/bashctrl
Alex A. Naanou 3d54d68ad4 multiple fixes...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2025-07-29 15:13:05 +03:00

326 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/bash
#----------------------------------------------------------------------
#
# Bash control script...
#
# Originally created in the late 90s to early 2Ks by Alex A. Naanou.
#
#
#----------------------------------------------------------------------
# base configuration...
# NOTE: these are overloadable...
# NOTE: spaces in file/dir names are not so good here, avoid if you can =)
[ -z $BASH_DIR ] \
&& export BASH_DIR=~/.bash/
[ -z $BIN_DIR ] \
&& export BIN_DIR=~/bin/
[ -z $RC_DIR ] \
&& export RC_DIR=~/
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
DEFAULT_PLUGINS=(
"alias 50 login"
"alias 50 start"
"alias 50 end"
"alias 50 logout"
"prompt 50 start"
"env 50 start"
)
DISABLED_PLUGINS=(
"bindings 50 login"
"bindings 50 start"
"ssh-agent 50 login"
"bash_completion 99 login"
"bash_completion 99 start"
)
#----------------------------------------------------------------------
# helpers...
# when sourcing a script $0 is set to '-bash'...
if [ "$0" = "-bash" ] ; then
SCRIPT=bashctrl
else
SCRIPT=`basename $0`
fi
function printhelp(){ cat << EOF
Bash control script
Usage::
$SCRIPT COMMAND [ARGS]
supported commands:
-h|--help|help - print this message and exit.
-l|list - list events and installed plugins.
-e|events - list supported events.
-p|plugins - list available plugins.
-s|script - list the plugins in a runnable bash script format.
add PLUGIN PRIO EVT
- install PLUGIN to EVT with PRIO.
if a plugin is already installed, change its settings.
NOTE: use a two digit priority (arbitary digit prios
are not supported by some commands).
del PLUGIN EVT|all
- remove PLUGIN from EVT.
if "all" is given, remove from all events.
setup - setup $SCRIPT directories and defaults.
-y|--yes - yes to all setup questions.
-n|--no - no to all setup questions.
NOTE: -y / -n arguments must be given before the setup.
run EVENT - run event scripts
Examples:
Setup $SCRIPT:
$ $SCRIPT -y setup
Bind prompt plugin to start event
$ $SCRIPT add prompt 50 start
EOF
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ANSWER_ALL=
function confirm(){
local ans
if [ -z $ANSWER_ALL ] ; then
read -p "$1 (Y/n): " ans
else
ans=$ANSWER_ALL
fi
( [ "$ans" == "" ] \
|| [ "$ans" == "y" ] \
|| [ "$ans" == "Y" ] ) \
&& ( [ -z "$2" ] \
|| echo "$2" )
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# XXX should explicitly check for events...
function update(){
local FILE=$1
local NAME=$(basename "$FILE")
NAME=${NAME//./}
NAME=${NAME^^}
local EVT=$2
if [ ! -e $FILE ] \
|| [ -z "`cat $FILE | grep $SCRIPT`" ] ; then
[ -e $FILE ] \
&& echo " Updating: $FILE..." \
|| echo " Generating: $FILE..."
{
echo "if [ -z \$BASHCTRL_${NAME} ] ; then"
echo " BASHCTRL_${NAME}=1"
echo " # Lines added/maintained by $SCRIPT:"
echo " source ${BIN_DIR}/${SCRIPT} run $EVT"
echo "fi"
echo
} >> $FILE
else
echo " Skipping: $FILE"
fi
}
#----------------------------------------------------------------------
# handle arguments...
if [ -z $1 ]; then
printhelp
exit
fi
while true; do
if [ -z $1 ]; then
break
fi
case $1 in
-h|--help|help)
printhelp
break
;;
-l|list)
echo installed plugins:
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo " `basename ${event}`"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
( [ "$plugin" == "*" ] \
|| [ $plugin == "README" ] ) \
&& continue
echo " $plugin"
done
done
break
;;
-e|events)
echo supported events:
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo " `basename ${event}`"
[ -e ${event}/README ] \
&& cat ${event}/README \
&& echo
done
break
;;
-p|plugins)
echo available plugins:
for plugin in ${BASH_DIR}/plugins/* ; do
plugin=`basename $plugin`
[ $plugin == "README" ] \
&& continue
echo " ${plugin}"
done
break
;;
-s|script)
echo '#!/bin/bash'
echo '# this script will setup a bash plugin configuration.'
echo '# generated by: $SCRIPT script'
echo '#'
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo
echo "# event: `basename ${event}`"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
( [ "$plugin" == "*" ] \
|| [ $plugin == "README" ] ) \
&& continue
echo "$SCRIPT add ${plugin/??_/} ${plugin/_*/} `basename ${event}`"
done
done
echo
break
;;
add)
if [[ $# < 4 ]] ; then
echo "$SCRIPT: Error: wrong argument format."
exit 1
fi
file=${BASH_DIR}/events/${4}/${3}_${2}
# remove old version if it exists...
plugin=${BASH_DIR}/plugins/${2}
if [ -r $plugin ]; then
evt=`echo ${BASH_DIR}/events/${4}/*_${2}`
for p in $evt; do
if [ $p == $evt ] && ! [ -e $p ]; then
break
fi
rm $p
done
target=`readlink -f "$plugin"`
(cd `dirname "$file"` \
&& ln -s $target `basename "$file"`)
else
echo "$SCRIPT: Error: plugin \"$2\" does not exist or is not readable."
fi
break
;;
del|delete)
# remove all...
if [ ${3} = all ] || [ ${3} = '' ] ; then
files=`echo ${BASH_DIR}/events/*/*_${2}`
echo "rmoving \"$files\"..."
rm $files
# remove one...
else
files=`echo ${BASH_DIR}/events/${3}/*_${2}`
echo "rmoving \"$files\"..."
rm $files
fi
break
;;
run)
if [ -z $2 ] ; then
echo '$SCRIPT: no EVENT given.' >&2
exit 1
fi
for plugin in ${BASH_DIR}/events/$2/* ; do
[ $plugin == "README" ] \
&& continue
source $plugin $2
done
shift
break
;;
-y|--yes)
ANSWER_ALL=y
;;
-n|--no)
ANSWER_ALL=n
;;
setup)
[ "`pwd`" == "${BASH_DIR}" ] \
&& exit 1
# dirs...
echo Creating directories...
mkdir -p \
${BASH_DIR}/{commands,dat,local,plugins}/ \
${BASH_DIR}/events/{start,login,logout,end}/ \
${BIN_DIR}/
# script...
echo Copying $SCRIPT...
cp $0 ${BASH_DIR}/
target=`readlink -f ${BASH_DIR}/$SCRIPT`
(cd ${BIN_DIR} \
&& ln -sf $target)
# install / update...
if [ -d ./plugins ] ; then
confirm "Found ./plugins directory, install contents?" \
"Installing plugins:" \
&& (for p in ./plugins/* ; do
[ "`basename $p`" != "README" ] \
&& echo " `basename $p`"
done) \
&& cp -Ri ./plugins/* ${BASH_DIR}/plugins/
fi
# plugins...
if confirm "Setup base plugin bindings?" "Installing bindings:" ; then
for plugin in "${DEFAULT_PLUGINS[@]}" ; do
echo " $SCRIPT add $plugin"
$0 add $plugin
done
fi
# bash rc files...
if confirm "Setup configuration files?" ; then
update ${RC_DIR}/.bashrc start
update ${RC_DIR}/.bash_login login
update ${RC_DIR}/.bash_logout logout
# XXX where do we trigger end???
fi
;;
esac
shift
done
#----------------------------------------------------------------------
# vim:set ts=4 sw=4 nowrap spell :