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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/bin/bash

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

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

# Function to display header
show_header() {
    clear
    echo -e "${BLUE}=======================================${NC}"
    echo -e "${GREEN}      Cursor-To-OpenAI 简易脚本      ${NC}"
    echo -e "${BLUE}=======================================${NC}"
    echo
}

# Function to check if Docker is installed
check_docker() {
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}未安装Docker。请先安装Docker。${NC}"
        exit 1
    fi
}

# Function to check if Node.js is installed
check_nodejs() {
    if ! command -v node &> /dev/null; then
        echo -e "${RED}未安装Node.js。请先安装Node.js。${NC}"
        exit 1
    fi

    if ! command -v npm &> /dev/null; then
        echo -e "${RED}未安装npm。请先安装npm。${NC}"
        exit 1
    fi
}

# Function to backup configuration before update
backup_configs() {
    echo -e "${YELLOW}正在备份配置文件...${NC}"
    backup_dir="backups/update_backup_$(date +"%Y%m%d_%H%M%S")"
    mkdir -p "$backup_dir"

    # Backup important configurations
    if [ -d data ]; then
        cp -r data "$backup_dir/"
    fi

    if [ -f .env ]; then
        cp .env "$backup_dir/"
    fi

    echo -e "${GREEN}配置文件已备份到 ${backup_dir}${NC}"
}

# Function to restore configuration after update
restore_configs() {
    if [ -z "$1" ]; then
        echo -e "${RED}未指定备份目录,无法恢复配置。${NC}"
        return 1
    fi

    backup_dir="$1"
    echo -e "${YELLOW}正在恢复配置文件...${NC}"

    if [ -d "$backup_dir/data" ]; then
        cp -r "$backup_dir/data/"* data/ 2>/dev/null
    fi

    if [ -f "$backup_dir/.env" ]; then
        cp "$backup_dir/.env" ./ 2>/dev/null
    fi

    echo -e "${GREEN}配置文件已恢复${NC}"
}

# Function for installation tasks
installation_menu() {
    show_header
    echo -e "${YELLOW}===== 安装菜单 =====${NC}"
    echo -e "1) 克隆仓库"
    echo -e "2) 安装依赖"
    echo -e "3) 创建配置文件"
    echo -e "4) 设置管理员账户"
    echo -e "5) 运行安装向导"
    echo -e "6) 构建并启动Docker容器"
    echo -e "7) 使用npm启动"
    echo -e "8) 返回主菜单"
    echo
    echo -n "请输入选择 [1-8]: "
    read -r choice

    case $choice in
        1)
            show_header
            echo -e "${YELLOW}正在克隆仓库...${NC}"
            read -p "请输入您的GitHub用户名: " username
            git clone "https://github.com/${username}/cursor-to-openai.git"
            if [ $? -eq 0 ]; then
                echo -e "${GREEN}仓库克隆成功!${NC}"
                cd cursor-to-openai
            else
                echo -e "${RED}克隆仓库失败!${NC}"
            fi
            read -p "按回车键继续..."
            installation_menu
            ;;
        2)
            show_header
            echo -e "${YELLOW}正在安装依赖...${NC}"
            npm install
            if [ $? -eq 0 ]; then
                echo -e "${GREEN}依赖安装成功!${NC}"
            else
                echo -e "${RED}依赖安装失败!${NC}"
            fi
            read -p "按回车键继续..."
            installation_menu
            ;;
        3)
            show_header
            echo -e "${YELLOW}正在创建配置文件...${NC}"

            if [ ! -f .env ]; then
                cp .env.example .env
                echo -e "${GREEN}.env文件已创建。${NC}"
            else
                echo -e "${YELLOW}.env文件已存在。${NC}"
            fi

            mkdir -p data

            if [ ! -f data/admin.json ]; then
                cp data/admin.example.json data/admin.json
                echo -e "${GREEN}admin.json文件已创建。${NC}"
            else
                echo -e "${YELLOW}admin.json文件已存在。${NC}"
            fi

            echo -e "${YELLOW}您应该编辑.env文件来配置您的环境。${NC}"
            read -p "是否现在编辑.env文件?(y/n): " edit_env
            if [[ $edit_env == "y" || $edit_env == "Y" ]]; then
                if command -v nano &> /dev/null; then
                    nano .env
                elif command -v vim &> /dev/null; then
                    vim .env
                else
                    echo -e "${RED}未找到编辑器。请稍后手动编辑.env文件。${NC}"
                fi
            fi

            read -p "按回车键继续..."
            installation_menu
            ;;
        4)
            show_header
            echo -e "${YELLOW}正在设置管理员账户...${NC}"
            node scripts/create-admin.js
            read -p "按回车键继续..."
            installation_menu
            ;;
        5)
            show_header
            echo -e "${YELLOW}正在运行安装向导...${NC}"
            node setup.js
            read -p "按回车键继续..."
            installation_menu
            ;;
        6)
            show_header
            echo -e "${YELLOW}正在构建并启动Docker容器...${NC}"
            check_docker
            docker compose up -d --build
            if [ $? -eq 0 ]; then
                echo -e "${GREEN}Docker容器启动成功!${NC}"
                echo -e "${GREEN}访问管理界面: http://localhost:3010${NC}"
            else
                echo -e "${RED}启动Docker容器失败!${NC}"
            fi
            read -p "按回车键继续..."
            installation_menu
            ;;
        7)
            show_header
            echo -e "${YELLOW}正在使用npm启动...${NC}"
            check_nodejs
            npm start &
            if [ $? -eq 0 ]; then
                echo -e "${GREEN}服务启动成功!${NC}"
                echo -e "${GREEN}访问管理界面: http://localhost:3010${NC}"
            else
                echo -e "${RED}启动服务失败!${NC}"
            fi
            read -p "按回车键继续..."
            installation_menu
            ;;
        8)
            main_menu
            ;;
        *)
            echo -e "${RED}无效选项。请重试。${NC}"
            read -p "按回车键继续..."
            installation_menu
            ;;
    esac
}

# Function for maintenance tasks
maintenance_menu() {
    show_header
    echo -e "${YELLOW}===== 维护菜单 =====${NC}"
    echo -e "1) 查看服务状态"
    echo -e "2) 刷新Cookie"
    echo -e "3) 强制刷新Cookie"
    echo -e "4) 管理邮箱"
    echo -e "5) 管理无效Cookie"
    echo -e "6) 查看日志"
    echo -e "7) 重启服务"
    echo -e "8) 停止服务"
    echo -e "9) 更新项目代码"
    echo -e "10) 备份项目数据"
    echo -e "11) 持续刷新Cookie直到成功"
    echo -e "12) 返回主菜单"
    echo
    echo -n "请输入选择 [1-12]: "
    read -r choice

    case $choice in
        1)
            show_header
            echo -e "${YELLOW}服务状态:${NC}"
            if docker ps | grep -q cursor-to-openai; then
                echo -e "${GREEN}Docker容器正在运行。${NC}"
                docker ps | grep cursor-to-openai
            else
                echo -e "${RED}Docker容器未运行。${NC}"
            fi

            pids=$(pgrep -f "node.*start")
            if [ -n "$pids" ]; then
                echo -e "${GREEN}Node.js服务正在运行,PID: $pids${NC}"
            else
                echo -e "${RED}Node.js服务未运行。${NC}"
            fi

            read -p "按回车键继续..."
            maintenance_menu
            ;;
        2)
            show_header
            echo -e "${YELLOW}正在刷新Cookie...${NC}"
            npm run refresh-cookies
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        3)
            show_header
            echo -e "${YELLOW}正在强制刷新Cookie...${NC}"
            npm run refresh-cookies -- --force
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        4)
            show_header
            echo -e "${YELLOW}正在管理邮箱...${NC}"
            npm run manage-emails
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        5)
            show_header
            echo -e "${YELLOW}正在管理无效Cookie...${NC}"
            node manage-invalid-cookies.js
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        6)
            show_header
            echo -e "${YELLOW}正在查看日志...${NC}"
            if docker ps | grep -q cursor-to-openai; then
                docker compose logs -f
            else
                echo -e "${RED}Docker容器未运行。${NC}"
                echo -e "${YELLOW}正在检查npm日志...${NC}"
                # Try to find logs in npm-debug.log or similar
                if [ -f npm-debug.log ]; then
                    cat npm-debug.log
                else
                    echo -e "${RED}未找到日志文件。${NC}"
                fi
            fi
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        7)
            show_header
            echo -e "${YELLOW}正在重启服务...${NC}"
            if docker ps | grep -q cursor-to-openai; then
                docker compose restart
                echo -e "${GREEN}Docker容器已重启。${NC}"
            else
                pids=$(pgrep -f "node.*start")
                if [ -n "$pids" ]; then
                    kill $pids
                    sleep 2
                    npm start &
                    echo -e "${GREEN}Node.js服务已重启。${NC}"
                else
                    echo -e "${RED}未检测到运行中的服务。${NC}"
                    echo -e "${YELLOW}是否要启动服务?(y/n): ${NC}"
                    read -r start_service
                    if [[ $start_service == "y" || $start_service == "Y" ]]; then
                        npm start &
                        echo -e "${GREEN}服务已启动。${NC}"
                    fi
                fi
            fi
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        8)
            show_header
            echo -e "${YELLOW}正在停止服务...${NC}"
            if docker ps | grep -q cursor-to-openai; then
                docker compose down
                echo -e "${GREEN}Docker容器已停止。${NC}"
            else
                pids=$(pgrep -f "node.*start")
                if [ -n "$pids" ]; then
                    kill $pids
                    echo -e "${GREEN}Node.js服务已停止。${NC}"
                else
                    echo -e "${RED}未检测到运行中的服务。${NC}"
                fi
            fi
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        9)
            show_header
            echo -e "${YELLOW}正在更新项目代码...${NC}"

            # 备份配置文件
            backup_configs
            backup_dir=$(ls -td backups/update_backup_* | head -1)

            # 检查是否存在未提交的更改
            if [ -n "$(git status --porcelain)" ]; then
                echo -e "${YELLOW}检测到未提交的更改。更新前请处理这些更改。${NC}"
                echo -e "1) 查看更改"
                echo -e "2) 备份并放弃更改"
                echo -e "3) 取消更新"
                echo -n "请选择操作 [1-3]: "
                read -r update_choice

                case $update_choice in
                    1)
                        git status
                        echo -e "${YELLOW}是否继续更新?(y/n): ${NC}"
                        read -r continue_update
                        if [[ $continue_update != "y" && $continue_update != "Y" ]]; then
                            echo -e "${YELLOW}更新已取消。${NC}"
                            read -p "按回车键继续..."
                            maintenance_menu
                            return
                        fi
                        ;;
                    2)
                        echo -e "${YELLOW}备份更改...${NC}"
                        git diff > "$backup_dir/local_changes.patch"
                        git checkout -- .
                        echo -e "${GREEN}更改已备份到 $backup_dir/local_changes.patch${NC}"
                        ;;
                    3)
                        echo -e "${YELLOW}更新已取消。${NC}"
                        read -p "按回车键继续..."
                        maintenance_menu
                        return
                        ;;
                    *)
                        echo -e "${RED}无效选项。更新已取消。${NC}"
                        read -p "按回车键继续..."
                        maintenance_menu
                        return
                        ;;
                esac
            fi

            # 更新代码
            git pull
            update_status=$?

            # 恢复配置文件
            restore_configs "$backup_dir"

            if [ $update_status -eq 0 ]; then
                echo -e "${GREEN}项目代码更新成功!${NC}"
                echo -e "${YELLOW}是否需要重新安装依赖?(y/n): ${NC}"
                read -r reinstall
                if [[ $reinstall == "y" || $reinstall == "Y" ]]; then
                    npm install
                    if [ $? -eq 0 ]; then
                        echo -e "${GREEN}依赖安装成功!${NC}"
                    else
                        echo -e "${RED}依赖安装失败!${NC}"
                    fi
                fi
            else
                echo -e "${RED}项目代码更新失败!${NC}"
            fi

            read -p "按回车键继续..."
            maintenance_menu
            ;;
        10)
            show_header
            echo -e "${YELLOW}正在备份项目数据...${NC}"

            # 创建备份目录
            backup_dir="backups/backup_$(date +"%Y%m%d_%H%M%S")"
            mkdir -p "$backup_dir"

            # 备份关键文件
            cp -r data "$backup_dir/" 2>/dev/null
            if [ -f .env ]; then
                cp .env "$backup_dir/"
            fi

            # 压缩备份
            tar -czf "${backup_dir}.tar.gz" "$backup_dir"
            rm -rf "$backup_dir"

            echo -e "${GREEN}备份已创建: ${backup_dir}.tar.gz${NC}"
            read -p "按回车键继续..."
            maintenance_menu
            ;;
        11)
            show_header
            echo -e "${YELLOW}持续刷新Cookie直到成功...${NC}"
            read -p "请输入最大尝试时间(分钟, 默认60): " max_time
            max_time=${max_time:-60}
            max_seconds=$((max_time * 60))

            echo -e "${YELLOW}将持续尝试刷新Cookie,最长 ${max_time} 分钟...${NC}"

            start_time=$(date +%s)
            success=false
            attempt=0

            while ! $success && [ $(($(date +%s) - start_time)) -lt $max_seconds ]; do
                attempt=$((attempt + 1))
                elapsed=$(($(date +%s) - start_time))
                remaining=$((max_seconds - elapsed))
                remaining_min=$((remaining / 60))
                remaining_sec=$((remaining % 60))

                echo -e "${YELLOW}尝试 #${attempt}...(剩余时间: ${remaining_min}${remaining_sec}秒)${NC}"

                # 运行刷新命令并检查输出
                output=$(npm run refresh-cookies -- --force 2>&1)
                echo "$output"

                if echo "$output" | grep -q "成功添加新的Cookie" || echo "$output" | grep -q "Successfully added new cookies"; then
                    success=true
                    echo -e "${GREEN}成功添加新Cookie!${NC}"
                else
                    wait_time=$((RANDOM % 61 + 30))  # 30-90秒随机间隔
                    echo -e "${YELLOW}等待 ${wait_time} 秒后重试...${NC}"
                    sleep $wait_time
                fi
            done

            if $success; then
                echo -e "${GREEN}成功刷新Cookie!${NC}"
            else
                echo -e "${RED}达到最大尝试时间,未能成功刷新Cookie。${NC}"
            fi

            read -p "按回车键继续..."
            maintenance_menu
            ;;
        12)
            main_menu
            ;;
        *)
            echo -e "${RED}无效选项。请重试。${NC}"
            read -p "按回车键继续..."
            maintenance_menu
            ;;
    esac
}

# Main menu function
main_menu() {
    show_header
    echo -e "${YELLOW}===== 主菜单 =====${NC}"
    echo -e "1) 启动服务 (npm)"
    echo -e "2) 安装配置"
    echo -e "3) 系统维护"
    echo -e "4) 退出"
    echo
    echo -n "请输入选择 [1-4]: "
    read -r choice

    case $choice in
        1)
            show_header
            echo -e "${YELLOW}正在使用npm启动服务...${NC}"
            check_nodejs

            # 检查Node.js服务是否已在运行
            pids=$(pgrep -f "node.*start")
            if [ -n "$pids" ]; then
                echo -e "${YELLOW}服务已在运行,PID: $pids${NC}"
                echo -e "${YELLOW}是否要重启服务?(y/n): ${NC}"
                read -r restart
                if [[ $restart == "y" || $restart == "Y" ]]; then
                    kill $pids
                    sleep 2
                    npm start &
                    echo -e "${GREEN}服务已重启${NC}"
                fi
            else
                npm start &
                echo -e "${GREEN}服务已启动${NC}"
            fi

            echo -e "${GREEN}访问管理界面: http://localhost:3010${NC}"
            read -p "按回车键继续..."
            main_menu
            ;;
        2)
            installation_menu
            ;;
        3)
            maintenance_menu
            ;;
        4)
            show_header
            echo -e "${GREEN}感谢使用Cursor-To-OpenAI简易脚本!${NC}"
            exit 0
            ;;
        *)
            echo -e "${RED}无效选项。请重试。${NC}"
            read -p "按回车键继续..."
            main_menu
            ;;
    esac
}

# Start the script
main_menu