#!/usr/bin/env bash # Link this workspace's output/ directory to the current user's shared output. # # Usage: # bash scripts/use_shared_output.sh # CHARTPIPE_SHARED_OUTPUT=/path/to/shared_output bash scripts/use_shared_output.sh # CHARTPIPE_SHARED_USER=alice bash scripts/use_shared_output.sh # bash scripts/use_shared_output.sh /path/to/shared_output set -euo pipefail umask 0002 DEFAULT_SHARED_ROOT="/data/liduan/ChartPipeline/ChartPipeline/shared_output" SHARED_ROOT="${1:-${CHARTPIPE_SHARED_OUTPUT:-$DEFAULT_SHARED_ROOT}}" SHARED_USER="${CHARTPIPE_SHARED_USER:-${USER:-$(id -un)}}" USER_OUTPUT_ROOT="$SHARED_ROOT/personal/$SHARED_USER" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)" OUTPUT_PATH="$PROJECT_ROOT/output" canonical_path() { local target="$1" if command -v readlink >/dev/null 2>&1; then readlink -f "$target" 2>/dev/null || printf '%s\n' "$target" else printf '%s\n' "$target" fi } is_empty_dir() { local dir="$1" [ -d "$dir" ] && [ -z "$(ls -A "$dir")" ] } ensure_shared_dirs() { mkdir -p "$SHARED_ROOT"/{archive,beautify,manual,personal} mkdir -p "$USER_OUTPUT_ROOT"/{quality_check,chart_template_samples,d3_regression,regression,manual} chmod g+rwX \ "$SHARED_ROOT" \ "$SHARED_ROOT"/archive \ "$SHARED_ROOT"/beautify \ "$SHARED_ROOT"/manual \ "$SHARED_ROOT"/personal \ "$USER_OUTPUT_ROOT" \ "$USER_OUTPUT_ROOT"/quality_check \ "$USER_OUTPUT_ROOT"/chart_template_samples \ "$USER_OUTPUT_ROOT"/d3_regression \ "$USER_OUTPUT_ROOT"/regression \ "$USER_OUTPUT_ROOT"/manual 2>/dev/null || { echo "[shared-output] WARN: could not chmod group-write on shared output directories" >&2 } local probe="$USER_OUTPUT_ROOT/.write_test.$$" if ! : > "$probe" 2>/dev/null; then echo "[shared-output] ERROR: user output root is not writable: $USER_OUTPUT_ROOT" >&2 exit 1 fi rm -f "$probe" } link_output() { ln -s "$USER_OUTPUT_ROOT" "$OUTPUT_PATH" echo "[shared-output] linked: $OUTPUT_PATH -> $USER_OUTPUT_ROOT" } echo "[shared-output] project root: $PROJECT_ROOT" echo "[shared-output] shared root : $SHARED_ROOT" echo "[shared-output] shared user : $SHARED_USER" echo "[shared-output] user output : $USER_OUTPUT_ROOT" ensure_shared_dirs if [ -L "$OUTPUT_PATH" ]; then current_target="$(readlink "$OUTPUT_PATH")" current_real="$(canonical_path "$OUTPUT_PATH")" user_output_real="$(canonical_path "$USER_OUTPUT_ROOT")" if [ "$current_real" = "$user_output_real" ]; then echo "[shared-output] output already points to user output: $current_target" exit 0 fi echo "[shared-output] ERROR: output is already a symlink to a different target:" >&2 echo " $OUTPUT_PATH -> $current_target" >&2 echo " expected -> $USER_OUTPUT_ROOT" >&2 echo "Move or remove the existing symlink manually, then rerun this script." >&2 exit 2 fi if [ -e "$OUTPUT_PATH" ]; then if is_empty_dir "$OUTPUT_PATH"; then rmdir "$OUTPUT_PATH" link_output exit 0 fi echo "[shared-output] ERROR: output exists and is not an empty directory:" >&2 echo " $OUTPUT_PATH" >&2 echo "This script will not overwrite or migrate existing results automatically." >&2 echo "Recommended migration:" >&2 echo " 1. Create/choose a versioned folder under $SHARED_ROOT/archive/" >&2 echo " 2. Move or copy current output contents there." >&2 echo " 3. Remove the local output directory." >&2 echo " 4. Rerun: bash scripts/use_shared_output.sh" >&2 exit 2 fi link_output