dotfiles/system/.local/bin/utils
Severin Kaderli 348ece6f11
Change email in all files
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
2019-03-07 17:27:17 +01:00

123 lines
4.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# SCRIPT NAME:
# utils
#
# AUTHOR:
# Severin Kaderli <severin@kaderli.dev>
#
# DESCRIPTION:
# This script provides functions and variables that I use in my other scripts.
# It is not supposed to be run directly but to be sourced by other scripts.
#
# USAGE:
# Source this script in the needed script.
#######################################
# Directory variables
#######################################
DIR="$( cd "$(dirname "${BASH_SOURCE[0]}" )" && cd ../../../ && pwd)"
SYSTEM_DIR="${DIR}/system"
PACKAGES_DIR="${DIR}/packages"
#######################################
# Bash color code variables
#######################################
RESET='\033[0m'
GREEN='\033[32m'
RED='\033[31m'
BLUE='\033[34m'
YELLOW='\033[33m'
#######################################
# Helper functions
#######################################
# Function to display fancy headers
# USAGE: print_header TITLE
function print_header() {
echo -e "\n${GREEN}╔══════════════════════════════════════════════════════════════════════════════╗"
echo -e "$(printf "%-77s" "${1}")"
echo -e "╚══════════════════════════════════════════════════════════════════════════════╝${RESET}"
}
# Function to print out a log entry together with the current date and time.
# USAGE: print_time_log SCRIPT MESSAGE
function print_time_log() {
echo -e "[$(date "+%F %T")][${1}] ${2}"
}
# Function to print a info message with the current time and to send
# a notification using notify-send.
# USAGE: print_notify TITLE MESSAGE
function print_notify() {
notify-send "${1}" "${2}"
echo -e "${YELLOW}[$(date "+%T")] ${1} ${2}${RESET}"
}
# Prints a section title.
# USAGE: print_section TITLE
function print_section() {
echo -e "${YELLOW}┌──────────────────────────────────────────────────────────────────────────────┐"
echo -e "$(printf "%-77s" "${1}")"
echo -e "└──────────────────────────────────────────────────────────────────────────────┘${RESET}"
}
function print_log() {
echo -e "$1"
}
# Creates the given directory in the home directory
# USAGE: create_directory DIRECTORY
function create_directory() {
print_log "Creating directory ${YELLOW}${HOME}/${1}${RESET}"
mkdir -p "${HOME}/${1}"
}
# Creates a symlink of the given file from the home directory to here.
# USAGE: create_link FILE
function create_link() {
print_log "Linking ${YELLOW}${HOME}/${1}${RESET} -> ${YELLOW}${SYSTEM_DIR}/${1}${RESET}"
rm -rf "${HOME:?}/$1"
ln -fs "${SYSTEM_DIR}/${1}" "${HOME}/${1}"
}
# Function to set owner and permission of a file
# USAGE: set_permission OWNER PERMISSION FILE
function set_permission() {
print_log "Changing owner of ${YELLOW}${3}${RESET} to ${YELLOW}${1}${RESET}"
sudo chown -R "${1}" "${3}"
print_log "Changing permission of ${YELLOW}${3}${RESET} to ${YELLOW}${2}${RESET}"
sudo chmod -R "${2}" "${3}"
}
# Function to add a user to a group
# USAGE add_to_group USER GROUP
function add_to_group() {
print_log "Add user ${YELLOW}${1}${RESET} to group ${YELLOW}${2}${RESET} "
sudo gpasswd -a "${1}" "${2}" > /dev/null 2>&1
}
# Function to enable a service
# USAGE: enable_service SERVICE
function enable_service() {
print_log "Enabling service ${YELLOW}${1}${RESET}"
sudo systemctl enable "${1}"
}
# Function to copy a udev rule to /etc/udev/rules.d/
# USAGE: copy_udev_rules FILE
function copy_udev_rule() {
print_log "Copying ${YELLOW}$(basename "${1}")${RESET} to ${YELLOW}/etc/udev/rules.d/${RESET}"
sudo cp "${1}" "/etc/udev/rules.d/"
}
# Function to copy sudoers files to /etc/sudoers.d/
# USAGE: copy_sudoers_content FILE
function copy_sudoers_content() {
print_log "Copying ${YELLOW}$(basename "${1}")${RESET} to ${YELLOW}/etc/sudoers.d/${RESET}"
sudo mkdir -p "/tmp/sudoers"
sudo cp "${1}" "/tmp/sudoers/$(basename "${1}")"
sudo chmod 0440 "/tmp/sudoers/$(basename "${1}")"
sudo cp -a "/tmp/sudoers/$(basename "${1}")" "/etc/sudoers.d/"
}