#!/bin/bash APPNAME="imagemaid" VERSION="2024-07-16" BOLD=$(tput bold) BLUE=$(tput setaf 4) RED=$(tput setaf 1) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) MAGENTA=$(tput setaf 5) GREEN=$(tput setaf 2) STOP_COLOR=$(tput sgr0) CONFIG_DIR="$HOME/scripts/${APPNAME}" CONFIG_FILE="${CONFIG_DIR}/config/.env" CONFIG_TEMPLATE_FILE="${CONFIG_DIR}/config/noexample.env" PLEX_PREFS_FILE="${HOME}/.config/plex/Library/Application Support/Plex Media Server/Preferences.xml" PLEX_CONFIG_DIR="${HOME}/.config/plex/Library/Application Support/Plex Media Server" TMPDIR_LOCATION="$HOME/.tmp/imagemaid-$(date +%Y%m%d-%H%M%S)" print_welcome_message() { term_width=$(tput cols) welcome_message="[[ Welcome to the unofficial ${APPNAME} script ]]" padding=$(printf '%*s' $(((term_width-${#welcome_message}) / 2)) '') echo -e "\n${CYAN}${BOLD}${padding}${welcome_message}${STOP_COLOR}\n" } check_python_version() { python_path=$(command -v python) echo -e "${YELLOW}${BOLD}[INFO] Python path available on ${HOME} to use:${STOP_COLOR} '${python_path}'" INSTALLED_PYTHON_VERSION=$(${python_path} --version 2>&1) if [[ ${INSTALLED_PYTHON_VERSION} =~ [Pp]ython\ ([0-9]+\.[0-9]+) ]]; then installed_version="${BASH_REMATCH[1]}" else echo "Error: Unable to determine Python version." echo -e "${RED}${BOLD}[ERROR] Failed to determine Python version available on ${HOME}. Terminating the script ... Bye!" exit 1 fi IFS='.' read -ra version_parts <<<"$installed_version" major_version="${version_parts[0]}" minor_version="${version_parts[1]}" if [[ "$major_version" -lt 3 || ("$major_version" -eq 3 && "$minor_version" -lt 8) ]]; then echo -e "${YELLOW}${BOLD}[INFO] Python 3.11+ required to install ${APPNAME}.${STOP_COLOR}" echo -e "${YELLOW}${BOLD}[INFO] Please install Python 3.11 or higher version using the following guide:${STOP_COLOR} 'https://docs.ultra.cc/unofficial-language-installers/install-python-using-pyenv'${STOP_COLOR}" echo -e "${RED}${BOLD}[ERROR] Terminating the script ... Bye!" exit 1 else echo "${YELLOW}${BOLD}[INFO] Installed Python version on ${HOME}:${STOP_COLOR} '${INSTALLED_PYTHON_VERSION}'" fi } get_plex_direct_url() { local uuid=$(grep -o 'CertificateUUID="[a-zA-Z0-9]*"' "$PLEX_PREFS_FILE" | grep -o '".*"' | sed 's/"//g') local ip=$(dig +short "${HOSTNAME}-direct.usbx.me" | sed 's/\./-/g') local plexport="$(app-ports show | grep "Plex Media Server" | awk '{print $1}')" local url="https://${ip}.${uuid}.plex.direct:${plexport}" PLEX_DIRECT_URL="${url}" echo -e "\n${YELLOW}${BOLD}[INFO] Plex Direct URL:${STOP_COLOR} '${PLEX_DIRECT_URL}'" } install_imagemaid() { if [ -d "${CONFIG_DIR}" ]; then echo -e "${YELLOW}${BOLD}[INFO] ${APPNAME} config already present at -${STOP_COLOR} '${CONFIG_DIR}'" echo -e "${RED}${BOLD}[ERROR] Terminating the script ... Bye!" exit 1 elif [ ! -f "$PLEX_PREFS_FILE" ]; then echo -e "${YELLOW}${BOLD}[INFO] Plex Preferences.xml file NOT found; ensure you have installed Plex Media Server before running this script.${STOP_COLOR}" echo -e "${RED}${BOLD}[ERROR] Terminating the script ... Bye!" exit 1 fi echo -e "${MAGENTA}${BOLD}[STAGE-1] Download ${APPNAME} config and configure${STOP_COLOR}" mkdir -p "${CONFIG_DIR}" git clone https://github.com/Kometa-Team/ImageMaid "${CONFIG_DIR}" >/dev/null 2>&1 mkdir "${CONFIG_DIR}/logs" # if [[ -f "${CONFIG_TEMPLATE_FILE}" ]]; then # mv "${CONFIG_TEMPLATE_FILE}" "${CONFIG_FILE}" # echo -e "${YELLOW}${BOLD}[INFO] Renamed config template to:${STOP_COLOR} '${CONFIG_FILE}'" # else # echo -e "${RED}${BOLD}[ERROR] Failed to download ${APPNAME} config template at:${STOP_COLOR} '${CONFIG_TEMPLATE_FILE}'" # exit 1 # fi check_python_version get_plex_direct_url PLEX_TOKEN=$(grep -oP 'PlexOnlineToken="\K[^"]+' "$PLEX_PREFS_FILE") echo -e "\n${YELLOW}${BOLD}[INFO] Plex Token:${STOP_COLOR} '${PLEX_TOKEN}'" INSTALLED_PYTHON_PATH=$(pyenv which python3.11) cat <"$CONFIG_FILE" PLEX_PATH=${PLEX_CONFIG_DIR} MODE=report SCHEDULE=07:00|daily|mode=report,09:00|weekly(sunday)|mode=move,11:00|weekly(sunday)|mode=clear PLEX_URL=${PLEX_DIRECT_URL} PLEX_TOKEN=${PLEX_TOKEN} DISCORD= TIMEOUT=600 SLEEP=60 IGNORE_RUNNING=False LOCAL_DB=False USE_EXISTING=False PHOTO_TRANSCODER=False EMPTY_TRASH=False CLEAN_BUNDLES=False OPTIMIZE_DB=False TRACE=False LOG_REQUESTS=False EOF PIP_PATH="$(dirname ${INSTALLED_PYTHON_PATH})/pip" echo -e "\n${YELLOW}${BOLD}[INFO] ${APPNAME} config file created at:${STOP_COLOR} '$CONFIG_FILE'" (cd ${CONFIG_DIR} && ${PIP_PATH} install -r requirements.txt --ignore-installed) >/dev/null 2>&1 echo -e "${MAGENTA}${BOLD}[STAGE-2] Create systemd for ${APPNAME}${STOP_COLOR}" local service_file="$HOME/.config/systemd/user/imagemaid.service" cat <"$service_file" [Unit] Description=ImageMaid After=multi-user.target [Service] Type=simple Restart=always RestartSec=3 ExecStart=${INSTALLED_PYTHON_PATH} %h/scripts/${APPNAME}/imagemaid.py StandardOutput=file:%h/scripts/${APPNAME}/logs/imagemaid.log StandardError=file:%h/scripts/${APPNAME}/logs/error.log [Install] WantedBy=multi-user.target EOF systemctl --user enable imagemaid.service >/dev/null 2>&1 systemctl --user start imagemaid.service echo -e "${YELLOW}[INFO] Created systemd file for ${APPNAME}:${STOP_COLOR} '${service_file}'" echo -e "\n${GREEN}${BOLD}[SUCCESS] ${APPNAME} installation completed! Check the logs at:${STOP_COLOR} '${HOME}/scripts/${APPNAME}/logs/imagemaid.log'" } uninstall_imagemaid() { systemctl --user stop imagemaid.service systemctl --user disable imagemaid.service >/dev/null 2>&1 SERVICE_FILE="$HOME/.config/systemd/user/imagemaid.service" rm -f "${SERVICE_FILE}" rm -rf "${CONFIG_DIR}" systemctl --user daemon-reload if [[ -f ${SERVICE_FILE} ]] || [[ -d "${CONFIG_DIR}" ]]; then echo -e "${RED}${BOLD}[ERROR] ${APPNAME} could not be fully uninstalled." else echo -e "${GREEN}${BOLD}[SUCCESS] ${APPNAME} has been uninstalled completely." fi } update_MODE_settings() { if [ ! -f "$CONFIG_FILE" ]; then echo -e "${RED}${BOLD}[ERROR] ${APPNAME} config file can't be found. Terminating the script ... Bye!" exit 1 fi get_input sed -i "s/MODE=.*/MODE=${INPUT_KEY}/" "$CONFIG_FILE" echo -e "${GREEN}[INFO] MODE key has been updated in $CONFIG_FILE.${STOP_COLOR}" } update_DISCORD_webhook_settings() { if [ ! -f "$CONFIG_FILE" ]; then echo -e "${RED}${BOLD}[ERROR] ${APPNAME} config file can't be found. Terminating the script ... Bye!" exit 1 fi get_input sed -i "s/DISCORD=.*/DISCORD=${INPUT_KEY}/" "$CONFIG_FILE" echo -e "${GREEN}[INFO] DISCROD URL has been updated in $CONFIG_FILE.${STOP_COLOR}" } get_input() { retries=3 while (( retries > 0 )); do read -rp "${BLUE}${BOLD}[INPUT REQUIRED] Enter your TMDB API key:${STOP_COLOR} " INPUT_KEY if [ ! -z "$INPUT_KEY" ]; then break else echo -e "${RED}${BOLD}[ERROR] API Key can't be empty. You have $retries attempts left.${STOP_COLOR}" retries=$((retries - 1)) if (( retries == 0 )); then echo -e "\n${RED}${BOLD}[ERROR] Maximum attempts reached. Terminating the script ... Bye!${STOP_COLOR}" exit 1 fi fi done } main_fn() { clear print_welcome_message echo -e "${YELLOW}${BOLD}[WARNING] Disclaimer: The scripts and documents shared here are provided as-is by fellow customers. Ultra.cc does not review, test, or endorse these contributions. Their inclusion does not imply fitness for any particular purpose, nor guarantee compliance with our Terms of Service. No support is provided for their use. You assume all risk in using them.${STOP_COLOR}\n" echo -e "${YELLOW}${BOLD}Actions Available: [1] Install ${APPNAME}, [2] Uninstall ${APPNAME}, [3] Update Discord Webhook, [4] Update Mode \n" read -rp "${MAGENTA}${BOLD}[INPUT REQUIRED] Select an action [1, 2, 3, or 4]:${STOP_COLOR} " SELECTED_ACTION case "$SELECTED_ACTION" in 1) install_imagemaid ;; 2) uninstall_imagemaid ;; 3) update_DISCORD_webhook_settings ;; 4) update_MODE_settings ;; *) echo -e "${RED}${BOLD}[ERROR] Invalid input provided. Terminating the script ... Bye!" exit 1 ;; esac } main_fn