Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 89 additions & 47 deletions add_ssh_key.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,94 @@
#!/bin/bash

# POSIX-compliant color definitions
ESC=$(printf '\033')
RC="${ESC}[0m" # Reset
RED="${ESC}[31m" # Red
GREEN="${ESC}[32m" # Green
YELLOW="${ESC}[33m" # Yellow
BLUE="${ESC}[34m" # Blue
CYAN="${ESC}[36m" # Cyan

# Check if zsh is being used
if [ -n "$ZSH_VERSION" ]; then
printf "%sDetected zsh. Using zsh for script execution.%s\n" "${CYAN}" "${RC}"
exec zsh "$0" "$@"
exit
fi
#!/bin/sh

# Source the common script
eval "$(curl -s https://raw.githubusercontent.com/Jaredy899/mac/refs/heads/dev/common_script.sh)"

# Variables
SSH_DIR="$HOME/.ssh"
AUTHORIZED_KEYS="$SSH_DIR/authorized_keys"

# Ensure the .ssh directory exists
if [ ! -d "$SSH_DIR" ]; then
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
printf "%sCreated %s and set permissions to 700.%s\n" "${GREEN}" "$SSH_DIR" "${RC}"
else
printf "%s%s already exists.%s\n" "${YELLOW}" "$SSH_DIR" "${RC}"
fi

# Ensure the authorized_keys file exists
if [ ! -f "$AUTHORIZED_KEYS" ]; then
touch "$AUTHORIZED_KEYS"
chmod 600 "$AUTHORIZED_KEYS"
printf "%sCreated %s and set permissions to 600.%s\n" "${GREEN}" "$AUTHORIZED_KEYS" "${RC}"
else
printf "%s%s already exists.%s\n" "${YELLOW}" "$AUTHORIZED_KEYS" "${RC}"
fi

# Add the public key to the authorized_keys file if not already added
printf "%sEnter the public key to add: %s" "${CYAN}" "${RC}"
read PUBLIC_KEY

if grep -q "$PUBLIC_KEY" "$AUTHORIZED_KEYS"; then
printf "%sPublic key already exists in %s.%s\n" "${YELLOW}" "$AUTHORIZED_KEYS" "${RC}"
else
echo "$PUBLIC_KEY" >> "$AUTHORIZED_KEYS"
printf "%sPublic key added to %s.%s\n" "${GREEN}" "$AUTHORIZED_KEYS" "${RC}"
fi

printf "%sDone.%s\n" "${GREEN}" "${RC}"
# Function to ensure directory and file exist with correct permissions
ensure_ssh_setup() {
if [ ! -d "$SSH_DIR" ]; then
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
print_success "Created $SSH_DIR and set permissions to 700."
else
print_warning "$SSH_DIR already exists."
fi

if [ ! -f "$AUTHORIZED_KEYS" ]; then
touch "$AUTHORIZED_KEYS"
chmod 600 "$AUTHORIZED_KEYS"
print_success "Created $AUTHORIZED_KEYS and set permissions to 600."
else
print_warning "$AUTHORIZED_KEYS already exists."
fi
}

# Function to import SSH keys from GitHub
import_ssh_keys() {
print_info "Enter the GitHub username: "
read -r github_user

ssh_keys_url="https://github.com/$github_user.keys"
keys=$(curl -s "$ssh_keys_url")

if [ -z "$keys" ]; then
print_error "No SSH keys found for GitHub user: $github_user"
else
print_success "SSH keys found for $github_user:"
printf "%s\n" "$keys"
print_info "Do you want to import these keys? [Y/n]: "
read -r confirm

case "$confirm" in
[Nn]*)
print_warning "SSH key import cancelled."
;;
*)
printf "%s\n" "$keys" >> "$AUTHORIZED_KEYS"
chmod 600 "$AUTHORIZED_KEYS"
print_success "SSH keys imported successfully!"
;;
esac
fi
}

# Function to add a manually entered public key
add_manual_key() {
print_info "Enter the public key to add: "
read -r PUBLIC_KEY

if grep -q "$PUBLIC_KEY" "$AUTHORIZED_KEYS"; then
print_warning "Public key already exists in $AUTHORIZED_KEYS."
else
printf "%s\n" "$PUBLIC_KEY" >> "$AUTHORIZED_KEYS"
chmod 600 "$AUTHORIZED_KEYS"
print_success "Public key added to $AUTHORIZED_KEYS."
fi
}

# Function to show SSH key menu
show_ssh_menu() {
show_menu_item 1 "$selected" "Import from GitHub"
show_menu_item 2 "$selected" "Enter your own public key"
}

# Main script
ensure_ssh_setup

# Handle menu selection
handle_menu_selection 2 "Select SSH key option" show_ssh_menu
choice=$?

case $choice in
1)
import_ssh_keys
;;
2)
add_manual_key
;;
esac

print_colored "$GREEN" "SSH key setup completed"
112 changes: 112 additions & 0 deletions common_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/bin/sh

# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
RC='\033[0m' # Reset color

# Function to read keyboard input
read_key() {
dd bs=1 count=1 2>/dev/null | od -An -tx1
}

# Function to show menu item
show_menu_item() {
if [ "$selected" -eq "$1" ]; then
printf " ${GREEN}→ %s${RC}\n" "$3"
else
printf " %s\n" "$3"
fi
}

# Function to handle menu selection
handle_menu_selection() {
selected=1
total_options=$1
saved_stty=$(stty -g)

cleanup() {
stty "$saved_stty"
printf "\n${GREEN}Script terminated.${RC}\n"
exit 0
}

trap cleanup INT

while true; do
# Clear screen and show header
printf "\033[2J\033[H"
print_colored "$CYAN" "$2"
echo

# Call the function that displays menu items
$3

printf "\n${MAGENTA}Use arrow keys to navigate, Enter to select, 'q' to exit${RC}\n"

# Read keyboard input
stty raw -echo
key=$(dd bs=3 count=1 2>/dev/null)
case "$key" in
$'\x1B\x5B\x41') # Up arrow
if [ $selected -eq 1 ]; then
selected=$total_options
else
selected=$((selected - 1))
fi
;;
$'\x1B\x5B\x42') # Down arrow
if [ $selected -eq $total_options ]; then
selected=1
else
selected=$((selected + 1))
fi
;;
$'\x0A'|$'\x0D') # Enter
stty "$saved_stty"
return $selected
;;
$'\x03') # Ctrl+C
stty "$saved_stty"
cleanup
;;
q|Q) # q or Q
stty "$saved_stty"
cleanup
;;
esac
stty "$saved_stty"
done
}

# Function to print colored text
print_colored() {
local color=$1
local message=$2
printf "${color}%s${RC}\n" "$message"
}

# Function to print error message
print_error() {
print_colored "$RED" "ERROR: $1"
}

# Function to print success message
print_success() {
print_colored "$GREEN" "SUCCESS: $1"
}

# Function to print warning message
print_warning() {
print_colored "$YELLOW" "WARNING: $1"
}

# Function to print info message
print_info() {
print_colored "$BLUE" "INFO: $1"
}
95 changes: 40 additions & 55 deletions dock_scripts/dock_manager.sh
Original file line number Diff line number Diff line change
@@ -1,90 +1,75 @@
#!/bin/bash
#!/bin/sh

# POSIX-compliant color definitions
ESC=$(printf '\033')
RC="${ESC}[0m" # Reset
RED="${ESC}[31m" # Red
GREEN="${ESC}[32m" # Green
YELLOW="${ESC}[33m" # Yellow
BLUE="${ESC}[34m" # Blue
CYAN="${ESC}[36m" # Cyan
# Source the common script
eval "$(curl -s https://raw.githubusercontent.com/Jaredy899/mac/refs/heads/dev/common_script.sh)"

# Check if a path argument is provided
if [ -n "$1" ]; then
GITPATH="$1"
else
GITPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GITPATH="$SCRIPT_DIR"
fi

printf "%sGITPATH is set to: %s%s\n" "${CYAN}" "$GITPATH" "${RC}"
printf "%sCurrent working directory: %s%s\n" "${CYAN}" "$(pwd)" "${RC}"
printf "%sScript location: %s%s\n" "${CYAN}" "${BASH_SOURCE[0]}" "${RC}"
print_info "GITPATH is set to: $GITPATH"
print_info "Current working directory: $(pwd)"
print_info "Script location: ${BASH_SOURCE[0]}"

# GitHub URL base for the necessary Dock scripts
GITHUB_BASE_URL="https://raw.githubusercontent.com/Jaredy899/mac/refs/heads/main/dock_scripts/"

# Function to remove Dock items using icon_remove.sh
remove_dock_items() {
local script_path="$GITPATH/icon_remove.sh"
printf "%sChecking for icon_remove.sh at: %s%s\n" "${CYAN}" "$script_path" "${RC}"
print_info "Checking for icon_remove.sh at: $script_path"
if [[ -f "$script_path" ]]; then
printf "%sRunning icon_remove.sh from local directory...%s\n" "${GREEN}" "${RC}"
print_success "Running icon_remove.sh from local directory..."
bash "$script_path"
else
printf "%sLocal icon_remove.sh not found. Running from GitHub...%s\n" "${YELLOW}" "${RC}"
print_warning "Local icon_remove.sh not found. Running from GitHub..."
bash -c "$(curl -fsSL $GITHUB_BASE_URL/icon_remove.sh)"
fi
}

# Function to add Dock items using icon_add.sh
add_dock_items() {
local script_path="$GITPATH/icon_add.sh"
printf "%sChecking for icon_add.sh at: %s%s\n" "${CYAN}" "$script_path" "${RC}"
print_info "Checking for icon_add.sh at: $script_path"
if [[ -f "$script_path" ]]; then
printf "%sRunning icon_add.sh from local directory...%s\n" "${GREEN}" "${RC}"
print_success "Running icon_add.sh from local directory..."
bash "$script_path"
else
printf "%sLocal icon_add.sh not found. Running from GitHub...%s\n" "${YELLOW}" "${RC}"
print_warning "Local icon_add.sh not found. Running from GitHub..."
bash -c "$(curl -fsSL $GITHUB_BASE_URL/icon_add.sh)"
fi
}

# Function to manage dock items
manage_dock() {
while true; do
printf "%sPlease select from the following options:%s\n" "${CYAN}" "${RC}"
printf "%s1)%s Add Dock icons\n" "${GREEN}" "${RC}"
printf "%s2)%s Remove Dock icons\n" "${GREEN}" "${RC}"
printf "%s0)%s Return to main menu\n" "${RED}" "${RC}"
printf "Enter your choice (0-2): "
read choice

case $choice in
1)
printf "%sAdding Dock items...%s\n" "${CYAN}" "${RC}"
add_dock_items
;;
2)
printf "%sRemoving Dock items...%s\n" "${CYAN}" "${RC}"
remove_dock_items
;;
0)
printf "%sReturning to main menu.%s\n" "${YELLOW}" "${RC}"
break
;;
*)
printf "%sInvalid option. Please enter a number between 0 and 2.%s\n" "${RED}" "${RC}"
;;
esac
printf "\n"
done
# Function to show dock menu items
show_dock_menu() {
show_menu_item 1 "$selected" "Add Dock icons"
show_menu_item 2 "$selected" "Remove Dock icons"
show_menu_item 3 "$selected" "Return to main menu"
}

# Run the dock manager
manage_dock
# Keep running until user chooses to exit
while true; do
# Handle menu selection
handle_menu_selection 3 "Dock Manager" show_dock_menu
choice=$?

printf "%s################################%s\n" "${YELLOW}" "${RC}"
printf "%s##%s %s##%s\n" "${YELLOW}" "${RC}" "${YELLOW}" "${RC}"
printf "%s##%s%s Dock management completed. %s##%s\n" "${YELLOW}" "${RC}" "${GREEN}" "${YELLOW}" "${RC}"
printf "%s##%s %s##%s\n" "${YELLOW}" "${RC}" "${YELLOW}" "${RC}"
printf "%s################################%s\n" "${YELLOW}" "${RC}"
case $choice in
1)
print_info "Adding Dock items..."
add_dock_items
print_colored "$GREEN" "Dock completed"
;;
2)
print_info "Removing Dock items..."
remove_dock_items
print_colored "$GREEN" "Dock completed"
;;
3)
print_info "Returning to main menu."
break
;;
esac
done
Loading