# Makefile for PowerShell script creation and timestamp management
# Author: Claude Code
# Description: Provides targets to create and manage PowerShell script files with standard header

.PHONY: help init clean list status install test pull push

# Default target
help:
	@echo "Usage: make <target>"
	@echo ""
	@echo "Targets:"
	@echo "  init <name>.ps1  - Create or update a PowerShell script"
	@echo "  list             - List all PowerShell scripts in the project"
	@echo "  install          - Install scripts to PowerShell profile directory"
	@echo "  test             - Test PowerShell scripts for syntax errors"
	@echo "  status           - Show git status"
	@echo "  clean            - Remove temporary files"
	@echo "  pull             - Pull changes from origin pwsh branch"
	@echo "  push             - Push changes to origin pwsh branch (e.g., make push ARGS='-f')"
	@echo ""

# Create or update a powershell script
init:
	@$(eval SCRIPT_NAME := $(word 3,$(MAKECMDGOALS)))
	@if [ -z "$(SCRIPT_NAME)" ]; then \
		echo "Error: Script name required"; \
		echo "Usage: make init <script_name>.ps1"; \
		exit 1; \
	fi
	@if [ ! -f "$(SCRIPT_NAME)" ]; then \
		echo "Creating pwsh script: $(SCRIPT_NAME)"; \
		echo "#" >> "$(SCRIPT_NAME)"; \
		echo "# File: $(SCRIPT_NAME)" >> "$(SCRIPT_NAME)"; \
		echo "# Description: " >> "$(SCRIPT_NAME)"; \
		echo "# Created: $$(date '+%Y-%m-%d %H:%M:%S')" >> "$(SCRIPT_NAME)"; \
		echo "# Updated: $$(date '+%Y-%m-%d %H:%M:%S')" >> "$(SCRIPT_NAME)"; \
		echo "#" >> "$(SCRIPT_NAME)"; \
		echo "" >> "$(SCRIPT_NAME)"; \
		chmod +x "$(SCRIPT_NAME)"; \
		echo "Created $(SCRIPT_NAME) with executable permissions"; \
	else \
		echo "Updating timestamp for existing script: $(SCRIPT_NAME)"; \
		FILE_TIME=$$(stat -c %y "$(SCRIPT_NAME)" | cut -d' ' -f1,2 | cut -d'.' -f1); \
		sed "s/# Updated: .*/# Updated: $$FILE_TIME/" "$(SCRIPT_NAME)" > "$(SCRIPT_NAME).tmp" && \
		mv "$(SCRIPT_NAME).tmp" "$(SCRIPT_NAME)"; \
		echo "Updated $(SCRIPT_NAME) with file modification time: $$FILE_TIME"; \
	fi

# Clean temporary files
clean:
	@rm -f *.ps1.tmp
	@echo "Removed temporary files"

# List all PowerShell scripts
list:
	@echo "PowerShell scripts in this project:"
	@find . -name "*.ps1" -type f | sort

# Show git status
status:
	@git status --short

# Install scripts to PowerShell profile directory
install:
	@echo "Installing scripts to PowerShell profile directory..."
	@if [ -z "$$PROFILE" ]; then \
		echo "Error: \$\$PROFILE is not set"; \
		echo "Please run this in PowerShell, not bash"; \
		exit 1; \
	fi
	@PROFILE_DIR=$$(dirname "$$PROFILE"); \
	echo "Profile directory: $$PROFILE_DIR"; \
	mkdir -p "$$PROFILE_DIR"; \
	cp -v profile.ps1 "$$PROFILE_DIR/"; \
	cp -v claude.ps1 "$$PROFILE_DIR/"; \
	echo "Installation complete. Restart PowerShell to load the new profile."

# Test PowerShell scripts for syntax errors
test:
	@echo "Testing PowerShell scripts..."
	@if command -v pwsh >/dev/null 2>&1; then \
		for script in *.ps1; do \
			if [ -f "$$script" ]; then \
				echo "Testing $$script..."; \
				pwsh -Command "Get-Content $$script | Out-Null" 2>&1; \
				if [ $$? -eq 0 ]; then \
					echo "  ✓ $$script is valid"; \
				else \
					echo "  ✗ $$script has errors"; \
				fi; \
			fi; \
		done; \
	else \
		echo "Error: PowerShell (pwsh) is not installed or not in PATH"; \
		echo "Skipping syntax validation"; \
	fi

push:
	@echo "Pushing from origin pwsh with arguments: $(ARGS)"
	git push origin main $(ARGS)

pull:
	@echo "Pulling to origin pwsh with arguments: $(ARGS)"
	git pull origin main $(ARGS)	

