#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' print_status() { echo -e "${BLUE}[INFO]${NC} $1"; } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; } print_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Global variable for commit message COMMIT_MESSAGE="" # Parse command line arguments for -m flag while [[ $# -gt 0 ]]; do case $1 in -m|--message) COMMIT_MESSAGE="$2" shift 2 ;; *) # Keep other arguments for main logic break ;; esac done # Function to automatically increment version increment_version() { print_status "Incrementing version..." # Check if we're in a git repository if ! git rev-parse --git-dir > /dev/null 2>&1; then print_warning "Not in a git repository - skipping version increment" return 0 fi # Get the highest version tag (not chronologically latest) LATEST_TAG=$(git tag -l 'v*.*.*' | sort -V | tail -n 1 || echo "v0.1.0") if [[ -z "$LATEST_TAG" ]]; then LATEST_TAG="v0.1.0" fi # Extract version components (remove 'v' prefix) VERSION=${LATEST_TAG#v} # Parse major.minor.patch using regex if [[ $VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then MAJOR=${BASH_REMATCH[1]} MINOR=${BASH_REMATCH[2]} PATCH=${BASH_REMATCH[3]} else print_error "Invalid version format in tag: $LATEST_TAG" print_error "Expected format: v0.1.0" return 1 fi # Increment patch version NEW_PATCH=$((PATCH + 1)) NEW_VERSION="v${MAJOR}.${MINOR}.${NEW_PATCH}" print_status "Current version: $LATEST_TAG" print_status "New version: $NEW_VERSION" # Stage all changes if git add . 2>/dev/null; then print_success "Staged all changes" else print_warning "Failed to stage changes (maybe not a git repository)" fi # Handle commit message - use global variable if set, otherwise prompt if [[ -z "$COMMIT_MESSAGE" ]]; then echo "" print_status "Please enter a meaningful commit message for version $NEW_VERSION:" echo -n "> " read -r COMMIT_MESSAGE fi # Check if user provided a message if [[ -z "$COMMIT_MESSAGE" ]]; then print_warning "No commit message provided. Using default message." COMMIT_MESSAGE="Automatic version increment" fi # Commit changes with user-provided message if git commit -m "Version $NEW_VERSION - $COMMIT_MESSAGE" 2>/dev/null; then print_success "Committed changes for version $NEW_VERSION" else print_warning "Failed to commit changes (maybe no changes to commit or not a git repository)" fi # Create new git tag if git tag "$NEW_VERSION" 2>/dev/null; then print_success "Created new version tag: $NEW_VERSION" # Push changes and tags to remote repository if git push 2>/dev/null; then print_success "Pushed changes to remote repository" else print_warning "Failed to push changes to remote repository" fi if git push --tags 2>/dev/null; then print_success "Pushed tags to remote repository" else print_warning "Failed to push tags to remote repository" fi else print_warning "Tag $NEW_VERSION already exists - using existing version" NEW_VERSION=$LATEST_TAG # Re-extract version components for existing tag VERSION=${NEW_VERSION#v} if [[ $VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then MAJOR=${BASH_REMATCH[1]} MINOR=${BASH_REMATCH[2]} NEW_PATCH=${BASH_REMATCH[3]} fi fi # Update version strings in source code update_source_version "$NEW_VERSION" print_success "Version updated to ${NEW_VERSION}" } # Function to update version strings in source code update_source_version() { local NEW_VERSION="$1" print_status "Updating version strings in source code..." # Replace hardcoded version strings in otp.c with the current git tag if [ -f "otp.c" ]; then # Update main menu version sed -i "s/OTP v[0-9]\+\.[0-9]\+\.[0-9]\+/OTP $NEW_VERSION/g" otp.c # Update ASCII output version sed -i "s/Version: v[0-9]\+\.[0-9]\+\.[0-9]\+/Version: $NEW_VERSION/g" otp.c # Update usage/help text version sed -i "s/Implementation v[0-9]\+\.[0-9]\+\.[0-9]\+/Implementation $NEW_VERSION/g" otp.c print_success "Updated version strings in otp.c to $NEW_VERSION" else print_warning "otp.c not found - skipping version string updates" fi } # Build functions build_project() { print_status "Cleaning previous build..." make clean increment_version print_status "Building OTP project..." make if [ $? -eq 0 ]; then print_success "Build completed successfully" else print_error "Build failed" return 1 fi } build_static() { print_status "Cleaning previous build..." make clean increment_version print_status "Building OTP project with static linking..." make static if [ $? -eq 0 ]; then print_success "Static build completed successfully" else print_error "Static build failed" return 1 fi } clean_project() { print_status "Cleaning build artifacts..." make clean print_success "Clean completed" } install_project() { print_status "Installing OTP project..." make install if [ $? -eq 0 ]; then print_success "Installation completed" else print_error "Installation failed" return 1 fi } uninstall_project() { print_status "Uninstalling OTP project..." make uninstall if [ $? -eq 0 ]; then print_success "Uninstallation completed" else print_error "Uninstallation failed" return 1 fi } # Main script logic case "${1:-build}" in build) build_project ;; static) build_static ;; clean) clean_project ;; install) install_project ;; uninstall) uninstall_project ;; *) echo "OTP Cipher Build Script" echo "Usage: $0 [-m \"commit message\"] {build|static|clean|install|uninstall}" echo "" echo "Options:" echo " -m, --message \"text\" - Specify commit message (skips interactive prompt)" echo "" echo "Commands:" echo " build - Build project with automatic version increment (default)" echo " static - Build with static linking and version increment" echo " clean - Clean build artifacts" echo " install - Install to system (requires build first)" echo " uninstall - Remove from system" echo "" echo "Examples:" echo " $0" echo " $0 -m \"Fixed checksum parsing bug\"" echo " $0 --message \"Added new feature\" static" exit 1 ;; esac