main
ec1f561 · 3 months ago 21 commits
 1#!/bin/bash
 2
 3# check curl exist or not
 4if ! command -v curl >/dev/null 2>&1; then
 5    echo "Install curl and run the script"
 6    exit 1
 7fi
 8
 9COMMON_SCRIPT_URL="https://scripts.ansari.wtf/base.sh"
10
11source <(curl -s $COMMON_SCRIPT_URL)
12
13cleanup() {
14    echo
15    echo -e "${YELLOW}Setup interrupted. Cleaning up...${NC}"
16    pkill -f code-server 2>/dev/null
17    exit 1
18}
19
20setup_config() {
21    local config_dir="$HOME/.config/code-server"
22    local config_file="$config_dir/config.yaml"
23
24    mkdir -p "$config_dir"
25    rm -f "$config_file"
26
27    cat > "$config_file" << 'EOF'
28bind-addr: 0.0.0.0:8000
29auth: none
30cert: false
31EOF
32    return 0
33}
34
35wait_for_service() {
36    local max_attempts=30
37    local attempt=0
38
39    printf "${BLUE}Starting code-server${NC} "
40
41    while [ $attempt -lt $max_attempts ]; do
42        if curl -s http://localhost:8000 >/dev/null 2>&1; then
43            printf "${GREEN}${NC}\n"
44            return 0
45        fi
46
47        printf "%s" "${SPINNER:$((attempt % ${#SPINNER})):1}"
48        sleep 1
49        printf "\b"
50        attempt=$((attempt + 1))
51    done
52
53    printf "${RED}${NC}\n"
54    echo -e "${RED}Timeout: Code-server failed to start${NC}"
55    return 1
56}
57
58
59main(){
60    clear
61    show_box "Termux VSCode Setup"
62
63    total_steps=5
64    current_step=0
65    progress_line=5
66
67    initialize_progress_bar
68    setup_cleanup_trap cleanup
69
70    run_step "pkg update -y" "Updating package list"
71    log_info "Upgrading packages"
72    yes " " | pkg upgrade -y > /dev/null 2>&1
73    current_step=$((current_step + 1))
74    run_step "pkg install tur-repo -y" "Installing tur-repo"
75    run_step "pkg install code-server git python3 -y" "Installing code-server, git, and python3"
76    run_step "setup_config" "Setting up code-server configuration"
77
78    log_info "Creating LOG directory"
79    mkdir -p "$HOME/.code-server-logs"
80    log_info "Starting code-server"
81    code-server > "$HOME/.code-server-logs/code-server.log" 2>&1 &
82
83    if wait_for_service; then
84        show_box "Code-server is running" 40 "GREEN" "Access your code-server at http://localhost:8000"
85        if command_exists "termux-open-url"; then
86            log_info "Opening in browser"
87            termux-open-url http://localhost:8000
88        fi
89    else
90        exit 1
91    fi
92}
93
94main