#!/bin/bash

# Initialize Blog Repository Setup
echo "Initializing blog repository setup..."

# Set up Git configuration
git config core.symlinks true

# Create necessary directories
mkdir -p src/content public/blog scripts .git/hooks

# Create symbolic links for src/content
echo "Creating symbolic links for src/content..."
ln -sf /mnt/synology/obsidian/Public/Blog/posts src/content/posts
ln -sf /mnt/synology/obsidian/Public/Blog/projects src/content/projects
ln -sf /mnt/synology/obsidian/Public/Blog/configurations src/content/configurations
ln -sf /mnt/synology/obsidian/Public/Blog/external-posts src/content/external-posts

# Create symbolic links for public/blog
echo "Creating symbolic links for public/blog..."
ln -sf /mnt/synology/obsidian/Public/Blog/configs public/blog/configs
ln -sf /mnt/synology/obsidian/Public/Blog/images public/blog/images
ln -sf /mnt/synology/obsidian/Public/Blog/infrastructure public/blog/infrastructure
ln -sf /mnt/synology/obsidian/Public/Blog/posts public/blog/posts

# Create the pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# Pre-commit hook to process symbolic links

echo "Running pre-commit hook for blog content..."

# Get the absolute path to the script
SCRIPT_PATH="$(git rev-parse --show-toplevel)/scripts/process-content-links.sh"

# Check if the script exists and is executable
if [ -x "$SCRIPT_PATH" ]; then
    bash "$SCRIPT_PATH"
    # Add any new or changed files resulting from the script
    git add -A
else
    echo "Error: Content processing script not found or not executable at $SCRIPT_PATH"
    echo "Please ensure the script exists and has execute permissions"
    exit 1
fi
EOF

# Create the post-commit hook
cat > .git/hooks/post-commit << 'EOF'
#!/bin/bash
# Post-commit hook to restore symbolic links

echo "Running post-commit hook to restore symbolic links..."

# Array of content directories and their targets
declare -A SYMLINK_TARGETS=(
    ["src/content/posts"]="/mnt/synology/obsidian/Public/Blog/posts"
    ["src/content/projects"]="/mnt/synology/obsidian/Public/Blog/projects"
    ["src/content/configurations"]="/mnt/synology/obsidian/Public/Blog/configurations"
    ["src/content/external-posts"]="/mnt/synology/obsidian/Public/Blog/external-posts"
    ["public/blog/configs"]="/mnt/synology/obsidian/Public/Blog/configs"
    ["public/blog/images"]="/mnt/synology/obsidian/Public/Blog/images"
    ["public/blog/infrastructure"]="/mnt/synology/obsidian/Public/Blog/infrastructure"
    ["public/blog/posts"]="/mnt/synology/obsidian/Public/Blog/posts"
)

for dir_path in "${!SYMLINK_TARGETS[@]}"; do
    target="${SYMLINK_TARGETS[$dir_path]}"
    if [ -d "$target" ]; then
        echo "Restoring symlink for $dir_path -> $target"
        rm -rf "$dir_path"
        mkdir -p "$(dirname "$dir_path")"
        ln -s "$target" "$dir_path"
    else
        echo "Warning: Target directory $target does not exist"
    fi
done

echo "Symbolic links restored!"
EOF

# Make hooks executable
chmod +x .git/hooks/pre-commit .git/hooks/post-commit

# Create process-content-links.sh
cat > scripts/process-content-links.sh << 'EOF'
#!/bin/bash

# Script to handle symbolic links before commit
echo "Processing symbolic links for content..."

# Array of content directories to process
declare -A CONTENT_PATHS
# src/content directories
CONTENT_PATHS["posts"]="src/content/posts"
CONTENT_PATHS["projects"]="src/content/projects"
CONTENT_PATHS["configurations"]="src/content/configurations"
CONTENT_PATHS["external-posts"]="src/content/external-posts"
# public/blog directories
CONTENT_PATHS["configs"]="public/blog/configs"
CONTENT_PATHS["images"]="public/blog/images"
CONTENT_PATHS["infrastructure"]="public/blog/infrastructure"
CONTENT_PATHS["blog-posts"]="public/blog/posts"

for dir_name in "${!CONTENT_PATHS[@]}"; do
    dir_path="${CONTENT_PATHS[$dir_name]}"
    if [ -L "$dir_path" ]; then
        echo "Processing $dir_path..."
        target=$(readlink "$dir_path")
        rm "$dir_path"
        mkdir -p "$(dirname "$dir_path")"
        cp -r "$target" "$dir_path"
        git add "$dir_path"
        echo "Processed $dir_path -> $target"
    else
        echo "Skipping $dir_path (not a symbolic link)"
    fi
done

echo "Content processing complete!"
EOF

# Make process-content-links.sh executable
chmod +x scripts/process-content-links.sh

echo "Setup complete! The repository is now configured for automatic symbolic link handling."
