#!/bin/bash clear # If the user appends the y flag AUTO_YES=false for arg in "$@"; do if [ "$arg" = "-y" ]; then AUTO_YES=true fi done # Color and formatting variables RESET="\033[0m" BOLD="\033[1m" GREEN="\033[32m" YELLOW="\033[33m" CYAN="\033[36m" RED="\033[31m" # Print functions info() { echo -e "\n${BOLD}${CYAN}>>> $1${RESET}\n"; } warn() { echo -e "\n${BOLD}${YELLOW}>>> $1${RESET}\n"; } error() { echo -e "\n${BOLD}${RED}>>> ERROR: $1${RESET}\n"; } # Check if script is sourced (return 0 2>/dev/null) if [ $? -ne 0 ]; then error "Please run this script with: source docker-debian.sh" echo "Otherwise, directory changes won't persist." exit 1 fi # Set SUDO prefix if [ "$EUID" -ne 0 ]; then SUDO="sudo" else SUDO="" fi # Prompt function prompt() { local message=$1 if [ "$AUTO_YES" = true ]; then echo -e "${BOLD}${CYAN}>>> $message [Y/n]: ${RESET}y" return 0 fi while true; do read -rp "$(echo -e "${BOLD}${CYAN}>>> $message [Y/n]: ${RESET}")" yn case $yn in [Yy]* | "" ) return 0 ;; [Nn]* ) return 1 ;; * ) warn "Please answer yes or no." ;; esac done } ### ======= Collect Choices First ======= prompt "Do you want to update and upgrade the system?" && DO_UPDATE=true prompt "Do you want to install Docker?" && DO_DOCKER=true prompt "Do you want to create a folder named after the hostname?" && USE_HOSTNAME=true if [ -z "$USE_HOSTNAME" ]; then prompt "Do you want to create a folder with a custom name?" && USE_CUSTOM=true if [ "$USE_CUSTOM" = true ]; then read -rp "$(echo -e "${BOLD}${CYAN}>>> Enter custom folder name: ${RESET}")" CUSTOM_DIR [ -z "$CUSTOM_DIR" ] && warn "No folder name entered. Skipping folder creation." && SKIP_FOLDER=true else SKIP_FOLDER=true fi fi if [ -z "$SKIP_FOLDER" ]; then prompt "Do you want to create a compose.yaml file now?" && CREATE_COMPOSE=true prompt "Do you want to create a .env file now?" && CREATE_ENV=true fi ### ======= Review Selections ======= info "Review your selections before continuing:" [ "$DO_UPDATE" = true ] && echo "✔ Update and upgrade the system" [ "$DO_DOCKER" = true ] && echo "✔ Install Docker" if [ -z "$SKIP_FOLDER" ]; then echo "✔ Create folder: $([ "$USE_HOSTNAME" = true ] && echo "$(hostname)" || echo "$CUSTOM_DIR")" [ "$CREATE_COMPOSE" = true ] && echo "✔ Create compose.yaml file" [ "$CREATE_ENV" = true ] && echo "✔ Create .env file" else echo "✖ No folder will be created" fi echo prompt "Proceed with these actions?" || { info "Operation cancelled."; return; } clear ### ======= Execute Actions ======= if [ "$DO_UPDATE" = true ]; then info "Updating and upgrading..." $SUDO apt update && $SUDO apt upgrade -y else info "Skipping system update and upgrade." fi clear if [ "$DO_DOCKER" = true ]; then info "Installing Docker..." $SUDO apt update $SUDO apt install -y ca-certificates curl $SUDO install -m 0755 -d /etc/apt/keyrings $SUDO curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc $SUDO chmod a+r /etc/apt/keyrings/docker.asc echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \ $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | \ $SUDO tee /etc/apt/sources.list.d/docker.list > /dev/null $SUDO apt update $SUDO apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin info "Docker installation complete." else info "Skipping Docker installation." fi clear if [ -z "$SKIP_FOLDER" ]; then if [ "$USE_HOSTNAME" = true ]; then FOLDER_NAME=$(hostname) else FOLDER_NAME="$CUSTOM_DIR" fi mkdir -p "$FOLDER_NAME" cd "$FOLDER_NAME" || exit info "Now inside folder: $FOLDER_NAME" if [ "$CREATE_COMPOSE" = true ]; then touch compose.yaml info "compose.yaml created." fi if [ "$CREATE_ENV" = true ]; then touch .env info ".env file created." fi else info "No folder created or entered." fi clear info "Script completed." rm -f ~/docker-debian.sh && echo -e "\n${BOLD}${GREEN}>>> Script file 'docker-debian.sh' in your home directory has been deleted.${RESET}\n"