# Updater System - Quick Setup Guide

## 🚀 Installation Steps

### Step 1: Run Migrations
```bash
php artisan migrate
```

### Step 2: Set Application Version
Add to your `.env` file:
```env
APP_VERSION=1.0.0
```

### Step 3: Create Storage Directories
```bash
mkdir -p storage/app/backups
mkdir -p storage/app/updates
mkdir -p storage/app/temp
chmod -R 755 storage/app
```

### Step 4: Verify Permissions
```bash
# Make sure these are writable
chmod -R 755 storage/
chmod -R 755 bootstrap/cache/
```

### Step 5: Clear Caches
```bash
php artisan config:clear
php artisan cache:clear
php artisan route:clear
```

### Step 6: Test Access
Visit: `http://your-domain.com/admin/updater`

---

## 📦 Creating Your First Update Package

### Option 1: Using Command (Recommended)
```bash
php artisan updater:create-package 1.0.1 \
    --title="First Update" \
    --description="Initial system improvements" \
    --changelog="- Fixed bugs\n- Added features"
```

### Option 2: Manual Creation
1. Create folder: `my_update/`
2. Create `update.json`:
```json
{
    "version": "1.0.1",
    "title": "First Update",
    "description": "System improvements",
    "changelog": "- Fixed bugs\n- Added features"
}
```
3. ZIP the folder: `update_v1.0.1.zip`

---

## 🧪 Testing the System

### Test 1: Create Backup
1. Go to `/admin/updater`
2. Click "Create Backup"
3. Check `storage/app/backups/` for ZIP file

### Test 2: Upload Update
1. Click "Upload Update"
2. Select your ZIP file
3. Check if it appears in the list

### Test 3: Install Update (Optional)
1. Click "Install" on the update
2. Wait for completion
3. Verify version changed

---

## ⚙️ Configuration

### PHP Settings (if upload fails)
Edit `php.ini`:
```ini
upload_max_filesize = 500M
post_max_size = 500M
max_execution_time = 300
memory_limit = 256M
```

### Laravel Settings
Already configured in `config/app.php`:
```php
'version' => env('APP_VERSION', '1.0.0'),
```

---

## 🔧 Troubleshooting

### Problem: Cannot create backup
**Solution**:
```bash
mkdir -p storage/app/backups
chmod -R 755 storage/app/backups
```

### Problem: Upload fails
**Solution**:
```bash
# Check PHP limits
php -i | grep upload_max_filesize
php -i | grep post_max_size

# Increase if needed (in php.ini)
upload_max_filesize = 500M
post_max_size = 500M
```

### Problem: Permission denied
**Solution**:
```bash
# Fix permissions
sudo chown -R www-data:www-data storage/
sudo chmod -R 755 storage/
```

### Problem: ZipArchive not found
**Solution**:
```bash
# Install PHP zip extension
sudo apt-get install php-zip
# or
sudo yum install php-zip

# Restart web server
sudo service apache2 restart
# or
sudo service nginx restart
```

---

## ✅ Verification Checklist

- [ ] Migrations run successfully
- [ ] Storage directories exist and writable
- [ ] Can access `/admin/updater`
- [ ] Can create backup successfully
- [ ] Can upload update package
- [ ] PHP extensions installed (zip)
- [ ] File permissions correct
- [ ] Version displays correctly

---

## 📝 Quick Reference

### Important Directories:
- `storage/app/backups/` - Backup files
- `storage/app/updates/` - Update packages
- `storage/app/temp/` - Temporary extraction

### Important Commands:
```bash
# Create package
php artisan updater:create-package [version]

# Clear cache
php artisan cache:clear

# Check logs
tail -f storage/logs/laravel.log

# Fix permissions
chmod -R 755 storage/
```

### Important Routes:
- `/admin/updater` - Main page
- `/admin/updater/create` - Upload page
- `/admin/updater/status` - System status

---

## 🎯 Next Steps

1. ✅ Complete setup steps above
2. ✅ Create a test backup
3. ✅ Create a test update package
4. ✅ Test upload and install
5. ✅ Document your update process
6. ✅ Train your team

---

**Setup Complete!** 🎉

You're ready to use the updater system.

