Uniglot/Bash/header

267 lines
8.8 KiB
Plaintext
Raw Normal View History

2016-08-30 11:32:34 -05:00
#!/bin/bash
2017-09-19 16:38:39 -05:00
## Vars ##
pullcmd='wget --timeout=5 -q -O -'
2016-08-30 13:15:28 -05:00
## Visual Functions ##
# These function creates a visual indicator that a step has happened.
function header () {
tput setaf 1; tput bold; echo $@; tput sgr0; return
}
function errorheader () {
tput setaf 1 1>&2; tput bold 1>&2; echo "ERROR:" $@ 1>&2; tput sgr0 1>&2; return
}
function infoheader() {
tput setaf 3; tput bold; echo $@; tput sgr0; return
}
function colorstrip() {
perl -e 'use strict; use warnings; while(<>) { s/\e\[[\d;]*m//g; print; }'
}
2016-08-30 13:15:28 -05:00
2016-08-30 11:32:34 -05:00
## Configuration reading ##
function param() {
if [ -z "$1" ] || [ ! -f "$1" ] || [ -z "$2" ]; then
2016-08-30 13:15:28 -05:00
errorheader "Configuration file not specified or doesn't exist."
2016-08-30 11:32:34 -05:00
exit 1;
fi
2016-08-30 13:15:28 -05:00
export "$2"="$(grep '^'"$2"'=' "$1" | sed 's#^'"$2"'='"##")"
2016-08-30 11:32:34 -05:00
return
}
2016-08-30 13:15:28 -05:00
## Help ##
# We expect each script to declare its own helptext functions.
## Logging ##
function logstatement() {
if [ -z "$logfile" ]; then
errorheader "Need the logfile environment variable exported."
exit 1
fi
if [ -z "$1" ]; then
errorheader "Need a statement to log."
exit 1
fi
echo "$1" | tee -a "$logfile"
return
2016-08-30 11:32:34 -05:00
}
## Storage functions ##
function addfs() {
if [ -z "$1" ] || [ -z "$2" ] || (echo "$3" | egrep -v -E '^[0-9]{1,4}'); then
echo "Need a filesystem(1), volume group path(2), and size (3)"
return
fi
lvcreate -L $3"G" -n "$(echo $2 | cut -f 4 -d '/')" "$(echo $2 | cut -f 3 -d '/')"
mkfs.ext4 -m 0 -E nodiscard "$2"
tune2fs -e remount-ro "$2"
tune2fs -c 1 "$2"
mkdir "$1"
echo "$2 $1 ext4 defaults 1 2" >> /etc/fstab
mount "$1" &>/dev/null
}
#This function will forcibly remove a filesystem 1 and lv 2 from a VG
function forciblyremove() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Need a filesystem(1) and volume group path(2)"
elif [ -z "$(mount | grep " ""$1"" ")" ]; then
errorheader Filesystem is not mounted -- cannot safely remove mount point.
else
fuser -km "$1"
umount -l "$1"
rm -Rf "$1"
fi
lvremove -f "$2"
sed -i "/\/$(echo $2 | rev | cut -f 1 -d '/' | rev) /d" /etc/fstab
}
## Date Math Functions ##
# These move dates to be used by AT jobs. They accept an date-style argument and return the new date in format YYMMDDHHMM.
#This function returns a new date incremented by (1)seconds
function incrementdate() {
if [ -z "$2" ]; then
currDateS=$(date +%s)
else
currDateS=$(date -d "$2" +%s)
fi
targetDateS=$(($currDateS + $1))
echo $(date -d '@'$targetDateS +%y%m%d%H%M)
}
#This function returns a new date decremented by (1)seconds
function decrementdate() {
if [ -z "$2" ]; then
currDateS=$(date +%s)
else
currDateS=$(date -d "$2" +%s)
fi
targetDateS=$(($currDateS - $1))
echo $(date -d '@'$targetDateS +%y%m%d%H%M)
}
## QA Functions ##
# This is for QA to test sanitizing input
2017-01-05 14:10:57 -06:00
if [ -z "$qapadding" ]; then qapadding=40; fi
2016-08-30 11:32:34 -05:00
function qaunittest {
2017-01-05 14:10:57 -06:00
unset actualreturn;
if [ -z "$1" ]; then
echo Usage: qaunittest title function expectedreturn input inputarguments
exit 1
2016-08-30 11:32:34 -05:00
fi
2017-01-05 14:10:57 -06:00
header="$1"
function="$2"
2016-08-30 11:32:34 -05:00
expectedreturn="$3"
2017-01-05 14:10:57 -06:00
userinput="$4"
fnargs="$5"
2019-04-30 16:07:30 -05:00
cmd="$(printf "source /opt/aninix/Uniglot/Bash/header;%s %s" "$function" "$fnargs")"
2017-01-05 14:10:57 -06:00
actualreturn="`bash -c "$cmd" 2>&1 <<EOM
2016-08-30 11:32:34 -05:00
$(echo -ne $userinput)
EOM
2017-01-05 14:10:57 -06:00
`"
printf "$header";
printf ":%*s" $(($qapadding - $(echo $header | wc -c) - 1)) '['
if [ "$actualreturn" == "$expectedreturn" ]; then
tput setaf 2; printf "PASS"; tput sgr0; printf ']\n';
2016-08-30 11:32:34 -05:00
else
2017-01-05 14:10:57 -06:00
tput setaf 1; printf "FAIL"; tput sgr0; printf '] -- found %s (expected %s) running '\''%s %s < "%s"'\''\n' "$actualreturn" "$expectedreturn" "$function" "$fnargs" "$userinput";
2016-08-30 11:32:34 -05:00
fi
return
}
## Prompt functions ##
## All input should pass through these to ensure it's appropriately sanitized.
## Do not use read statements directly!
## We do not unset the variable ${!1} so that if the script is accepting command-line arguments that are invalid the user is prompted.
export maxattempts=5
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-var() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Need a variable name (1) and a prompt string (2)"
return 1;
fi
inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | egrep -v '^[-0-9a-zA-Z ,.@]{1,50}$' &>/dev/null); do
printf "%s: " "$2";
read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1))
done
return 0;
}
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-var-len() {
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "Need a variable name (1), prompt string (2), and a length (3)"
return 1;
fi
inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9a-zA-Z]{1,$3}$" &>/dev/null); do
printf "%s (Max length: %s): " "$2" "$3";
read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1))
done
return 0;
}
# This function will prompt the user for variable 1 with string 2 until it gets a nonempty answer.
function prompt-password() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Need a variable name (1) and a prompt string (2)"
return 1;
fi
inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || [ $(echo ${!1} | wc -c) -gt 50 ]; do
printf "%s: " "$2";
read -s $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
echo
inputtime=$(($inputtime + 1))
done
return 0;
}
#This function will prompt the user for a variable 1 with string 2 until it gets an answer between min 3 and max 4.
function prompt-num-var() {
if [ -z "$1" ] || [ -z "$2" ] || [ "$3" -ne "$3" ] || [ "$4" -le "$3" ]; then
echo "Need a variable name (1), prompt string (2), min (3), and max (4)";
echo 1: $1;
echo 2: $2;
echo 3: $3;
echo 4: $4;
return 1;
fi
inputtime="0"
#echo "$0 is checking for $1 -- currently set to <${!1}>"
while [ -z "${!1}" ] || (echo "${!1}" | grep -v -E "^[0-9]{1,8}$" &>/dev/null) || [ "${!1}" -lt "$3" ] || [ "${!1}" -gt "$4" ]; do
printf "%s (between %s and %s): " "$2" "$3" "$4";
read $1;
if [ "${!1}" == "OPT" ] || [ $inputtime -gt $maxattempts ]; then exit 0; fi
inputtime=$(($inputtime + 1))
done
return 0;
}
2016-08-30 14:01:10 -05:00
## Torrenting functions ##
# Configure the torrenting engine.
function configuretorrent {
if [ ! -f "$1" ]; then
errorheader "Torrent configuration file not found."
exit 1;
fi
param "$1" torrentengine
param "$1" torrentterms
param "$1" magnetposition
param "$1" torrentdebugging
param "$1" searchunifier
if [ "$torrentdebugging" == "true" ]; then
echo '<'$torrentengine'>'
echo '<'$torrentterms'>'
echo '<'$magnetposition'>'
echo '<'$torrentdebugging'>'
echo '<'$searchunifier'>'
fi
export torrentconfigured="true"
}
# Get a magnet link for downloads.
function getmagnetlink {
if [ -z "$1" ]; then
errorheader "Need search terms";
exit 1;
fi
if [ "$torrentconfigured" != "true" ]; then
errorheader Torrent not configured.
exit 1;
fi
searchstring="$(echo "$1" | sed "s/ /$searchunifier/g")"
searchlink="$(printf "$torrentengine" "$searchstring")"
2017-09-19 16:38:39 -05:00
/bin/bash -c "$pullcmd "$searchlink" &>/dev/null"
if [ $? -ne 0 ]; then
errorheader Could not pull search results.
exit 1
fi
searchresult="$(/bin/bash -c "$pullcmd $searchlink" | bash -c "$torrentterms")"
magnetlink="$(/bin/bash -c "$pullcmd $searchresult" | grep 'magnet:?' | cut -f "$magnetposition" -d \"| head -n 1)"
2016-08-30 14:01:10 -05:00
if [ $torrentdebugging == "true" ]; then
echo '<'$searchstring'>'
echo '<'$searchlink'>'
echo '<'$searchresult'>'
echo '<'$magnetlink'>'
fi
}
2019-04-30 15:53:55 -05:00
### AniNIX Foundation help ###
function findaninixcheckouts {
find /usr/local/src/ -type f -name config -exec egrep -l 'url[[:space:]]=[[:space:]]/srv/foundation|url[[:space:]]=[[:space:]]https://aninix.net|url[[:space:]]=[[:space:]]([a-zA-Z0-9])+@aninix.net' {} \; 2>/dev/null | sed 's#.git/config$##'
}
function aninixgitstatus {
for i in `findaninixcheckouts`; do
infoheader BEGIN REPORT for "$i"
git -C "$i" status
infoheader END REPORT
echo
done
}