/ Makefile
Makefile
  1  # KeepSync Makefile
  2  # Provides standard make commands that integrate with our build scripts
  3  
  4  # Variables
  5  SHELL := /bin/bash
  6  PROJECT_NAME := keepsync
  7  VERSION := 1.0.0
  8  BUILD_DIR := bin
  9  DIST_DIR := dist
 10  SCRIPTS_DIR := scripts
 11  
 12  # Installation directories (auto-detected based on privileges)
 13  ifeq ($(shell id -u), 0)
 14  	# Running as root/sudo - system-wide installation
 15  	INSTALL_DIR := /usr/local/bin
 16  	SERVICE_DIR := /etc/systemd/system
 17  	CONFIG_DIR := /etc/keepsync
 18  	DATA_DIR := /var/lib/keepsync
 19  	INSTALL_TYPE := system
 20  else
 21  	# Running as regular user - user installation
 22  	INSTALL_DIR := $(HOME)/.local/bin
 23  	SERVICE_DIR := $(HOME)/.config/systemd/user
 24  	CONFIG_DIR := $(HOME)/.config/keepsync
 25  	DATA_DIR := $(HOME)/.local/share/keepsync
 26  	INSTALL_TYPE := user
 27  endif
 28  
 29  # Default target
 30  .PHONY: all
 31  all: build
 32  
 33  # Build the project
 34  .PHONY: build
 35  build:
 36  	@echo "Building $(PROJECT_NAME)..."
 37  	@chmod +x $(SCRIPTS_DIR)/integrated-build.sh
 38  	@$(SCRIPTS_DIR)/integrated-build.sh build
 39  	@echo "Build complete. Binary is in $(BUILD_DIR)/$(PROJECT_NAME)"
 40  
 41  # Build for all platforms
 42  .PHONY: build-all
 43  build-all:
 44  	@echo "Building $(PROJECT_NAME) for all platforms..."
 45  	@chmod +x $(SCRIPTS_DIR)/integrated-build.sh
 46  	@$(SCRIPTS_DIR)/integrated-build.sh build-all
 47  	@echo "Build complete. Binaries are in $(BUILD_DIR)/"
 48  
 49  # Install the binary and systemd service
 50  .PHONY: install
 51  install: build
 52  	@echo "Installing $(PROJECT_NAME) ($(INSTALL_TYPE) installation)..."
 53  	@mkdir -p $(INSTALL_DIR) $(SERVICE_DIR) $(CONFIG_DIR) $(DATA_DIR)
 54  	@cp $(BUILD_DIR)/$(PROJECT_NAME) $(INSTALL_DIR)/
 55  	@chmod +x $(INSTALL_DIR)/$(PROJECT_NAME)
 56  	@echo "Binary installed to $(INSTALL_DIR)/$(PROJECT_NAME)"
 57  	@# Install systemd service
 58  	@sed 's|INSTALL_DIR|$(INSTALL_DIR)|g' $(SCRIPTS_DIR)/keepsync.service > $(SERVICE_DIR)/keepsync.service
 59  	@echo "Service file installed to $(SERVICE_DIR)/keepsync.service"
 60  ifeq ($(INSTALL_TYPE), system)
 61  	@# System installation - setup TPM access
 62  	@if [ -e /dev/tpmrm0 ]; then \
 63  		echo "# KeepSync TPM access" > /etc/udev/rules.d/99-keepsync-tpm.rules; \
 64  		echo 'KERNEL=="tpmrm0", GROUP="tss", MODE="0664"' >> /etc/udev/rules.d/99-keepsync-tpm.rules; \
 65  		udevadm control --reload-rules; \
 66  		echo "TPM udev rules configured for user access"; \
 67  	fi
 68  	@# Create keepsync user if it doesn't exist
 69  	@if ! getent passwd keepsync > /dev/null 2>&1; then \
 70  		useradd --system --shell /bin/false --home-dir /var/lib/keepsync --create-home keepsync; \
 71  		echo "Created keepsync system user"; \
 72  	fi
 73  	@# Add keepsync user to tss group for TPM access
 74  	@if getent group tss > /dev/null 2>&1; then \
 75  		usermod -a -G tss keepsync; \
 76  		echo "Added keepsync user to tss group for TPM access"; \
 77  	else \
 78  		echo "Note: 'tss' group not found - TPM will use simulator mode"; \
 79  	fi
 80  	@systemctl daemon-reload
 81  	@echo "Service installed. Enable with: sudo systemctl enable keepsync"
 82  else
 83  	@# User installation - enable user systemd service
 84  	@systemctl --user daemon-reload
 85  	@systemctl --user enable keepsync.service
 86  	@echo "User service enabled."
 87  endif
 88  	@echo ""
 89  	@echo "✅ Installation complete!"
 90  	@echo "   Type: $(INSTALL_TYPE)"
 91  	@echo "   Binary: $(INSTALL_DIR)/$(PROJECT_NAME)"
 92  	@echo "   Config: $(CONFIG_DIR)"
 93  	@echo "   Data: $(DATA_DIR)"
 94  	@echo ""
 95  	@echo "Next steps:"
 96  ifeq ($(INSTALL_TYPE), system)
 97  	@echo "   sudo systemctl enable keepsync   # Enable service"
 98  	@echo "   sudo systemctl start keepsync    # Start service"
 99  else
100  	@echo "   systemctl --user start keepsync  # Start service"
101  endif
102  
103  # Uninstall the binary and systemd service
104  .PHONY: uninstall
105  uninstall:
106  	@echo "Uninstalling $(PROJECT_NAME) ($(INSTALL_TYPE) installation)..."
107  ifeq ($(INSTALL_TYPE), system)
108  	@# System uninstallation
109  	@if systemctl is-active --quiet keepsync; then \
110  		sudo systemctl stop keepsync; \
111  		echo "Service stopped"; \
112  	fi
113  	@if systemctl is-enabled --quiet keepsync; then \
114  		sudo systemctl disable keepsync; \
115  		echo "Service disabled"; \
116  	fi
117  	@if [ -f $(SERVICE_DIR)/keepsync.service ]; then \
118  		rm -f $(SERVICE_DIR)/keepsync.service; \
119  		echo "Service file removed"; \
120  	fi
121  	@if [ -f /etc/udev/rules.d/99-keepsync-tpm.rules ]; then \
122  		rm -f /etc/udev/rules.d/99-keepsync-tpm.rules; \
123  		udevadm control --reload-rules; \
124  		echo "TPM udev rules removed"; \
125  	fi
126  	@systemctl daemon-reload
127  else
128  	@# User uninstallation
129  	@if systemctl --user is-active --quiet keepsync; then \
130  		systemctl --user stop keepsync; \
131  		echo "User service stopped"; \
132  	fi
133  	@if systemctl --user is-enabled --quiet keepsync; then \
134  		systemctl --user disable keepsync; \
135  		echo "User service disabled"; \
136  	fi
137  	@if [ -f $(SERVICE_DIR)/keepsync.service ]; then \
138  		rm -f $(SERVICE_DIR)/keepsync.service; \
139  		echo "User service file removed"; \
140  	fi
141  	@systemctl --user daemon-reload
142  endif
143  	@# Remove binary
144  	@if [ -f $(INSTALL_DIR)/$(PROJECT_NAME) ]; then \
145  		rm -f $(INSTALL_DIR)/$(PROJECT_NAME); \
146  		echo "Binary removed from $(INSTALL_DIR)"; \
147  	fi
148  	@# Ask about removing data (optional)
149  	@echo ""
150  	@echo "Do you want to remove configuration and data directories?"
151  	@echo "Config: $(CONFIG_DIR)"
152  	@echo "Data: $(DATA_DIR)"
153  	@read -p "Remove data? [y/N]: " confirm; \
154  	if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then \
155  		if [ -d $(CONFIG_DIR) ]; then rm -rf $(CONFIG_DIR); echo "Config directory removed"; fi; \
156  		if [ -d $(DATA_DIR) ]; then rm -rf $(DATA_DIR); echo "Data directory removed"; fi; \
157  	else \
158  		echo "Data directories preserved"; \
159  	fi
160  	@echo ""
161  	@echo "✅ Uninstallation complete!"
162  	@echo "   $(PROJECT_NAME) has been removed from your system."
163  
164  # Clean build artifacts
165  .PHONY: clean
166  clean:
167  	@echo "Cleaning build artifacts..."
168  	@rm -rf $(BUILD_DIR)/
169  	@rm -rf $(DIST_DIR)/
170  	@rm -f *.log
171  	@rm -f *.tmp
172  	@echo "Build artifacts cleaned."
173  
174  # Full repository cleanup (removes obsolete content)
175  .PHONY: clean-full
176  clean-full:
177  	@echo "Running full repository cleanup..."
178  	@chmod +x $(SCRIPTS_DIR)/enhanced-keepsync-cleanup.sh
179  	@$(SCRIPTS_DIR)/enhanced-keepsync-cleanup.sh --no-confirm
180  	@echo "Full cleanup complete."
181  
182  # Run tests
183  .PHONY: test
184  test:
185  	@echo "Running tests..."
186  	@chmod +x $(SCRIPTS_DIR)/integrated-build.sh
187  	@$(SCRIPTS_DIR)/integrated-build.sh test
188  	@echo "Tests complete."
189  
190  # Run integration tests
191  .PHONY: integration
192  integration:
193  	@echo "Running integration tests..."
194  	@chmod +x $(SCRIPTS_DIR)/integrated-build.sh
195  	@$(SCRIPTS_DIR)/integrated-build.sh integration
196  	@echo "Integration tests complete."
197  
198  # Create distribution packages
199  .PHONY: package
200  package: build-all
201  	@echo "Creating distribution packages..."
202  	@chmod +x $(SCRIPTS_DIR)/integrated-build.sh
203  	@$(SCRIPTS_DIR)/integrated-build.sh package
204  	@echo "Packages created in $(DIST_DIR)/"
205  
206  # Fix import paths
207  .PHONY: fix-imports
208  fix-imports:
209  	@echo "Fixing import paths..."
210  	@chmod +x $(SCRIPTS_DIR)/fix-import-paths.sh
211  	@$(SCRIPTS_DIR)/fix-import-paths.sh
212  	@echo "Import paths fixed."
213  
214  # Fix package structure
215  .PHONY: fix-packages
216  fix-packages:
217  	@echo "Fixing package structure..."
218  	@chmod +x $(SCRIPTS_DIR)/fix-package-structure.sh
219  	@$(SCRIPTS_DIR)/fix-package-structure.sh
220  	@echo "Package structure fixed."
221  
222  # Update dependencies
223  .PHONY: deps
224  deps:
225  	@echo "Updating dependencies..."
226  	@chmod +x $(SCRIPTS_DIR)/update-dependencies.sh
227  	@$(SCRIPTS_DIR)/update-dependencies.sh
228  	@echo "Dependencies updated."
229  
230  # Make all scripts executable
231  .PHONY: scripts-executable
232  scripts-executable:
233  	@echo "Making scripts executable..."
234  	@chmod +x $(SCRIPTS_DIR)/make-scripts-executable.sh
235  	@$(SCRIPTS_DIR)/make-scripts-executable.sh
236  	@echo "Scripts are now executable."
237  
238  # Initialize the project (fix everything and prepare for build)
239  .PHONY: init
240  init: scripts-executable fix-imports fix-packages deps
241  	@echo "Project initialized and ready for build."
242  
243  # Help target
244  .PHONY: help
245  help:
246  	@echo "KeepSync Makefile"
247  	@echo ""
248  	@echo "Usage: make [target]"
249  	@echo ""
250  	@echo "Targets:"
251  	@echo "  all               Build the project (default)"
252  	@echo "  build             Build the project for the current platform"
253  	@echo "  build-all         Build the project for all platforms"
254  	@echo "  install           Install the binary and systemd service"
255  	@echo "  uninstall         Uninstall the binary and systemd service"
256  	@echo "  clean             Clean build artifacts"
257  	@echo "  clean-full        Full repository cleanup (removes obsolete content)"
258  	@echo "  test              Run tests"
259  	@echo "  integration       Run integration tests"
260  	@echo "  package           Create distribution packages"
261  	@echo "  fix-imports       Fix import paths"
262  	@echo "  fix-packages      Fix package structure"
263  	@echo "  deps              Update dependencies"
264  	@echo "  scripts-executable Make all scripts executable"
265  	@echo "  init              Initialize the project (fix everything and prepare for build)"
266  	@echo "  help              Show this help message"
267  	@echo ""
268  	@echo "Example: make build"