summaryrefslogtreecommitdiff
path: root/build-repo.sh
blob: 3a17d2e74974f1eee323c8198979a72a6859d704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash

# Script to build and maintain a Mesk repository
# Automatically discovers, validates, and indexes .mesk packages

set -e  # Exit on any error

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="${1:-$SCRIPT_DIR/repo}"  # Use first argument or default to 'repo'
SOURCE_DIR="${2:-$SCRIPT_DIR/packages}"  # Directory to scan for .mesk files
MESK_BIN="${MESK_BIN:-$SCRIPT_DIR/target/release/mesk}"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if mesk binary exists
if [ ! -f "$MESK_BIN" ]; then
    log_error "Mesk binary not found at: $MESK_BIN"
    log_info "Looking for mesk in PATH..."
    MESK_BIN=$(which mesk 2>/dev/null || echo "")
    if [ -z "$MESK_BIN" ]; then
        log_error "Mesk binary not found in PATH either."
        log_info "Please build mesk first with: cargo build --release"
        exit 1
    else
        log_info "Found mesk at: $MESK_BIN"
    fi
else
    log_info "Using mesk binary: $MESK_BIN"
fi

# Create directories if they don't exist
mkdir -p "$REPO_DIR"
mkdir -p "$SOURCE_DIR"

# Function to validate a .mesk package
validate_package() {
    local package="$1"
    log_info "Validating package: $(basename "$package")"
    
    if "$MESK_BIN" validate "$package" >/dev/null 2>&1; then
        log_success "Package $(basename "$package") is valid"
        return 0
    else
        log_error "Package $(basename "$package") is invalid"
        return 1
    fi
}

# Main function to build repository
build_repository() {
    log_info "Starting repository build process..."
    log_info "Repository directory: $REPO_DIR"
    log_info "Source directory: $SOURCE_DIR"
    
    # Find all .mesk files in source directory (recursively)
    local package_files=()
    while IFS= read -r -d '' file; do
        package_files+=("$file")
    done < <(find "$SOURCE_DIR" -name "*.mesk" -type f -print0)
    
    if [ ${#package_files[@]} -eq 0 ]; then
        log_warning "No .mesk packages found in $SOURCE_DIR"
        log_info "Place your .mesk files in the source directory and run again"
        
        # Still generate index even if no packages
        log_info "Generating empty repository index..."
        if "$MESK_BIN" gen-index "$REPO_DIR"; then
            log_success "Empty repository index generated"
        else
            log_error "Failed to generate empty repository index"
            exit 1
        fi
        
        return 0
    fi
    
    log_info "Found ${#package_files[@]} .mesk package(s) to process"
    
    # Validate packages first
    local valid_packages=()
    local invalid_count=0
    
    for package in "${package_files[@]}"; do
        if validate_package "$package"; then
            valid_packages+=("$package")
        else
            ((invalid_count++))
        fi
    done
    
    if [ $invalid_count -ne 0 ]; then
        log_warning "$invalid_count package(s) failed validation"
        log_info "Only processing valid packages"
    fi
    
    if [ ${#valid_packages[@]} -eq 0 ]; then
        log_error "No valid packages to process"
        log_info "Repository build failed"
        exit 1
    fi
    
    # Copy valid packages to repository directory
    log_info "Copying valid packages to repository..."
    for package in "${valid_packages[@]}"; do
        local package_name=$(basename "$package")
        local dest_path="$REPO_DIR/$package_name"
        
        # Compare timestamps to avoid unnecessary copies
        if [ ! -f "$dest_path" ] || [ "$package" -nt "$dest_path" ]; then
            cp "$package" "$REPO_DIR/"
            log_info "Copied $(basename "$package") to repository"
        else
            log_info "Package $(basename "$package") already exists and is up to date"
        fi
    done
    
    # Generate repository index
    log_info "Generating repository index..."
    if "$MESK_BIN" gen-index "$REPO_DIR"; then
        log_success "Repository index generated successfully"
    else
        log_error "Failed to generate repository index"
        exit 1
    fi
    
    # Create/update repository info file
    local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    cat > "$REPO_DIR/REPO_INFO" << EOF
# Repository Information
Generated At: $timestamp
Total Packages: ${#valid_packages[@]}
Invalid Packages: $invalid_count
Source Directory: $SOURCE_DIR
Repository Directory: $REPO_DIR

Valid Packages:
EOF
    
    for package in "${valid_packages[@]}"; do
        echo "- $(basename "$package")" >> "$REPO_DIR/REPO_INFO"
    done
    
    log_success "Repository build completed successfully!"
    log_info "Repository location: $REPO_DIR"
    log_info "Packages processed: ${#valid_packages[@]}"
    if [ $invalid_count -ne 0 ]; then
        log_info "Invalid packages: $invalid_count"
    fi
}

# Function to show help
show_help() {
    echo "Usage: $0 [REPO_DIR] [SOURCE_DIR]"
    echo ""
    echo "Automatically builds a Mesk repository from .mesk packages."
    echo ""
    echo "Arguments:"
    echo "  REPO_DIR    Directory to create/build repository (default: ./repo)"
    echo "  SOURCE_DIR  Directory containing .mesk files to add (default: ./packages)"
    echo ""
    echo "Environment:"
    echo "  MESK_BIN    Path to mesk binary (default: ./target/release/mesk or in PATH)"
    echo ""
    echo "Example:"
    echo "  $0 /path/to/repo /path/to/packages"
    echo "  $0" # will use defaults
}

# Parse command line options
case "${1:-}" in
    -h|--help)
        show_help
        exit 0
        ;;
    *)
        build_repository
        ;;
esac