File size: 7,205 Bytes
8d3cb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash

# Fix Permissions Script for Legal Dashboard
# This script resolves common permission issues

echo "πŸ”§ Legal Dashboard - Permission Fix Script"
echo "=========================================="

# Function to create directory with fallback
create_dir_with_fallback() {
    local dir_path=$1
    local fallback_path=$2
    
    echo "πŸ“ Creating directory: $dir_path"
    
    if mkdir -p "$dir_path" 2>/dev/null && [ -w "$dir_path" ]; then
        echo "βœ… Successfully created: $dir_path"
        return 0
    else
        echo "⚠️  Failed to create $dir_path, trying fallback: $fallback_path"
        if mkdir -p "$fallback_path" 2>/dev/null && [ -w "$fallback_path" ]; then
            echo "βœ… Successfully created fallback: $fallback_path"
            return 0
        else
            echo "❌ Failed to create both primary and fallback directories"
            return 1
        fi
    fi
}

# Check current user and permissions
echo ""
echo "πŸ” System Information:"
echo "  - Current user: $(whoami)"
echo "  - User ID: $(id -u)"
echo "  - Group ID: $(id -g)"
echo "  - Working directory: $(pwd)"
echo "  - Home directory: $HOME"

# Check if running in Docker
if [ -f /.dockerenv ]; then
    echo "  - Environment: Docker Container"
    IN_DOCKER=true
else
    echo "  - Environment: Host System"
    IN_DOCKER=false
fi

# Check if running in HF Spaces
if [ "$SPACE_ID" != "" ]; then
    echo "  - Platform: Hugging Face Spaces"
    IN_HF_SPACES=true
else
    echo "  - Platform: Standard"
    IN_HF_SPACES=false
fi

echo ""
echo "πŸ—οΈ Creating necessary directories..."

# Create directories with fallbacks
if [ "$IN_HF_SPACES" = true ]; then
    # HF Spaces specific paths
    create_dir_with_fallback "/tmp/legal_dashboard/data" "$HOME/legal_dashboard/data"
    create_dir_with_fallback "/tmp/legal_dashboard/cache" "$HOME/legal_dashboard/cache"
    create_dir_with_fallback "/tmp/legal_dashboard/logs" "$HOME/legal_dashboard/logs"
    create_dir_with_fallback "/tmp/legal_dashboard/uploads" "$HOME/legal_dashboard/uploads"
    
    # Set environment variables for HF Spaces
    export DATABASE_DIR="/tmp/legal_dashboard/data"
    export TRANSFORMERS_CACHE="/tmp/legal_dashboard/cache"
    export HF_HOME="/tmp/legal_dashboard/cache"
    
    echo "βœ… HF Spaces directories configured"
    
elif [ "$IN_DOCKER" = true ]; then
    # Docker specific paths
    create_dir_with_fallback "/app/data" "/tmp/app_data"
    create_dir_with_fallback "/app/database" "/tmp/app_database"
    create_dir_with_fallback "/app/cache" "/tmp/app_cache"
    create_dir_with_fallback "/app/logs" "/tmp/app_logs"
    create_dir_with_fallback "/app/uploads" "/tmp/app_uploads"
    create_dir_with_fallback "/app/backups" "/tmp/app_backups"
    
    echo "βœ… Docker directories configured"
    
else
    # Host system paths
    create_dir_with_fallback "./data" "$HOME/legal_dashboard/data"
    create_dir_with_fallback "./cache" "$HOME/legal_dashboard/cache"
    create_dir_with_fallback "./logs" "$HOME/legal_dashboard/logs"
    create_dir_with_fallback "./uploads" "$HOME/legal_dashboard/uploads"
    create_dir_with_fallback "./backups" "$HOME/legal_dashboard/backups"
    
    echo "βœ… Host system directories configured"
fi

# Set permissions where possible
echo ""
echo "πŸ”’ Setting permissions..."

# Function to set permissions safely
safe_chmod() {
    local path=$1
    local perm=$2
    
    if [ -d "$path" ] && [ -w "$path" ]; then
        chmod $perm "$path" 2>/dev/null && echo "  βœ… Set $perm on $path" || echo "  ⚠️  Could not set permissions on $path"
    fi
}

# Apply permissions to existing directories
for dir in "/app/data" "/app/database" "/app/cache" "/app/logs" "/app/uploads" "/app/backups" \
           "/tmp/legal_dashboard" "/tmp/app_data" "/tmp/app_database" "/tmp/app_cache" \
           "$HOME/legal_dashboard" "./data" "./cache" "./logs" "./uploads" "./backups"; do
    safe_chmod "$dir" 755
done

# Test database creation
echo ""
echo "πŸ—„οΈ Testing database creation..."

test_db_creation() {
    local test_dir=$1
    local test_db="$test_dir/test.db"
    
    if [ -d "$test_dir" ] && [ -w "$test_dir" ]; then
        if python3 -c "
import sqlite3
import os
try:
    conn = sqlite3.connect('$test_db')
    conn.execute('CREATE TABLE test (id INTEGER)')
    conn.close()
    os.remove('$test_db')
    print('βœ… Database test successful in $test_dir')
    exit(0)
except Exception as e:
    print('❌ Database test failed in $test_dir: {}'.format(e))
    exit(1)
" 2>/dev/null; then
            echo "$test_dir"  # Return the working directory
            return 0
        fi
    fi
    return 1
}

# Find working database directory
WORKING_DB_DIR=""
for test_dir in "/app/data" "/tmp/legal_dashboard/data" "/tmp/app_data" "$HOME/legal_dashboard/data" "./data"; do
    if test_db_creation "$test_dir"; then
        WORKING_DB_DIR="$test_dir"
        break
    fi
done

if [ "$WORKING_DB_DIR" != "" ]; then
    echo "βœ… Database directory confirmed: $WORKING_DB_DIR"
    export DATABASE_DIR="$WORKING_DB_DIR"
else
    echo "⚠️  No writable database directory found, will use in-memory database"
    export DATABASE_DIR=":memory:"
fi

# Create .env file if it doesn't exist
echo ""
echo "βš™οΈ Creating environment configuration..."

ENV_FILE=".env"
if [ ! -f "$ENV_FILE" ]; then
    cat > "$ENV_FILE" << EOF
# Auto-generated environment configuration
DATABASE_DIR=$DATABASE_DIR
DATABASE_NAME=legal_documents.db
JWT_SECRET_KEY=your-secret-key-change-in-production-$(date +%s)
LOG_LEVEL=INFO
ENVIRONMENT=production
PYTHONPATH=/app
PYTHONUNBUFFERED=1
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7
EOF
    echo "βœ… Created $ENV_FILE"
else
    echo "βœ… $ENV_FILE already exists"
fi

# Test Python imports
echo ""
echo "🐍 Testing Python dependencies..."

python3 -c "
try:
    import fastapi
    import uvicorn
    import sqlite3
    import os
    print('βœ… Core dependencies available')
except ImportError as e:
    print('❌ Missing dependency: {}'.format(e))
    
try:
    import gradio
    print('βœ… Gradio available')
except ImportError:
    print('⚠️  Gradio not available (ok if not using HF Spaces)')
" 2>/dev/null

# Final status
echo ""
echo "πŸ“Š Final Status:"
echo "  - Database directory: ${DATABASE_DIR:-Not set}"
echo "  - Cache directory: ${TRANSFORMERS_CACHE:-Not set}"
echo "  - Log level: ${LOG_LEVEL:-INFO}"
echo "  - Environment: ${ENVIRONMENT:-development}"

# Instructions
echo ""
echo "πŸš€ Next Steps:"
echo "  1. Run the application:"
if [ "$IN_HF_SPACES" = true ]; then
    echo "     python app.py"
elif [ "$IN_DOCKER" = true ]; then
    echo "     ./start.sh"
    echo "     # or"
    echo "     uvicorn app.main:app --host 0.0.0.0 --port 8000"
else
    echo "     ./start.sh"
    echo "     # or"
    echo "     python app.py"
    echo "     # or"
    echo "     uvicorn app.main:app --host 0.0.0.0 --port 8000"
fi

echo ""
echo "  2. Default login credentials:"
echo "     Username: admin"
echo "     Password: admin123"
echo ""
echo "  3. Change default password after first login!"

echo ""
echo "πŸŽ‰ Permission fix completed!"
echo "============================================"