#!/bin/bash

# Exit on error
set -e

# Change to the website directory
if [ ! -d "/usr/www/amatsuro/manoktok.com" ]; then
    echo "Error: Directory /usr/www/amatsuro/manoktok.com does not exist"
    exit 1
fi

cd /usr/www/amatsuro/manoktok.com

# Create config directory if it doesn't exist
mkdir -p config

# Backup existing configuration if it exists
if [ -f config/application.php ]; then
    cp config/application.php config/application.php.backup
    echo "Created backup of existing configuration"
fi

# Create application.php with configuration
cat > config/application.php << 'EOL'
<?php
return [
    // Database Configuration
    'db' => [
        'host' => 'localhost',
        'username' => 'amatsuro',
        'password' => '@2jz6EaUv43@',
        'database' => 'amatsuro_manoktok'
    ],
    
    // Application Settings
    'app' => [
        'name' => 'Manoktok POS',
        'debug' => false,
        'timezone' => 'Asia/Manila',
        'url' => 'https://manoktok.com',
        'error_log' => true,
        'error_log_file' => __DIR__ . '/logs/auth_error.log'
    ],
    
    // Session Configuration
    'session' => [
        'lifetime' => 7200, // 2 hours
        'path' => '/',
        'domain' => 'manoktok.com',
        'secure' => true,
        'httponly' => true,
        'samesite' => 'Lax'
    ],

    // Google Sign-In Configuration
    'google_client_id' => '950686562587-lqdrvru3b5dr3i404kgrg0vih4d17rom.apps.googleusercontent.com',
    'google_client_secret' => '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',
    'google_redirect_uri' => 'https://manoktok.com/auth.php'
];
EOL

# Create logs directory
mkdir -p config/logs
chmod 755 config/logs

# Set proper permissions
chmod 644 config/application.php

# Verify the file was created
if [ -f config/application.php ]; then
    echo "Configuration file created successfully!"
    echo "Location: $(pwd)/config/application.php"
else
    echo "Error: Failed to create configuration file"
    exit 1
fi 