Skip to content

Commit 39894c1

Browse files
committed
Add environment configuration file support to Tailscale installation and receiver scripts.
- Introduced an environment file for configuration at `/etc/default/tailscale-receive`, allowing users to customize settings such as target user, target directory, and logging level. - Enhanced the installation script to create and backup the environment file, ensuring proper permissions and ownership. - Updated the receiver script to read configuration values from the environment file, improving flexibility and user experience. - Implemented preflight checks to validate directory permissions and existing installations, enhancing installation reliability. - Improved error handling and logging throughout the scripts for better user feedback.
1 parent f245f42 commit 39894c1

3 files changed

Lines changed: 256 additions & 39 deletions

File tree

install.sh

Lines changed: 140 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ DEST_SEND_SCRIPT_PATH="/usr/local/bin/tailscale-send.sh"
3434
SYS_KIO_SERVICEMENU_DIR="/usr/share/kio/servicemenus"
3535
SYS_KSERVICES5_SERVICEMENU_DIR="/usr/share/kservices5/ServiceMenus"
3636

37+
# Environment file for configuration
38+
ENV_FILE_PATH="/etc/default/tailscale-receive"
39+
3740
# --- Functions ---
3841

3942
# Function to print a formatted header
@@ -73,25 +76,92 @@ are_scripts_installed() {
7376
backup_existing_config() {
7477
local backup_dir="/tmp/tailscale-receiver-backup-$(date +%Y%m%d-%H%M%S)"
7578
mkdir -p "$backup_dir"
76-
79+
7780
if [ -f "$DEST_SCRIPT_PATH" ]; then
7881
cp "$DEST_SCRIPT_PATH" "$backup_dir/"
7982
echo "Backed up receiver script to $backup_dir/"
8083
fi
81-
84+
8285
if [ -f "$DEST_SEND_SCRIPT_PATH" ]; then
8386
cp "$DEST_SEND_SCRIPT_PATH" "$backup_dir/"
8487
echo "Backed up sender script to $backup_dir/"
8588
fi
86-
89+
8790
if [ -f "$SERVICE_FILE_PATH" ]; then
8891
cp "$SERVICE_FILE_PATH" "$backup_dir/"
8992
echo "Backed up service file to $backup_dir/"
9093
fi
91-
94+
95+
# Backup environment file if it exists
96+
backup_environment_file
97+
9298
echo "Backup created at: $backup_dir"
9399
}
94100

101+
# Function to create environment file
102+
create_environment_file() {
103+
local target_user="$1"
104+
local target_dir="/home/$target_user/Downloads/tailscale"
105+
106+
print_header "Creating Environment Configuration File"
107+
108+
# Create directory if it doesn't exist
109+
mkdir -p "$(dirname "$ENV_FILE_PATH")" || {
110+
print_error_and_exit "Failed to create directory for environment file: $(dirname "$ENV_FILE_PATH")"
111+
}
112+
113+
# Create environment file with proper permissions
114+
cat > "$ENV_FILE_PATH" << EOF
115+
# Tailscale Receiver Configuration File
116+
# This file contains configuration for the tailscale-receive service
117+
# Generated by install.sh on $(date)
118+
119+
# Target user for file ownership (required)
120+
TARGET_USER=$target_user
121+
122+
# Target directory for received files (required)
123+
TARGET_DIR=$target_dir
124+
125+
# Logging level: debug, info, warn, error
126+
LOG_LEVEL=info
127+
128+
# Optional: Tailscale auth key for automatic authentication
129+
# Obtain from: https://login.tailscale.com/admin/settings/keys
130+
# TS_AUTHKEY=tskey-xxxxxxxxxxxxxxxxxx
131+
132+
# Optional: Archive management settings
133+
# ARCHIVE_ENABLED=true
134+
# ARCHIVE_DAYS=14
135+
# ARCHIVE_DIR_NAME=archive
136+
137+
# Optional: Notification settings
138+
# NOTIFY_TIMEOUT=10
139+
EOF
140+
141+
# Set proper permissions (root:root, 600)
142+
chmod 600 "$ENV_FILE_PATH" || {
143+
print_error_and_exit "Failed to set permissions on environment file: $ENV_FILE_PATH"
144+
}
145+
146+
chown root:root "$ENV_FILE_PATH" || {
147+
print_warning "Failed to set ownership on environment file (non-critical): $ENV_FILE_PATH"
148+
}
149+
150+
print_success "Environment file created: $ENV_FILE_PATH"
151+
echo " 📝 Edit this file to customize settings:"
152+
echo " sudo nano $ENV_FILE_PATH"
153+
echo " 🔒 File permissions: $(stat -c '%a %U:%G' "$ENV_FILE_PATH")"
154+
}
155+
156+
# Function to backup environment file if it exists (called from backup_existing_config)
157+
backup_environment_file() {
158+
if [ -f "$ENV_FILE_PATH" ]; then
159+
local backup_file="$backup_dir/$(basename "$ENV_FILE_PATH")"
160+
cp "$ENV_FILE_PATH" "$backup_file"
161+
echo "Backed up environment file to $backup_dir/"
162+
fi
163+
}
164+
95165
# Function to perform preflight checks
96166
preflight_checks() {
97167
echo "🔍 Performing preflight checks..."
@@ -173,11 +243,54 @@ preflight_checks() {
173243
echo "✅ GUI dialog tools available"
174244
fi
175245

246+
# Check if target directories are writable (simulate what the script will do)
247+
local test_user
248+
if [[ -n "${TARGET_USER:-}" ]]; then
249+
test_user="$TARGET_USER"
250+
elif [[ -n "${SUDO_USER:-}" ]]; then
251+
test_user="$SUDO_USER"
252+
else
253+
test_user="root"
254+
fi
255+
256+
if id "$test_user" &>/dev/null; then
257+
local test_dir="/home/$test_user/Downloads"
258+
if [[ -d "$test_dir" ]]; then
259+
if [[ -w "$test_dir" ]]; then
260+
echo "✅ Target directory structure looks good: $test_dir"
261+
else
262+
echo "⚠️ WARNING: Target directory not writable: $test_dir"
263+
echo " The service may fail to create the tailscale subdirectory."
264+
echo " Check permissions: ls -ld $test_dir"
265+
((issues_found++))
266+
fi
267+
else
268+
echo "⚠️ WARNING: Downloads directory doesn't exist: $test_dir"
269+
echo " It will be created during installation."
270+
fi
271+
fi
272+
273+
# Check for existing installation
274+
if [ -f "$SERVICE_FILE_PATH" ] || [ -f "$DEST_SCRIPT_PATH" ] || [ -f "$ENV_FILE_PATH" ]; then
275+
echo "ℹ️ Existing installation detected."
276+
echo " A backup will be created during installation."
277+
fi
278+
279+
# Check available disk space
280+
local available_space
281+
available_space=$(df /usr/local | awk 'NR==2 {print int($4/1024)}') # KB to MB
282+
if [[ $available_space -lt 50 ]]; then
283+
echo "⚠️ WARNING: Low disk space available: ${available_space}MB"
284+
echo " At least 50MB recommended for installation and logs."
285+
((issues_found++))
286+
fi
287+
176288
# Summary
177289
if [ $issues_found -eq 0 ]; then
178290
echo "✅ All preflight checks passed!"
179291
else
180-
echo "⚠️ Some optional checks failed, but installation can proceed."
292+
echo "⚠️ $issues_found issue(s) found. Review warnings above."
293+
echo " Installation can proceed but may have limited functionality."
181294
fi
182295

183296
echo ""
@@ -277,22 +390,15 @@ get_target_user() {
277390
create_receiver_script() {
278391
local target_user="$1"
279392
local target_dir="/home/$target_user/Downloads/tailscale"
280-
393+
281394
# Create the target directory and set ownership
282395
mkdir -p "$target_dir"
283396
chown "$target_user:$target_user" "$target_dir"
284-
285-
# Read the source script and replace the configuration
286-
local script_content
287-
script_content=$(cat "$SOURCE_SCRIPT")
288-
289-
# Replace the configuration section
290-
script_content=$(echo "$script_content" | sed "s|TARGET_DIR=.*|TARGET_DIR=\"$target_dir/\"|")
291-
script_content=$(echo "$script_content" | sed "s|FIX_OWNER=.*|FIX_OWNER=\"$target_user\"|")
292-
293-
# Write the configured script
294-
echo "$script_content" > "$DEST_SCRIPT_PATH"
295-
chmod +x "$DEST_SCRIPT_PATH"
397+
398+
# Copy the script without modification (configuration via environment file)
399+
install -m 0755 "$SOURCE_SCRIPT" "$DEST_SCRIPT_PATH" || {
400+
print_error_and_exit "Failed to install receiver script to '$DEST_SCRIPT_PATH'"
401+
}
296402
}
297403

298404
# Function to show interactive setup wizard
@@ -424,13 +530,15 @@ fi
424530
# 6. Create and configure the receiver script
425531
echo "➡️ Installing the receiver script..."
426532
create_receiver_script "$TARGET_USER"
427-
print_success "Receiver script installed to '$DEST_SCRIPT_PATH' (configured for user: $TARGET_USER)."
533+
print_success "Receiver script installed to '$DEST_SCRIPT_PATH'."
428534

429-
# 7. Copy and set permissions for the sender script (for Dolphin context menu)
535+
# 6. Create environment configuration file
536+
create_environment_file "$TARGET_USER"
537+
538+
# 7. Install the sender script (for Dolphin context menu)
430539
if [ -f "$SEND_SOURCE_SCRIPT" ]; then
431540
echo "➡️ Installing the sender script..."
432-
cp "$SEND_SOURCE_SCRIPT" "$DEST_SEND_SCRIPT_PATH" || print_error_and_exit "Failed to copy script to '$DEST_SEND_SCRIPT_PATH'."
433-
chmod +x "$DEST_SEND_SCRIPT_PATH" || print_error_and_exit "Failed to make sender script executable."
541+
install -m 0755 "$SEND_SOURCE_SCRIPT" "$DEST_SEND_SCRIPT_PATH" || print_error_and_exit "Failed to install sender script to '$DEST_SEND_SCRIPT_PATH'."
434542
print_success "Sender script installed to '$DEST_SEND_SCRIPT_PATH'."
435543
else
436544
echo "⚠️ Sender script '$SEND_SOURCE_SCRIPT' not found. Skipping send integration."
@@ -449,6 +557,7 @@ Wants=network-online.target
449557
Type=simple
450558
User=root
451559
Group=root
560+
EnvironmentFile=-$ENV_FILE_PATH
452561
ExecStart=$DEST_SCRIPT_PATH
453562
Restart=on-failure
454563
RestartSec=15s
@@ -483,7 +592,7 @@ if [ -f "$DEST_SEND_SCRIPT_PATH" ]; then
483592
echo "➡️ Installing Dolphin service menu..."
484593
mkdir -p "$SYS_KIO_SERVICEMENU_DIR" "$SYS_KSERVICES5_SERVICEMENU_DIR"
485594

486-
# Write a single .desktop content
595+
# Create desktop service menu file
487596
DESKTOP_CONTENT='[Desktop Entry]
488597
Type=Service
489598
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
@@ -496,8 +605,14 @@ Icon=network-workgroup
496605
Exec=/usr/local/bin/tailscale-send.sh %F
497606
'
498607

499-
echo "$DESKTOP_CONTENT" > "$SYS_KIO_SERVICEMENU_DIR/tailscale-send.desktop" || print_error_and_exit "Failed to write service menu (.kio)."
500-
echo "$DESKTOP_CONTENT" > "$SYS_KSERVICES5_SERVICEMENU_DIR/tailscale-send.desktop" || print_error_and_exit "Failed to write service menu (kservices5)."
608+
# Create temporary file and install it
609+
TMP_DESKTOP_FILE=$(mktemp) || print_error_and_exit "Failed to create temporary file for desktop menu."
610+
echo "$DESKTOP_CONTENT" > "$TMP_DESKTOP_FILE" || print_error_and_exit "Failed to write desktop content to temporary file."
611+
612+
install -m 0644 "$TMP_DESKTOP_FILE" "$SYS_KIO_SERVICEMENU_DIR/tailscale-send.desktop" || print_error_and_exit "Failed to install service menu (.kio)."
613+
install -m 0644 "$TMP_DESKTOP_FILE" "$SYS_KSERVICES5_SERVICEMENU_DIR/tailscale-send.desktop" || print_error_and_exit "Failed to install service menu (kservices5)."
614+
615+
rm -f "$TMP_DESKTOP_FILE"
501616
print_success "Dolphin service menu installed."
502617

503618
# Rebuild KDE service cache if tools exist

0 commit comments

Comments
 (0)