Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- # (MIT, GPL3+) Alberto Salvia Novella (es20490446e)
- main () {
- checkPermissions
- insertHook "colors" "base"
- }
- checkPermissions () {
- user=$(id -u)
- if [ "${user}" -ne 0 ]; then
- (>&2 echo "Not enough permissions")
- echo "Type 'sudo' or 'su;' before this command"
- exit 1
- fi
- }
- execute () {
- function="${1}"
- command="${2}"
- error=$(eval "${command}" 2>&1 >"/dev/null")
- if [ ${?} -ne 0 ]; then
- echo "${function}: $error"
- exit 1
- fi
- }
- insert () {
- string="${1}"
- toInsert="${2}"
- separator="${3}"
- if [ "${string}" != "" ]; then
- string=$(echo "${string}""${separator}")
- fi
- echo "${string}""${toInsert}"
- }
- insertAfter () {
- string="${1}"
- toInsert="${2}"
- after="${3}"
- separator="${4}"
- IFS="${separator}" read -r -a string <<< "$string"
- for element in "${string[@]}"; do
- if [ "${lastElement}" = "${after}" ] && [ "${element}" != "${toInsert}" ]; then
- inserted=$(insert "${inserted}" "${toInsert}" "${separator}")
- fi
- inserted=$(insert "${inserted}" "${element}" "${separator}")
- lastElement="${element}"
- done
- echo "${inserted}"
- }
- insertHook () {
- toInsert=${1}
- after=${2}
- file="/etc/mkinitcpio.conf"
- hooks=$(variableInFile "HOOKS" "${file}")
- inserted=$(insertAfter "${hooks}" "${toInsert}" "${after}" " ")
- modifyVariableInFile "HOOKS" "${inserted}" "${file}"
- updateInitImage
- }
- modifyVariableInFile () {
- variable="${1}"
- content="${2}"
- file="${3}"
- if [ ! -f "${file}" ]; then
- echo "modifyVariableInFile: file doesn't exist: ${file}"
- exit 1
- fi
- sed -i "s/^${variable}\=.*/${variable}=\"${content}\"/" "${file}"
- }
- updateInitImage () {
- execute "updateInitImage" "mkinitcpio"
- }
- variableInFile () {
- variable="${1}"
- file="${2}"
- source "${file}"
- eval value=\$\{${variable}\}
- echo "${value}"
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement