void-bash-installer/functions/functions

236 lines
4.4 KiB
Plaintext
Raw Normal View History

2021-03-11 14:04:04 +01:00
wronginput() {
printf "Wrong input: %s\n" "${1}"
printf "this is valid: %s\n" "${2}"
}
input() {
output=""
echo "${1}"
read -r -p "[${2}]: " output
test -z "${output}" && output="${2}"
return 0
}
#multiplechoice "networksystem" "dhcpcd" "networkmanager" "iwd" "none"
multiplechoice() {
output=""
local done=""
local input=("${@}")
for i in $(seq 1 "$(( ${#input[@]} - 1 ))"); do
if [[ "${i}" == "1" ]]; then
choices="${input[$i]}"
else
choices="${choices}|${input[$i]}"
fi
done
while input "[${choices}]: " "${input[1]}"; do
for i in $(seq 1 "$(( ${#input[@]} - 1 ))"); do
if [[ "${output}" == "${input[$i]}" ]] || [[ "${output}" == "${input[$i]:0:2}" ]]; then
setconf add "${1}" "${input[$i]}"
done=1
fi
done
if [[ -z "${done}" ]]; then
echo wronginput "${output}"
echo use these: "${input[@]}"
else
break
fi
done
}
#yesno question y
yesno() {
local input=""
local retval=""
local default=""
local q=""
case "${2}" in
Y|y) q="${1} [Y|n]:"; default=y;;
N|n) q="${1} [y|N]:"; default=n;;
esac
while read -r -p "${q} " input; do
test -z "${input}" && input="${default}"
case "${input}" in
y*|Y*|*es*|*ES*|*Es*|*eS*|1)
retval=1
break
;;
n*|N*|0)
retval=""
break
;;
*) wronginput "${input}" "${choices[*]}"
;;
esac
done
if [[ -z "${retval}" ]]; then
return 1
else
return 0
fi
}
input_pw() {
output=""
local pw1=1
local pw2=2
until [[ "${pw1}" == "${pw2}" ]]; do
read -r -s -p "${1}: " pw1 && echo
read -r -s -p "${1} verify: " pw2 && echo
test -z "${pw1}" && pw1=oem
test -z "${pw2}" && pw2=oem
done
echo password set to "\"oem\""
output="${pw1}"
}
#setconf add lang de_DE
#setconf rm lang
setconf() {
case "${1}" in
add)
cfg+=( ["${2}"]="${3}" )
if grep -q -w "${2}" "${var}"; then
sed -i "/${2}/d" "${var}"
fi
[[ "${4}" == "nonvolatile" ]] && echo "cfg+=( [${2}]=\"${3}\" )" >> "${var}" ;;
rm)
cfg+=( [${2}]="" )
sed -i "/${2}/d" "${var}" ;;
esac
}
addpkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
2021-03-12 14:35:49 +01:00
test -f "${apps}"/"${input[$i]}" && source "${apps}"/"${input[$i]}"
2021-03-11 14:04:04 +01:00
pkg+=( "${input[$i]}" )
#echo "pkg+=( \"${input[$i]}\" )" >> "${vars}/pkg"
done
}
ignorepkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
ignorepkg+=( "${input[$i]}" )
#echo "pkg+=( \"${input[$i]}\" )" >> "${vars}/pkg"
done
}
servicesenable() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
services_enable+=( "${input[$i]}" )
done
}
servicesdisable() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
services_disable+=( "${input[$i]}" )
done
}
ignorepkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
ignorepkg+=( "${input[$i]}" )
done
}
2021-03-12 16:46:31 +01:00
removepkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
removepkg+=( "${input[$i]}" )
done
}
2021-03-11 14:04:04 +01:00
check() {
if [[ $1 == "alnum" ]]; then
if [[ "${2}" =~ ^[[:alnum:]]+$ ]] || [[ "${2}" =~ ^[0-9A-Za-z._]+$ ]]; then
return 0
else
echo "only alphanumerics and ._"
return 1
fi
elif [[ $1 == "num" ]]; then
if [[ "${2}" =~ ^[0-9]+$ ]]; then
return 0
else
echo "only numbers ${2}"
return 1
fi
fi
}
#useradd valuename index value
useradder() {
users+=( ["${1}${2}"]="${3}" )
#echo "users+=( [${1}${2}]=${3} )"
echo "users+=( [${1}${2}]=\"${3}\" )" >> "${var}"
}
module() {
case $1 in
start)
#if [[ ! "${4}" == "dontcheck" ]]; then
# test -f "${steps}/${2}-${run}" && return 1
#fi
modulename="${2}"
var="${vars}/${modulename}"
desc="${3}"
echo "${3}.."
#[[ "${run}" == "pre" ]] && rm -rf "${var}" && touch "${var}"
touch "${var}"
#return 0
;;
end)
#touch "${step}"
modulename=""
var=""
step=""
donttouch=""
setstep=""
;;
esac
}
umounter() {
umount -R "${1}"
vgchange -an
for i in $(find /dev/mapper -name "voidluks*" -printf "%P\n"); do
cryptsetup close "${i}"
done
}
inst() {
xbps-install -Sy -R "${remote}" -r "${dest}" "${@}" || exit
}
remove() {
xbps-remove -Ry -r "${dest}" "${ignorepkg[@]}" "${@}" || exit
}
#if findinarray "${i}" "${array[@]}"; then echo found; fi
findinarray() {
local input=()
local index=1
for i in "${@}"; do
if [[ ! "${index}" == "1" ]]; then
input+=( "${i}" )
fi
index=$(( index + 1 ))
done
for i in "${input[@]}"; do
if [[ "${i}" == "${1}" ]]; then
return 0
fi
done
return 1
2021-03-12 14:20:05 +01:00
}