void-bash-installer/functions/functions

218 lines
4.1 KiB
Plaintext

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]}"
use="${input[$i]}"
else
choices="${choices}|${input[$i]}"
use+=" ${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: "${use}"
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}" "${vars}/cfg"; then
# sed -i "/${2}/d" "${vars}/cfg"
#fi
echo "cfg+=( [${2}]=\"${3}\" )" >> "${vars}/cfg" ;;
esac
}
addpkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
pkg+=( "${input[$i]}" )
echo "pkg+=( \"${input[$i]}\" )" >> "${vars}/pkg"
done
}
servicesenable() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
services_enable+=( "${input[$i]}" )
echo "services_enable+=( \"${input[$i]}\" )" >> "${vars}/services_enable"
done
}
servicesdisable() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
services_disable+=( "${input[$i]}" )
echo "services_disable+=( \"${input[$i]}\" )" >> "${vars}/services_disable"
done
}
ignorepkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
ignorepkg+=( "${input[$i]}" )
echo "ignorepkg+=( \"${input[$i]}\" )" >> "${vars}/ignorepkg"
done
}
removepkg() {
local input=( ${@} )
for i in $(seq 0 "$(( ${#input[@]} - 1 ))"); do
removepkg+=( "${input[$i]}" )
echo "removepkg+=( \"${input[$i]}\" )" >> "${vars}/removepkg"
done
}
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}\" )" >> "${vars}/users"
}
module() {
case $1 in
start)
modulename="${2}"
desc="${3}"
#echo "${3}.."
;;
end)
modulename=""
;;
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
}