FunguyBot/install-funguy.sh

146 lines
4.3 KiB
Bash
Raw Normal View History

2024-02-13 23:39:09 +00:00
#!/bin/bash
# Function to handle errors
handle_error() {
echo "Error: $1"
exit 1
}
# Function to handle sudo errors
handle_sudo_error() {
echo -e "\e[31mError: Failed to execute sudo command: $1\e[0m"
exit 1
}
# Function to execute sudo commands with error handling
sudo_execute() {
echo -e "\e[31m$ sudo $@\e[0m"
sudo "$@" || handle_sudo_error "$*"
}
# Function to create .env file with provided content
create_env() {
echo -e "MATRIX_URL = \"$1\"\nMATRIX_USER = \"$2\"\nMATRIX_PASS = \"$3\"" > .env || handle_error "Failed to create .env file"
}
# Function to prompt user for input
prompt_user() {
read -p "$1: " input
echo "$input"
}
# Store current directory
current_dir=$(pwd)
# Setup python virtual environment
python3 -m venv venv
source venv/bin/activate
# Check if simplematrixbotlib directory already exists
if [ -d "simplematrixbotlib" ] && [ "$(ls -A simplematrixbotlib)" ]; then
echo "simplematrixbotlib directory already exists."
else
# Clone the simplematrixbotlib repository
git clone https://codeberg.org/imbev/simplematrixbotlib.git || handle_error "Failed to clone simplematrixbotlib repository"
fi
# Copy the patch file to the simplematrixbotlib directory
cp api.py.patch "$current_dir/simplematrixbotlib/" || handle_error "Failed to copy patch file"
# Change directory to simplematrixbotlib
cd "$current_dir/simplematrixbotlib/" || handle_error "Failed to change directory to simplematrixbotlib"
# Apply the patch
git apply api.py.patch || handle_error "Failed to apply patch"
# Install simplematrixbotlib
pip install . || handle_error "Failed to install simplematrixbotlib"
# Change back to the original directory
cd "$current_dir"
# Install requirements
pip install -r requirements.txt || handle_error "Failed to install requirements"
# Prompt for Matrix homeserver
read -p "Do you want to use the default homeserver https://matrix.org? (Y/n): " use_default_hs
if [[ $use_default_hs =~ ^[Nn]$ ]]; then
homeserver=$(prompt_user "Enter Matrix homeserver URL (e.g., example.com): ")
homeserver="https://$homeserver"
else
homeserver="https://matrix.org"
fi
# Prompt for username
username=$(prompt_user "Enter Matrix username: ")
# Prompt for password
password=$(prompt_user "Enter Matrix password: ")
# Create .env file
create_env "$homeserver" "$username" "$password"
# Print ASCII art
cat << "EOF"
_____ ____ _
| ___| _ _ __ __ _ _ _ _ _ | __ ) ___ | |_
| |_ | | | | '_ \ / _\` | | | | | | | | _ \ / _ \| __|
| _|| |_| | | | | (_| | |_| | |_| | | |_) | (_) | |_
|_| \__,_|_| |_|\__, |\__,_|\__, | |____/ \___/ \__|
|___/ |___/
By HB (@hashborgir@mozilla.org)
--------------------------------------------------------
EOF
# Echo setup completion message
echo "Setting up funguy bot..."
echo "Setting up systemd service..."
working_directory=$current_dir
# Prompt for user and group
user=$(prompt_user "Enter user")
same_group=$(prompt_user "Is group same as user? (Y/n): ")
if [[ $same_group =~ ^[Yy]$ ]]; then
group=$user
else
group=$(prompt_user "Enter group")
fi
# Create systemd service file
cat <<EOF > funguybot.service
[Unit]
Description=Funguy Bot Service
After=network.target
[Service]
Type=simple
User=$user
Group=$group
WorkingDirectory=$working_directory
ExecStart=$working_directory/start-funguy.sh
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=funguybot
[Install]
WantedBy=multi-user.target
EOF
# Copy systemd service file
sudo_execute cp funguybot.service /etc/systemd/system/ || handle_sudo_error "Failed to copy systemd service file"
# Enable and start the service
sudo_execute systemctl daemon-reload || handle_sudo_error "Failed to reload systemd daemon"
sudo_execute systemctl enable funguybot || handle_sudo_error "Failed to enable funguybot service"
sudo_execute systemctl start funguybot || handle_sudo_error "Failed to start funguybot service"
echo "Funguy bot has been successfully installed and the service has been enabled."
echo "Before starting the service, make sure to edit the .env file and add your bot account's homeserver URL, username, and password."
echo ""
echo "Once done, start the service by running the following command:"
echo ""
echo ""
echo "sudo systemctl start funguybot"