Files
tesla-bot/install.sh
2025-06-19 12:55:10 +00:00

169 lines
5.1 KiB
Bash

#!/bin/bash
REPO_URL="https://git.bitmaster.cc/BitMaster/tesla-bot.git"
CLONE_DIR="tesla-bot"
CRON_NAME="tesla-inventory-bot"
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
NC="\e[0m"
function banner() {
echo -e "${GREEN}"
echo "=============================="
echo "🚗 Tesla Inventory Bot Setup"
echo "=============================="
echo -e "${NC}"
}
function system_status() {
echo -e "${YELLOW}...checking system status...${NC}"
echo -n "📦 Docker: "
if command -v docker &>/dev/null; then
echo -e "${GREEN}Installed${NC}"
docker_status="ok"
else
echo -e "${RED}Missing${NC}"
docker_status="missing"
fi
echo -n "🐳 Docker Compose: "
if docker compose version &>/dev/null; then
echo -e "${GREEN}Available (v2)${NC}"
compose_status="ok"
else
echo -e "${RED}Missing or incompatible${NC}"
compose_status="missing"
fi
echo -n "📁 Project folder '$CLONE_DIR': "
if [ -d "$CLONE_DIR" ]; then
echo -e "${YELLOW}Exists${NC}"
folder_status="exists"
else
echo -e "${GREEN}Not present${NC}"
folder_status="missing"
fi
echo -n "📦 Docker image '$CRON_NAME': "
if docker images --format '{{.Repository}}' | grep -q "$CRON_NAME"; then
echo -e "${YELLOW}Already built${NC}"
image_status="built"
else
echo -e "${RED}Not built${NC}"
image_status="missing"
fi
echo -n "📦 Docker container '$CRON_NAME': "
if docker ps -a --format '{{.Names}}' | grep -q "^$CRON_NAME$"; then
echo -e "${RED}Not running${NC}"
container_status="exists"
else
echo -e "${GREEN}Not running${NC}"
container_status="missing"
fi
}
function check_and_install_docker() {
if [ "$docker_status" = "missing" ]; then
echo -e "${YELLOW}Installing Docker...${NC}"
apt update
apt install -y docker.io
systemctl enable --now docker
echo -e "${GREEN}✅ Docker installed.${NC}"
fi
if [ "$compose_status" = "missing" ]; then
echo -e "${YELLOW}Installing Docker Compose plugin...${NC}"
if apt-cache show docker-compose-plugin &>/dev/null; then
apt install -y docker-compose-plugin
else
mkdir -p ~/.docker/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.27.1/docker-compose-linux-x86_64 \\
-o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
export PATH="$HOME/.docker/cli-plugins:$PATH"
fi
echo -e "${GREEN}✅ Docker Compose v2 installed.${NC}"
fi
}
function menu() {
echo ""
echo -e "${YELLOW}1. Install bot (clone + build)"
echo -e "2. Check bot status + send test Telegram message"
echo -e "3. Enable .log file logging"
echo -e "4. Setup new cronjob (every X min)"
echo -e "5. Show current cronjobs"
echo -e "6. Remove existing cronjob"
echo -e "7. Exit${NC}"
echo ""
}
function clone_and_build() {
check_and_install_docker
if [ "$folder_status" = "exists" ] || [ "$image_status" = "built" ]; then
echo -e "${YELLOW}⚠️ Project folder or image already exists.${NC}"
read -rp "$(echo -e "${YELLOW}🔁 Do you want to reinstall everything? This will remove old data. [Y/n] ${NC}")" confirm
confirm="${confirm,,}"
if [[ "$confirm" =~ ^(y|yes| )?$ ]]; then
echo -e "${YELLOW}🧹 Cleaning up...${NC}"
docker rm -f "$CRON_NAME" >/dev/null 2>&1
docker rmi -f "$CRON_NAME" >/dev/null 2>&1
rm -rf "$CLONE_DIR"
echo -e "${GREEN}✅ Cleaned up previous install.${NC}"
else
echo -e "${RED}❌ Installation aborted.${NC}"
return
fi
fi
echo -e "${GREEN}📥 Cloning repo...${NC}"
git clone "$REPO_URL" "$CLONE_DIR"
cd "$CLONE_DIR" || exit 1
echo -e "${GREEN}🔧 Building Docker image...${NC}"
docker compose build
echo -e "${YELLOW}📨 Enter your Telegram bot token:${NC}"
read -rp "🔑 TELEGRAM_TOKEN: " token
echo -e "${YELLOW}👥 Enter your Telegram chat ID (or leave blank to auto-detect):${NC}"
read -rp "🆔 TELEGRAM_CHAT_ID: " chat_id
if [ -z "$chat_id" ]; then
echo -e "${YELLOW}🔍 Trying to auto-detect chat_id from getUpdates...${NC}"
chat_id=$(curl -s "https://api.telegram.org/bot$token/getUpdates" | grep -oE '"id":-100[0-9]+' | head -n1 | cut -d: -f2)
if [ -n "$chat_id" ]; then
echo -e "${GREEN}✅ Detected chat_id: $chat_id${NC}"
else
echo -e "${RED}❌ Failed to detect chat_id. Please enter it manually.${NC}"
read -rp "🆔 TELEGRAM_CHAT_ID: " chat_id
fi
fi
sed -i "s|^TELEGRAM_TOKEN = .*|TELEGRAM_TOKEN = \"$token\"|" bot.py
sed -i "s|^CHAT_ID = .*|CHAT_ID = \"$chat_id\"|" bot.py
echo -e "${GREEN}✅ Credentials updated in bot.py${NC}"
}
# -- Start script --
clear
banner
system_status
menu
read -rp "$(echo -e "${YELLOW}👉 Choose [1-7]: ${NC}")" choice
case $choice in
1) clone_and_build ;;
2) echo "🧪 [NOT IMPLEMENTED] Check bot status" ;;
3) echo "🧪 [NOT IMPLEMENTED] Enable logging" ;;
4) echo "🧪 [NOT IMPLEMENTED] Setup cronjob" ;;
5) echo "🧪 [NOT IMPLEMENTED] Show cronjobs" ;;
6) echo "🧪 [NOT IMPLEMENTED] Remove cronjob" ;;
7) echo -e "${RED}❌ Exiting...${NC}"; exit 0 ;;
*) echo -e "${RED}❌ Invalid option.${NC}" ;;
esac