File size: 2,433 Bytes
972bac4
 
 
759f193
972bac4
 
 
2343246
972bac4
 
 
 
 
 
 
 
 
6cdeae2
2e6c0a0
6cdeae2
 
 
 
 
5be6289
6cdeae2
c68e54b
 
6cdeae2
 
 
c5d0bef
32ddde5
 
 
 
 
 
 
 
 
 
 
 
 
6c3fcb8
 
 
 
 
 
 
 
 
 
 
 
 
 
e8d37c7
6c3fcb8
 
972bac4
 
 
32ddde5
646d44e
 
 
972bac4
 
 
646d44e
972bac4
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
set -e  # Exit if any command fails

# echo "πŸ” GITHUB_PAT: ${GITHUB_PAT}"  # Mask token in logs

# Define variables
REPO_URL="https://github.com/blendersb/teleapi.git"
BRANCH="${BRANCH:=main}"
TARGET_DIR="/app/teleapi"
APP_DIR="/app"

# Only clone if GITHUB_PAT is set
if [ -n "$GITHUB_PAT" ]; then
  echo "πŸ“₯ GITHUB_PAT detected. Cloning private repository..."

  # Clone only if the target directory does not exist
  if [ ! -d "$TARGET_DIR" ]; then
  
    if [ -n "$CODE_REPO" ]; then
      echo "πŸ“₯ CODE_REPOSITRY detected. Cloning private repository..."
      git clone -b "$BRANCH" "$CODE_REPO" "$TARGET_DIR"
    else
      echo "πŸ“₯ Cloning private repository..."
      git clone -b "$BRANCH" "https://blendersb:${GITHUB_PAT}@github.com/blendersb/teleapi.git" "$TARGET_DIR"
    fi
    
    cd "$TARGET_DIR"

    echo "πŸ“ Latest Git Commit:"
    git log -1 --pretty=format:"πŸ”Ή %h πŸ”Ή %s πŸ”Ή by %an πŸ”Ή on %ad" --date=short
    echo

    
    echo "πŸ“‚ Moving contents from $TARGET_DIR to $APP_DIR..."
    shopt -s dotglob  # include hidden files like .env, .gitignore
    mv "$TARGET_DIR"/* "$APP_DIR"/
    rm -rf "$TARGET_DIR"
    shopt -u dotglob
  
    # Set permissions (optional)
    echo "πŸ”§ Setting main folder permissions..."
    chmod -R +x "$APP_DIR"
    # chmod -R 777 "$APP_DIR/*"
    ls -la "$APP_DIR"
    echo "πŸ”§ Setting up sessions & permissions..."
    SESSION_DIR="$APP_DIR/sessions"
    chmod -R 777 "$APP_DIR/sessions"
    
    echo "πŸ” Scanning files in: $SESSION_DIR"
    echo
    
    find "$SESSION_DIR" -type f | while read -r file; do
        perms=$(stat -c "%A" "$file")  # Get permission string
        if [ -w "$file" ]; then
            writable="βœ… Writable"
        else
            writable="❌ Not Writable"
        fi
        echo "$perms  $writable  β†’  $file"
        # rm -rf "$file"
    done
    
  else
    echo "βœ… Repository already cloned at $TARGET_DIR"
  fi
  
  # Change to repo directory
  cd "$APP_DIR"
  
  # Install Python dependencies
  if [ -f "$APP_DIR/requirements.txt" ]; then
    echo "πŸ“¦ Installing Python dependencies..."
    pip3 install --no-cache-dir --upgrade -r "requirements.txt"
  else
    echo "⚠️  No requirements.txt found in $APP_DIR"
  fi
  echo "βœ… Init script complete. Starting application..."
  # Start the actual app (from CMD)
  exec "$@"
else
  echo "ℹ️ GITHUB_PAT not set. Skipping clone step."
fi