# cbpp-include.cfg - Variables and functions commonly used in custom scripts for
# CrunchBang GNU/Linux .
# Ported to #!++
# by Ben Young
# Ported to Manjaro
# by Daniel Napora
# Usage: say text [delayAfterText]
say() {
fold -s -w 76 <<< "$1" | sed 's/^/ /' # wraps text nicely and adds two leading spaces
sleep "${2-0}"
}
# Usage: prompt text [Y | N | Q]
prompt() {
local answer prompt default
if [[ ${2^} = Q* ]]; then
#say "$1"
read -srn1 -p "$1" answer
echo
[[ ${answer,} = 'q' ]] && return 0 || return 1
fi
if [[ ! $2 || ${2^} = Y* ]]; then
prompt='Y/n'
default='Y'
elif [[ ${2^} = N* ]]; then
prompt='y/N'
default='N'
fi
while true; do
read -r -p "$1 [$prompt] " answer
[[ ! $answer ]] &&
answer=$default
if [[ ${answer^} = Y* ]]; then
say
return 0
elif [[ ${answer^} = N* ]]; then
say
return 1
fi
done
}
# Check the connection by downloading a file from ftp.debian.org. No disk space used.
# Usage: connectiontest [attempts]
# If attempt count is not specified or 0, then it will loop forever and exit(!) your main program with 1 exit status.
connectiontest() {
local TEXT_CHECKING='Checking internet connection...'
local TEXT_FAILED='Internet connection test failed!'
local TEXT_ASK_RETRY=$'\n\nThis script requires a working internet connection. Please configure your internet connection, then hit any key to continue, else hit "q" to quit.'
local TEXT_ABORT='Script aborted.'
local TEXT_OK='Internet connection test passed!'
local -i i attempts=${1-0}
for (( i=0; i < attempts || attempts == 0; i++ )); do
say "$TEXT_CHECKING"
if wget -O - 'http://ftp.debian.org/debian/README' &> /dev/null; then
say "$TEXT_OK" 1
return 0
fi
say "$TEXT_FAILED"
if (( i == attempts - 1 )); then # if last attempt
return 1
elif prompt "$TEXT_ASK_RETRY" Q; then # if user wants to quit
say "$TEXT_ABORT" 2
(( attempts == 0 )) && exit 1 || return 1
fi
clear
done
}
menuStart() {
echo ' '
}
# Usage: menuItem label command
menuItem() {
echo " - "
echo ' '
echo ' '
echo " $2"
echo ' '
echo ' '
echo '
'
}
# Usage: menuSeparator [label]
menuSeparator() {
if [[ $1 ]]; then
echo " "
else
echo ' '
fi
}
# Usage menuSubmenu id label # http://openbox.org/wiki/Help:Menus
menuSubmenu() {
echo " '
}
menuEnd() {
echo ' '
}
# Usage: promptInstall title description package...
promptInstall() {
while true; do # Repeat until there are no errors
if [[ $TRYAGAIN ]]; then # previous try failed
say
say "There was a problem installing ${2,,}."
say
prompt ' Hit any key to try again, or "q" to quit...' Q && return 1
fi
local TRYAGAIN=true
clear
say
say "INSTALL ${1^^}"
say '------------------------'
say "This script will install ${2,,}."
say
prompt ' Run the installer now?' || return 0
clear
connectiontest || continue
clear
say 'Updating sources...' 1
sudo pacman -Syu
clear
say 'Installing package...' 1
sudo pacman -S "${@:3}" || continue
clear
say
say "${2^} has been installed successfully."
say
say 'Hit any key to exit...'
read -srn1
return 0
done
}