From 542e80f524e13fe1c8fc8cdd7129aa68e2725c4b Mon Sep 17 00:00:00 2001 From: eshanized Date: Sun, 22 Dec 2024 02:22:27 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(=5Futils):=20add=20snigdha?= =?UTF-8?q?=20os=20utils=20pkgbuild=20&=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snigdhaos-utils/PKGBUILD | 38 +++ snigdhaos-utils/README.install | 22 ++ snigdhaos-utils/pacman | 10 + snigdhaos-utils/snigdhaos-backup.sh | 218 +++++++++++++++++ snigdhaos-utils/snigdhaos-cleanup.sh | 230 ++++++++++++++++++ .../snigdhaos-uninstall-package.sh | 133 ++++++++++ snigdhaos-utils/snigdhaos-update.sh | 151 ++++++++++++ 7 files changed, 802 insertions(+) create mode 100644 snigdhaos-utils/PKGBUILD create mode 100644 snigdhaos-utils/README.install create mode 100644 snigdhaos-utils/pacman create mode 100644 snigdhaos-utils/snigdhaos-backup.sh create mode 100755 snigdhaos-utils/snigdhaos-cleanup.sh create mode 100755 snigdhaos-utils/snigdhaos-uninstall-package.sh create mode 100755 snigdhaos-utils/snigdhaos-update.sh diff --git a/snigdhaos-utils/PKGBUILD b/snigdhaos-utils/PKGBUILD new file mode 100644 index 00000000..467104c0 --- /dev/null +++ b/snigdhaos-utils/PKGBUILD @@ -0,0 +1,38 @@ +# Maintainer: Eshan Roy + +pkgname=snigdhaos-utils +pkgver=1 +pkgrel=1 +pkgdesc="A set of utility scripts for Snigdha OS" +arch=('any') +url="https://snigdhaos.org/" +license=('MIT') +depends=('bash') +source=( + 'snigdhaos-cleanup.sh' + 'snigdhaos-uninstall-package.sh' + 'snigdhaos-update.sh' + 'snigdhaos-backup.sh' + 'pacman' +) # Add the pacman file source URL here +sha256sums=( + 'SKIP' + 'SKIP' + 'SKIP' + 'SKIP' +) + +# The package installation directory +install_dir="/usr/local/bin" + +package() { + # Install the update script + install -Dm755 "$srcdir/snigdhaos-update.sh" "$pkgdir$install_dir/snigdhaos-update.sh" + # Install the cleanup script + install -Dm755 "$srcdir/snigdhaos-cleanup.sh" "$pkgdir$install_dir/snigdhaos-cleanup.sh" + # Install the uninstaller script + install -Dm755 "$srcdir/snigdhaos-uinstall-package.sh" "$pkgdir$install_dir/snigdhaos-uinstall-package.sh" + + # Install the pacman file + install -Dm755 "$srcdir/pacman" "$pkgdir$install_dir/pacman" +} diff --git a/snigdhaos-utils/README.install b/snigdhaos-utils/README.install new file mode 100644 index 00000000..20393e60 --- /dev/null +++ b/snigdhaos-utils/README.install @@ -0,0 +1,22 @@ +Snigdha OS Utilities Installation Instructions +============================================ + +Thank you for installing Snigdha OS Utilities. Below are the steps and +information regarding this package: + +1. The following scripts are installed to /usr/local/bin: + - snigdhaos-update.sh: A script for system update and maintenance. + - snigdhaos-cleanup.sh: A script for cleaning up unused files and caches. + - snigdhaos-backup.sh: A script for backing up important system files. + - pacman: A script that intercepts the `pacman` command to use `snigdhaos-update.sh`. + +2. Post-installation: + - Ensure that the scripts in `/usr/local/bin` have the correct permissions. + - If you want to use `snigdhaos-update.sh` as a replacement for `pacman -Syyu`, make sure that the `pacman` script is in place and executable. + +3. Optional: You can create aliases or modify shell profiles to make usage more convenient. + +Example usage: +- Run `snigdhaos-update.sh` directly for system updates. +- Use `snigdhaos-cleanup.sh` for system cleanup. +- Use `snigdhaos-backup.sh` for backups. diff --git a/snigdhaos-utils/pacman b/snigdhaos-utils/pacman new file mode 100644 index 00000000..f02aa44f --- /dev/null +++ b/snigdhaos-utils/pacman @@ -0,0 +1,10 @@ +#!/bin/bash +# Custom pacman wrapper to run snigdhaos-update.sh + +# Check if the user wants to update the system +if [[ "$@" == "-Syyu" ]]; then + sudo /usr/local/bin/snigdhaos-update.sh # Run your custom update script +else + # Call the real pacman for other operations + /usr/bin/pacman "$@" +fi diff --git a/snigdhaos-utils/snigdhaos-backup.sh b/snigdhaos-utils/snigdhaos-backup.sh new file mode 100644 index 00000000..3d5d48a9 --- /dev/null +++ b/snigdhaos-utils/snigdhaos-backup.sh @@ -0,0 +1,218 @@ +#!/bin/bash + +# Author: Eshan Roy +# Description: Backup important Snigdha OS system configurations and settings + +set -e # Exit immediately if a command exits with a non-zero status + +# Colors +RED="\033[1;31m" +GREEN="\033[1;32m" +YELLOW="\033[1;33m" +BLUE="\033[1;34m" +RESET="\033[0m" + +# Emojis for user feedback +INFO="๐Ÿ“" +SUCCESS="โœ…" +WARNING="โš ๏ธ" +ERROR="โŒ" +BACKUP="๐Ÿ’พ" + +# Directories and files to be backed up +BACKUP_DIR="$HOME/snigdhaos_backups" +DATE=$(date +"%Y-%m-%d_%H-%M-%S") +SNIGDHAOS_BACKUP="$BACKUP_DIR/snigdhaos_backup_$DATE" +LOG_FILE="$BACKUP_DIR/backup_log_$DATE.txt" + +# List of important configuration directories and files +CONFIG_FILES=( + "/etc/pacman.conf" + "/etc/fstab" + "/etc/locale.conf" + "/etc/vconsole.conf" + "/etc/hostname" + "/etc/hosts" + "/etc/sudoers" + "/etc/systemd/system" + "/etc/X11/xorg.conf.d" + "/etc/udev" + "/etc/NetworkManager" + "/etc/crypttab" + "/etc/mkinitcpio.conf" + "/etc/pm.conf" + "$HOME/.bashrc" + "$HOME/.zshrc" + "$HOME/.config/i3" + "$HOME/.config/sway" + "$HOME/.config/kitty" + "$HOME/.config/nvim" + "$HOME/.config/alacritty" + "$HOME/.config/rofi" +) + +# Function to display help message +show_help() { + echo -e "${BLUE}Usage: ${RESET}snigdhaos-backup.sh [OPTIONS]" + echo -e "" + echo -e "${GREEN}This script will back up important Snigdha OS system configuration files and settings.${RESET}" + echo -e "" + echo -e "${YELLOW}Options:${RESET}" + echo -e " ${GREEN}-h, --help${RESET} Show this help message." + echo -e " ${GREEN}-v, --version${RESET} Display the script version." + echo -e " ${GREEN}-b, --backup-dir${RESET} Specify a custom backup directory (default: \$HOME/snigdhaos_backups)." + echo -e " ${GREEN}-l, --list${RESET} List the files that will be backed up." + echo -e " ${GREEN}-e, --encrypt${RESET} Encrypt the backup with GPG." + echo -e " ${GREEN}-c, --compression${RESET} Choose compression format (gzip, xz, bz2)." + echo -e " ${GREEN}-r, --restore${RESET} Restore backup from a specified file." + echo -e "" + echo -e "Example usage:" + echo -e " snigdhaos-backup.sh -e # Create an encrypted backup." + echo -e " snigdhaos-backup.sh -l # List files to be backed up." + echo -e " snigdhaos-backup.sh -b /path/to/dir # Specify a custom backup directory." +} + +# Function to display version information +show_version() { + echo -e "${BLUE}Version: 1.1.0${RESET}" +} + +# Parse command line options +CUSTOM_BACKUP_DIR=false +LIST_FILES=false +ENCRYPT_BACKUP=false +COMPRESSION_FORMAT="gz" +RESTORE=false +while [[ "$#" -gt 0 ]]; do + case "$1" in + -h|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + -b|--backup-dir) + CUSTOM_BACKUP_DIR=true + BACKUP_DIR="$2" + shift + ;; + -l|--list) + LIST_FILES=true + shift + ;; + -e|--encrypt) + ENCRYPT_BACKUP=true + shift + ;; + -c|--compression) + COMPRESSION_FORMAT="$2" + shift + ;; + -r|--restore) + RESTORE=true + BACKUP_FILE="$2" + shift + ;; + *) + echo -e "${ERROR} Unknown option: $1${RESET}" + show_help + exit 1 + ;; + esac +done + +# Ensure backup directory exists +mkdir -p "$BACKUP_DIR" + +# Function to display a user warning +user_warning() { + echo -e "${WARNING} WARNING: You are about to proceed with the backup or restoration process.${RESET}" + echo -e "${YELLOW}This action may overwrite existing backup data or restore files from a previous backup.${RESET}" + read -p "Do you want to continue? (y/n): " user_input + if [[ ! "$user_input" =~ ^[Yy]$ ]]; then + echo -e "${ERROR} Operation aborted by user.${RESET}" + exit 1 + fi +} + +# Function to list files to be backed up +list_files() { + echo -e "${INFO} The following files and directories will be backed up:${RESET}" + for item in "${CONFIG_FILES[@]}"; do + echo -e "${GREEN} - $item${RESET}" + done +} + +# Function to backup a file or directory +backup_item() { + local item="$1" + if [ -e "$item" ]; then + echo -e "${INFO} Backing up $item...${RESET}" | tee -a "$LOG_FILE" + cp -r "$item" "$SNIGDHAOS_BACKUP" | tee -a "$LOG_FILE" + else + echo -e "${WARNING} Warning: $item not found. Skipping.${RESET}" | tee -a "$LOG_FILE" + fi +} + +# Function to create a compressed backup tarball +create_tarball() { + echo -e "${INFO} Creating $COMPRESSION_FORMAT tarball of backup...${RESET}" | tee -a "$LOG_FILE" + case "$COMPRESSION_FORMAT" in + gz) tar -czf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;; + xz) tar -cJf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;; + bz2) tar -cjf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;; + *) echo -e "${ERROR} Invalid compression format! Using gzip by default." | tee -a "$LOG_FILE" ;; + esac +} + +# Function to encrypt the backup +encrypt_backup() { + if $ENCRYPT_BACKUP; then + echo -e "${INFO} Encrypting the backup...${RESET}" | tee -a "$LOG_FILE" + gpg --symmetric --cipher-algo AES256 "$BACKUP_DIR/$TAR_NAME" | tee -a "$LOG_FILE" + rm "$BACKUP_DIR/$TAR_NAME" + echo -e "${SUCCESS} Backup encrypted successfully!" | tee -a "$LOG_FILE" + fi +} + +# Function to restore a backup +restore_backup() { + echo -e "${INFO} Restoring backup from $BACKUP_FILE...${RESET}" | tee -a "$LOG_FILE" + tar -xvf "$BACKUP_FILE" -C "$HOME" | tee -a "$LOG_FILE" + echo -e "${SUCCESS} Restore completed successfully!" | tee -a "$LOG_FILE" +} + +# Backup process +backup_process() { + echo -e "${INFO} Starting backup process...${RESET}" | tee -a "$LOG_FILE" + + # Loop through the list of configuration files and directories + for item in "${CONFIG_FILES[@]}"; do + backup_item "$item" + done + + # Create a tarball for easy backup storage + TAR_NAME="snigdhaos_backup_$DATE.tar.$COMPRESSION_FORMAT" + create_tarball + encrypt_backup + echo -e "${SUCCESS} Backup completed successfully! Backup is located at: $BACKUP_DIR/$TAR_NAME" | tee -a "$LOG_FILE" +} + +# List files to be backed up if --list is passed +if $LIST_FILES; then + list_files + exit 0 +fi + +# Start backup or restore process +if $RESTORE; then + user_warning + restore_backup +else + user_warning + echo -e "${INFO} Preparing backup...${RESET}" | tee -a "$LOG_FILE" + SNIGDHAOS_BACKUP="$BACKUP_DIR/snigdhaos_backup_$DATE" + backup_process +fi diff --git a/snigdhaos-utils/snigdhaos-cleanup.sh b/snigdhaos-utils/snigdhaos-cleanup.sh new file mode 100755 index 00000000..290402c0 --- /dev/null +++ b/snigdhaos-utils/snigdhaos-cleanup.sh @@ -0,0 +1,230 @@ +#!/bin/bash + +# Author : Eshan Roy +# Author URL : https://eshanized.github.io/ + +set -e # Exit immediately if a command exits with a non-zero status + +# Colors +RED="\033[1;31m" +GREEN="\033[1;32m" +YELLOW="\033[1;33m" +BLUE="\033[1;34m" +RESET="\033[0m" + +# Function to display error message and exit +error_handler() { + echo -e "\n${RED}๐Ÿšจ An error occurred during the cleanup process.${RESET}" + echo -e "${RED}Error on line $1. Please check your system and try again.${RESET}" + exit 1 +} + +# Trap errors and call error_handler +trap 'error_handler $LINENO' ERR + +# Function to display help message +show_help() { + echo -e "${BLUE}Usage: ${RESET}snigdhaos-cleanup.sh [OPTIONS]" + echo -e "" + echo -e "${GREEN}This script will clean up your system by deleting unused package caches, crash reports, application logs, application caches, and trash.${RESET}" + echo -e "" + echo -e "${YELLOW}Options:${RESET}" + echo -e " ${GREEN}-h, --help${RESET} Show this help message." + echo -e " ${GREEN}-v, --version${RESET} Display the script version." + echo -e " ${GREEN}-f, --force${RESET} Skip confirmation prompt and force cleanup." + echo -e " ${GREEN}--dry-run${RESET} Simulate cleanup without deleting anything." + echo -e " ${GREEN}--rotate-logs${RESET} Rotate and archive logs instead of truncating them." + echo -e " ${GREEN}--memory-cache${RESET} Clean system memory cache." + echo -e " ${GREEN}--backup${RESET} Back up important configuration files." + echo -e " ${GREEN}--notify${RESET} Send a notification when the cleanup is complete." + echo -e "" + echo -e "Example usage:" + echo -e " snigdhaos-cleanup.sh # Starts the cleanup process with a confirmation prompt." + echo -e " snigdhaos-cleanup.sh -f # Skips the confirmation and forces cleanup." + echo -e " snigdhaos-cleanup.sh --dry-run # See what would be cleaned without actually deleting." +} + +# Function to display version information +show_version() { + echo -e "${BLUE}Version: 1.1.0${RESET}" +} + +# Parse command line options +FORCE_CLEAN=false +DRY_RUN=false +ROTATE_LOGS=false +CLEAN_MEMORY_CACHE=false +BACKUP=false +NOTIFY=false +while [[ "$#" -gt 0 ]]; do + case "$1" in + -h|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + -f|--force) + FORCE_CLEAN=true + shift + ;; + --dry-run) + DRY_RUN=true + shift + ;; + --rotate-logs) + ROTATE_LOGS=true + shift + ;; + --memory-cache) + CLEAN_MEMORY_CACHE=true + shift + ;; + --backup) + BACKUP=true + shift + ;; + --notify) + NOTIFY=true + shift + ;; + *) + echo -e "${RED}โŒ Unknown option: $1${RESET}" + show_help + exit 1 + ;; + esac +done + +# Ensure the script is run as root for certain actions +if [ "$EUID" -ne 0 ]; then + echo -e "${RED}โŒ Please run this script as root or with sudo.${RESET}" + exit 1 +fi + +# Function to send notifications +send_notification() { + MESSAGE="Cleanup completed successfully!" + notify-send "Cleanup Script" "$MESSAGE" || { echo -e "${RED}โŒ Failed to send notification.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Notification sent.${RESET}" +} + +# Function to back up important config files +backup_config_files() { + BACKUP_DIR="/path/to/backup" + CONFIG_DIRS=("/etc" "/home/*/.config") + mkdir -p "$BACKUP_DIR" + for dir in "${CONFIG_DIRS[@]}"; do + sudo cp -r "$dir" "$BACKUP_DIR" || { echo -e "${RED}โŒ Failed to backup $dir.${RESET}"; exit 1; } + done + echo -e "${GREEN}โœ… Configuration files backed up.${RESET}" +} + +# Function to clean memory cache +clean_memory_cache() { + echo -e "${YELLOW}โš ๏ธ Cleaning memory cache...${RESET}" + sudo sync && sudo echo 3 > /proc/sys/vm/drop_caches || { echo -e "${RED}โŒ Failed to clean memory cache.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Memory cache cleaned.${RESET}" +} + +# Function to rotate logs +rotate_logs() { + LOG_DIR="/var/log" + LOG_ARCHIVE_DIR="/var/log/archives" + mkdir -p "$LOG_ARCHIVE_DIR" + sudo find "$LOG_DIR" -type f -name "*.log" -exec bash -c 'gzip -c "$0" > "$1/$(basename $0).gz" && truncate -s 0 "$0"' {} "$LOG_ARCHIVE_DIR" \; + echo -e "${GREEN}โœ… Logs rotated and archived.${RESET}" +} + +# Function to clean package cache +clean_package_cache() { + echo -e "${YELLOW}โš ๏ธ Deleting package cache...${RESET}" + if ! $DRY_RUN; then + sudo pacman -Sc --noconfirm || { echo -e "${RED}โŒ Failed to clean package cache.${RESET}"; exit 1; } + fi + echo -e "${GREEN}โœ… Package cache cleaned.${RESET}" +} + +# Function to clean crash reports +clean_crash_reports() { + echo -e "${YELLOW}โš ๏ธ Deleting crash reports...${RESET}" + CRASH_DIR="/var/lib/systemd/coredump" + if [ -d "$CRASH_DIR" ]; then + sudo rm -rf "$CRASH_DIR"/* || { echo -e "${RED}โŒ Failed to clean crash reports.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Crash reports cleaned.${RESET}" + else + echo -e "${GREEN}โœ… No crash reports found.${RESET}" + fi +} + +# Function to clean application logs +clean_application_logs() { + echo -e "${YELLOW}โš ๏ธ Truncating application logs...${RESET}" + LOG_DIR="/var/log" + sudo find "$LOG_DIR" -type f -name "*.log" -exec truncate -s 0 {} \; || { echo -e "${RED}โŒ Failed to clean application logs.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Application logs cleaned.${RESET}" +} + +# Function to clean application caches +clean_application_caches() { + echo -e "${YELLOW}โš ๏ธ Deleting application caches...${RESET}" + CACHE_DIR="/home/*/.cache" + sudo rm -rf $CACHE_DIR/* || { echo -e "${RED}โŒ Failed to clean application caches.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Application caches cleaned.${RESET}" +} + +# Function to empty trash +clean_trash() { + echo -e "${YELLOW}โš ๏ธ Emptying trash...${RESET}" + TRASH_DIR="/home/*/.local/share/Trash" + sudo rm -rf $TRASH_DIR/* || { echo -e "${RED}โŒ Failed to empty trash.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… Trash emptied.${RESET}" +} + +# Prompt for confirmation if not forced +if ! $FORCE_CLEAN; then + echo -e "${YELLOW}โš ๏ธ WARNING: This script will permanently delete unnecessary files, logs, and caches to free up disk space.${RESET}" + echo -e "${BLUE}๐Ÿ‘‰ The following will be cleaned:${RESET}" + echo -e "${GREEN} - Package cache${RESET}" + echo -e "${GREEN} - Crash reports${RESET}" + echo -e "${GREEN} - Application logs${RESET}" + echo -e "${GREEN} - Application caches${RESET}" + echo -e "${GREEN} - Trash${RESET}" + read -p "$(echo -e "${YELLOW}โ“ Are you sure you want to proceed? [y/N]: ${RESET}")" CONFIRM + if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then + echo -e "${RED}โŒ Cleanup operation cancelled.${RESET}" + exit 0 + fi +fi + +# Run specific cleanup tasks based on flags +echo -e "${BLUE}๐Ÿงน Starting cleanup process...${RESET}" + +# Perform the selected cleanup tasks +if $BACKUP; then + backup_config_files +fi + +if $CLEAN_MEMORY_CACHE; then + clean_memory_cache +fi + +if $ROTATE_LOGS; then + rotate_logs +else + clean_package_cache + clean_crash_reports + clean_application_logs + clean_application_caches + clean_trash +fi + +# Send notification if required +if $NOTIFY; then + send_notification +fi + +echo -e "\n${GREEN}โœ… Cleanup completed successfully!${RESET}" +exit 0 diff --git a/snigdhaos-utils/snigdhaos-uninstall-package.sh b/snigdhaos-utils/snigdhaos-uninstall-package.sh new file mode 100755 index 00000000..cc3fca45 --- /dev/null +++ b/snigdhaos-utils/snigdhaos-uninstall-package.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +# author : eshan roy +# author url : https://eshanized.github.io/ + +set -e # exit immediately if a command exits with a non-zero status + +# colors +RED="\033[1;31m" +GREEN="\033[1;32m" +YELLOW="\033[1;33m" +BLUE="\033[1;34m" +RESET="\033[0m" + +# log file path +LOG_FILE="/var/log/package_uninstall.log" + +# function to display error message and exit +error_handler() { + echo -e "\n${RED}๐Ÿšจ an error occurred during the uninstallation process.${RESET}" + echo -e "${RED}command: $BASH_COMMAND${RESET}" + echo -e "${RED}error on line $1. please check your system and try again.${RESET}" + echo -e "$(date) - ERROR: an error occurred during the uninstallation process for $PACKAGE" >> $LOG_FILE + exit 1 +} + +# trap errors and call error_handler +trap 'error_handler $LINENO' ERR + +# function to display help message +show_help() { + echo -e "${BLUE}usage: ${RESET}$0 [options]" + echo -e "" + echo -e "${GREEN}this script will uninstall the specified package, remove its cache, and clean up orphaned packages.${RESET}" + echo -e "" + echo -e "${YELLOW}options:${RESET}" + echo -e " ${GREEN}-h, --help${RESET} show this help message." + echo -e " ${GREEN}-d, --dry-run${RESET} show what will be done without performing any actions." + echo -e " ${GREEN}-f, --force${RESET} force removal of the package even if it has dependencies." + echo -e " ${GREEN}-b, --backup${RESET} backup configuration files before uninstallation." + echo -e "" + echo -e "${YELLOW}example usage:${RESET}" + echo -e " $0 firefox # uninstalls the firefox package and its cache." + echo -e " $0 -d firefox # show the dry-run actions for firefox." + echo -e " $0 -f firefox # force remove firefox package." + echo -e " $0 -b firefox # backup configuration before uninstalling firefox." + echo -e " $0 -h # displays this help message." +} + +# parse command line options +DRY_RUN=false +FORCE=false +BACKUP=false + +while [[ "$1" == -* ]]; do + case "$1" in + -h|--help) show_help; exit 0 ;; + -d|--dry-run) DRY_RUN=true; shift ;; + -f|--force) FORCE=true; shift ;; + -b|--backup) BACKUP=true; shift ;; + *) echo -e "${RED}โŒ invalid option $1.${RESET}"; show_help; exit 1 ;; + esac +done + +# ensure the script is run as root for certain actions +if [ "$EUID" -ne 0 ]; then + echo -e "${RED}โŒ please run this script as root or with sudo.${RESET}" + exit 1 +fi + +# check if a package name is provided +if [ -z "$1" ]; then + echo -e "${RED}โŒ no package specified.${RESET}" + echo -e "usage: $0 " + exit 1 +fi + +PACKAGE=$1 + +# check if package is installed +if ! pacman -Qi $PACKAGE > /dev/null; then + echo -e "${RED}โŒ the package $PACKAGE is not installed on your system.${RESET}" + exit 1 +fi + +# confirm with the user +echo -e "${YELLOW}โš ๏ธ warning: this script will uninstall the specified package and remove its cache.${RESET}" +read -p "$(echo -e "${BLUE}are you sure you want to uninstall $PACKAGE and remove its cache? [y/N]: ${RESET}")" CONFIRM +if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then + echo -e "${RED}โŒ operation cancelled.${RESET}" + exit 0 +fi + +# backup configuration files (optional) +if $BACKUP; then + echo -e "${BLUE}๐Ÿ”„ backing up configuration files for $PACKAGE...${RESET}" + backup_dir="/path/to/backup/$PACKAGE" + mkdir -p "$backup_dir" + cp /etc/$PACKAGE/* "$backup_dir" || { echo -e "${RED}โŒ backup failed.${RESET}"; exit 1; } + echo -e "${GREEN}โœ… backup completed!${RESET}" +fi + +# dry-run mode (only show what would be done) +if $DRY_RUN; then + echo -e "${YELLOW}dry-run mode enabled: the following actions would be performed.${RESET}" + pacman -Qi $PACKAGE + echo -e "${BLUE}would uninstall $PACKAGE, remove its cache, and clean orphaned packages.${RESET}" + exit 0 +fi + +# force removal option +if $FORCE; then + echo -e "${YELLOW}โš ๏ธ force removal enabled for $PACKAGE.${RESET}" + sudo pacman -Rdd $PACKAGE --noconfirm || { echo -e "${RED}โŒ failed to force remove $PACKAGE.${RESET}"; exit 1; } +else + # uninstall the package and its dependencies + echo -e "${BLUE}๐Ÿ“ฆ uninstalling $PACKAGE...${RESET}" + sudo pacman -Rns $PACKAGE --noconfirm || { echo -e "${RED}โŒ failed to uninstall $PACKAGE.${RESET}"; exit 1; } +fi + +# remove the package's cache +echo -e "${BLUE}๐Ÿ—‘๏ธ removing cache for $PACKAGE...${RESET}" +sudo pacman -Sc --noconfirm || echo -e "${YELLOW}โ— cache removal skipped.${RESET}" + +# clean up orphaned packages +echo -e "${BLUE}๐Ÿงน cleaning up orphaned packages...${RESET}" +sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo -e "${YELLOW}no orphaned packages to remove.${RESET}" + +# log the uninstallation +echo -e "$(date) - successfully uninstalled $PACKAGE" >> $LOG_FILE + +echo -e "\n${GREEN}โœ… package $PACKAGE successfully uninstalled!${RESET}" +exit 0 diff --git a/snigdhaos-utils/snigdhaos-update.sh b/snigdhaos-utils/snigdhaos-update.sh new file mode 100755 index 00000000..931c7188 --- /dev/null +++ b/snigdhaos-utils/snigdhaos-update.sh @@ -0,0 +1,151 @@ +#!/bin/bash + +# author : eshan roy +# author url : https://eshanized.github.io/ + +# colors for formatting output in terminal +RED="\033[1;31m" # red color for errors +GREEN="\033[1;32m" # green color for success messages +YELLOW="\033[1;33m" # yellow color for warnings +BLUE="\033[1;34m" # blue color for info messages +RESET="\033[0m" # reset color to default + +# log file path to save script output +LOG_FILE="/var/log/snigdha_update_script.log" + +# function to display error message and exit +error_handler() { + echo -e "\n${RED}๐Ÿšจ an error occurred during the update process.${RESET}" # print error message in red + echo -e "${RED}error on line $1. please check your system and try again.${RESET}" # indicate the line number where error occurred + echo -e "$(date) - error on line $1" >> $LOG_FILE # log error with timestamp + exit 1 # exit the script with error status +} + +# trap errors and call error_handler when an error occurs +trap 'error_handler $LINENO' ERR + +# function to display help message +show_help() { + # display general usage of the script + echo -e "${BLUE}usage: ${RESET}$0 [options]" + echo -e "" + echo -e "${GREEN}this script will update the package database, upgrade all installed packages, and clean up orphaned packages.${RESET}" + echo -e "" + # show available options for the script + echo -e "${YELLOW}options:${RESET}" + echo -e " ${GREEN}-h, --help${RESET} show this help message." + echo -e " ${GREEN}-v, --verbose${RESET} enable verbose output." + echo -e " ${GREEN}-d, --dry-run${RESET} show what would be done without making changes." + echo -e " ${GREEN}-b, --backup${RESET} backup important configuration files before updating." + echo -e "" + # provide usage examples for the user + echo -e "${YELLOW}example usage:${RESET}" + echo -e " $0 # updates the system and removes orphaned packages." + echo -e " $0 -v # run in verbose mode." + echo -e " $0 -d # dry run (preview only)." + echo -e " $0 -b # backup configuration files before updating." +} + +# parse command line options for verbose, dry-run, and backup flags +VERBOSE=false # default to false, verbose output is off +DRY_RUN=false # default to false, dry-run mode is off +BACKUP=false # default to false, backup is not done by default + +while [[ "$1" != "" ]]; do # loop through each command-line argument + case $1 in + -h|--help) show_help; exit 0 ;; # if -h or --help is passed, show help and exit + -v|--verbose) VERBOSE=true ;; # if -v or --verbose is passed, enable verbose mode + -d|--dry-run) DRY_RUN=true ;; # if -d or --dry-run is passed, enable dry-run mode + -b|--backup) BACKUP=true ;; # if -b or --backup is passed, enable backup option + *) echo -e "${RED}โŒ invalid option: $1${RESET}"; show_help; exit 1 ;; # handle invalid options + esac + shift # move to the next argument +done + +# ensure the script is run as root for certain actions +if [ "$EUID" -ne 0 ]; then # check if the script is being run by root user + echo -e "${RED}โŒ please run this script as root or with sudo.${RESET}" # print error if not root + exit 1 # exit the script +fi + +# warning message before proceeding with the update +echo -e "${YELLOW}โš ๏ธ warning: this script will update your system, upgrade installed packages, and clean up orphaned packages.${RESET}" +if $BACKUP; then # if the backup option is enabled + echo -e "${YELLOW}โš ๏ธ you have chosen to backup important configuration files.${RESET}" + read -p "$(echo -e "${BLUE}are you sure you want to proceed? [y/N]: ${RESET}")" CONFIRM # ask for confirmation +else + read -p "$(echo -e "${BLUE}are you sure you want to proceed? [y/N]: ${RESET}")" CONFIRM # normal confirmation prompt +fi +if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then # check if user confirmed + echo -e "${RED}โŒ operation cancelled.${RESET}" # print cancellation message + exit 0 # exit the script +fi + +# backup important configuration files (optional) +backup_config_files() { + echo -e "${BLUE}๐Ÿ“ฆ backing up important configuration files...${RESET}" # inform about backup + BACKUP_DIR="/home/$USER/important_configs" # directory to store backups + mkdir -p $BACKUP_DIR # create backup directory if it doesn't exist + + # list of important config files to back up + CONFIG_FILES=( + "/etc/pacman.conf" + "/etc/makepkg.conf" + "/etc/sudoers" + "/etc/systemd/system" + "$HOME/.bashrc" + "$HOME/.zshrc" + ) + + for FILE in "${CONFIG_FILES[@]}"; do # loop through each config file + if [ -e "$FILE" ]; then # check if the file exists + cp -r "$FILE" "$BACKUP_DIR" || { echo -e "${RED}โŒ failed to back up $FILE.${RESET}"; exit 1; } # copy the file to backup + echo -e "${GREEN}โœ… backed up $FILE${RESET}" # inform the user the file was backed up + else + echo -e "${YELLOW}โš ๏ธ skipping $FILE (not found).${RESET}" # if the file is not found, skip it + fi + done +} + +# log the start of the script +echo -e "$(date) - starting update process" >> $LOG_FILE # log the start with timestamp + +# update the package database and upgrade packages +echo -e "${BLUE}๐Ÿ”„ updating the package database...${RESET}" +if $VERBOSE; then # if verbose mode is enabled + sudo pacman -Sy --verbose || error_handler $LINENO # run with verbose output +else + sudo pacman -Sy || error_handler $LINENO # run normally +fi + +echo -e "${BLUE}๐Ÿ“ฆ upgrading installed packages...${RESET}" +if $DRY_RUN; then # if dry-run mode is enabled + echo -e "${YELLOW}โš ๏ธ dry run mode: the following commands would be executed but won't make changes.${RESET}" + echo -e " sudo pacman -Su --noconfirm" # show the dry-run commands +else + if $VERBOSE; then # if verbose mode is enabled + sudo pacman -Su --noconfirm --verbose || error_handler $LINENO # upgrade with verbose + else + sudo pacman -Su --noconfirm || error_handler $LINENO # upgrade normally + fi +fi + +# clean up orphaned packages +echo -e "${BLUE}๐Ÿงน cleaning up orphaned packages...${RESET}" +if $DRY_RUN; then # if dry-run mode is enabled + orphaned_packages=$(pacman -Qtdq) # list orphaned packages + if [ -z "$orphaned_packages" ]; then # if no orphaned packages + echo -e "${YELLOW}no orphaned packages to remove.${RESET}" # inform the user + else # if orphaned packages exist + echo -e "${YELLOW}the following orphaned packages would be removed:${RESET} $orphaned_packages" + fi +else # if dry-run mode is not enabled + sudo pacman -Rns $(pacman -Qtdq) --noconfirm || echo -e "${YELLOW}no orphaned packages to remove.${RESET}" # remove orphaned packages +fi + +# log successful completion +echo -e "$(date) - system successfully updated." >> $LOG_FILE # log the successful completion + +# final success message +echo -e "\n${GREEN}โœ… system successfully updated!${RESET}" # print success message +exit 0 # exit the script