dropshell/install.sh
Your Name 8b807da346 .
2025-04-21 10:25:28 +12:00

155 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/src/version.sh"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to create directory with proper ownership
mkdir_with_ownership() {
local target_dir="$1"
local current_dir="$target_dir"
local parent_dir
local owner
local group
local created_dirs=()
# Find the lowest existing parent directory
while [ ! -d "$current_dir" ] && [ "$current_dir" != "/" ]; do
created_dirs+=("$current_dir")
parent_dir="$(dirname "$current_dir")"
current_dir="$parent_dir"
done
# If we found an existing directory, get its ownership
if [ -d "$current_dir" ]; then
owner=$(stat -c %U "$current_dir")
group=$(stat -c %G "$current_dir")
else
echo -e "${RED}Error: Could not find any existing parent directory${NC}"
return 1
fi
# Create the directory and set ownership for all created directories
if mkdir -p "$target_dir"; then
# Set ownership for all created directories in reverse order
for ((i=${#created_dirs[@]}-1; i>=0; i--)); do
chown "$owner:$group" "${created_dirs[$i]}"
done
return 0
else
echo -e "${RED}Error: Failed to create directory $target_dir${NC}"
return 1
fi
}
# Check if USER_DEFINITIONS is provided
if [ -z "$1" ]; then
echo -e "${RED}Error: USER_DEFINITIONS path is required${NC}"
echo "Usage: $0 <USER_DEFINITIONS_PATH>"
echo "Example: $0 ~/.config/dropshell"
exit 1
fi
USER_DEFINITIONS="$1"
# Script locations
SCRIPT_SOURCE="$SCRIPT_DIR/src/dropshell.sh"
SCRIPT_DEST="/usr/local/bin/dropshell"
COMPLETION_SOURCE="$SCRIPT_DIR/src/dropshell-completion.bash"
COMPLETION_DEST="/etc/bash_completion.d/dropshell"
EXAMPLE_DIR="$SCRIPT_DIR/.example"
DROPSHELL_OPT="/opt/dropshell"
# Check if running with sudo/root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Please run as root or with sudo${NC}"
exit 1
fi
# Check if source script exists
if [ ! -f "$SCRIPT_SOURCE" ]; then
echo -e "${RED}Error: $SCRIPT_SOURCE not found${NC}"
echo "Please ensure the script exists in the src directory"
exit 1
fi
# Check if completion script exists
if [ ! -f "$COMPLETION_SOURCE" ]; then
echo -e "${RED}Error: $COMPLETION_SOURCE not found${NC}"
echo "Please ensure the completion script exists in the src directory"
exit 1
fi
# Create user definitions directory if it doesn't exist
if [ ! -d "$USER_DEFINITIONS" ]; then
echo "Creating user definitions directory..."
mkdir_with_ownership "$USER_DEFINITIONS" || exit 1
fi
# Check if example directory exists and copy example files if needed
if [ ! -d "$EXAMPLE_DIR" ]; then
echo -e "${RED}Error: Example directory not found at $EXAMPLE_DIR${NC}"
echo "Please ensure the .example directory exists in the project root"
exit 1
fi
# Check and create servers directory
if [ ! -d "$USER_DEFINITIONS/servers" ]; then
echo "Copying example files..."
mkdir_with_ownership "$USER_DEFINITIONS/servers" || exit 1
cp -ra "$EXAMPLE_DIR"/servers/* "$USER_DEFINITIONS/servers"
fi
# Check and create usertemplates directory
if [ ! -d "$USER_DEFINITIONS/usertemplates" ]; then
echo "Copying example files..."
mkdir_with_ownership "$USER_DEFINITIONS/usertemplates" || exit 1
cp -ra "$EXAMPLE_DIR"/usertemplates/* "$USER_DEFINITIONS/usertemplates"
fi
# install user directory.
echo "Creating symbolic link for /opt/dropshell/user..."
mkdir -p "$DROPSHELL_OPT" || exit 1
if [ -e "$DROPSHELL_OPT/user" ]; then
rm -rf "$DROPSHELL_OPT/user"
fi
ln -s "$USER_DEFINITIONS" "$DROPSHELL_OPT/user"
# Create symbolic link
echo "Creating symbolic link for running from $SCRIPT_DEST..."
if [ -L "$SCRIPT_DEST" ]; then
rm "$SCRIPT_DEST"
fi
ln -s "$SCRIPT_SOURCE" "$SCRIPT_DEST"
# Make sure the scripts are executable
chmod +x "$SCRIPT_SOURCE"
chmod +x "$COMPLETION_SOURCE"
# Install bash completion
echo "Installing bash completion..."
if [ ! -d "/etc/bash_completion.d" ]; then
mkdir_with_ownership "/etc/bash_completion.d" || exit 1
fi
# Install completion script
cp "$COMPLETION_SOURCE" "$COMPLETION_DEST"
chmod 644 "$COMPLETION_DEST"
# Create environment file
cat > "/etc/dropshell.conf" << EOF
# Dropshell configuration
USER_DEFINITIONS="$USER_DEFINITIONS"
EOF
# Source the completion script
echo "source $COMPLETION_DEST" >> ~/.bashrc
echo -e "${GREEN}Installation completed successfully!${NC}"
exit 0