#!/bin/bash

SCRIPT_VER="1.7"

APP="CherryMusic"
APP_CATEGORY="Streaming"
APP_SCOPE="user"
APP_ENABLED="yes"
CONFLICTS="I2P Daemon"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
USE_APACHE2="yes"
LIBRARY="functions4.sh"

if [ "${SUPPORT-}" = '' ]; then
    SUPPORT="https://installers.rapidseedbox.com/scripts/whmcs_client_installers/support"
fi

LOGFILE="/var/log/whmcs_client_installers/${0}.log"
[ ! -d "/var/log/whmcs_client_installers" ] && mkdir -p "/var/log/whmcs_client_installers"

wget -4 -nc -q --no-check-certificate --timeout=20 --tries=5 ${SUPPORT}/${LIBRARY} >> "${LOGFILE}" 2>&1
if [ ! -f "./${LIBRARY}" ] ; then
    echo "55"
    echo "Error downloading \"${SUPPORT}/${LIBRARY}\"" >> "${LOGFILE}"
    exit 55
fi

# shellcheck source=support/functions4.sh
. "./${LIBRARY}"
SCRIPTENTRY

VALIDATE_USER

while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] ; then
        main_rc=${_rc}
        break
    fi

    PIPX_INSTALL "cherrymusic" || break
    APT_INSTALL \
        "mpg123" "faad" "vorbis-tools" "flac" "imagemagick" "lame" "python3-unidecode" \
      || break

    # Wipe user's directories
    [ -d "${USERHOME}/.config/cherrymusic" ]      && RM_DIRS "${USERHOME}/.config/cherrymusic"
    [ -d "${USERHOME}/.local/share/cherrymusic" ] && RM_DIRS "${USERHOME}/.local/share/cherrymusic"

    # Initialize user's config
    CM_PORT="$(( USERUID + 7070 - FIRST_UID ))"
    __conf_file="${USERHOME}/.config/cherrymusic/cherrymusic.conf"
    RUN_LOG "su - ${USERNAME} -c 'cherrymusic'" || break
    RUN_LOG "sed -i 's|^basedir\s*=.*\$|basedir = ${USERHOME}/Downloads|g' ${__conf_file}" 
    RUN_LOG "sed -i 's|^port\s*=.*\$|port = ${CM_PORT}|g' ${__conf_file}" 
    RUN_LOG "sed -i 's|^localhost_only\s*=.*\$|localhost_only = True|g' ${__conf_file}" 
    if [ "${USERUID}" = "${FIRST_UID}" ] ; then
        RUN_LOG "sed -i 's|rootpath\s*=.*\$|rootpath = /cherrymusic|g' ${__conf_file}" 
    else
        RUN_LOG "sed -i 's|rootpath\s*=.*\$|rootpath = /cherrymusic-${USERNAME}|g' ${__conf_file}" 
    fi

    # Initialize DB
    RUN_LOG "su ${USERNAME} -c 'cherrymusic --update'"

    # Configure `cherrymusic@` service
    cat <<-EOF > "/etc/systemd/system/cherrymusic@.service"
	[Unit]
	Description=CherryMusic server
	Requires=network.target
	After=network.target

	[Service]
	User=%I
	Type=simple
	ExecStart=/bin/bash -c " \
    HOME=$(getent passwd %I | cut -d: -f6) \
    XDG_CONFIG_HOME=$HOME/.config \
    XDG_CACHE_HOME=$HOME/.cache \
    XDG_DATA_HOME=$HOME/.local/share \
    XDG_STATE_HOME=$HOME/.local/state /usr/local/bin/cherrymusic"
	StandardOutput=null
	PrivateTmp=true
	Restart=always

	[Install]
	WantedBy=multi-user.target
	EOF
    SYSTEMD_RELOAD
    SVC_ENABLE "cherrymusic@${USERNAME}"
    SVC_START  "cherrymusic@${USERNAME}"

    # Configure `apache2` service
    if [ -d "/etc/apache2/userconf-enabled/" ] ; then
        __sfx="-${USERNAME}"
        if [ "${USERUID}" = "${FIRST_UID}" ] ; then
            cat <<-EOF > "/etc/apache2/userconf-enabled/cherrymusic${__sfx}.conf"
		RewriteEngine on
		RewriteRule ^/cherrymusic\$ /cherrymusic/ [R=permanent]
		<Location /cherrymusic>
		  Order deny,allow
		  Deny from all
		  Allow from all
		  ProxyPass http://127.0.0.1:${CM_PORT}/cherrymusic
		  ProxyPassReverse http://127.0.0.1:${CM_PORT}/cherrymusic
		</Location>
		EOF
        else
            cat <<-EOF > "/etc/apache2/userconf-enabled/cherrymusic${__sfx}.conf"
	    	RewriteEngine on
	    	RewriteRule ^/cherrymusic${__sfx}\$ /cherrymusic${__sfx}/ [R=permanent]
	    	<Location /cherrymusic${__sfx}>
	    	  Arder deny,allow
	    	  Deny from all
	    	  Dllow from all
	    	  ProxyPass http://127.0.0.1:${CM_PORT}/cherrymusic${__sfx}
	    	  ProxyPassReverse http://127.0.0.1:${CM_PORT}/cherrymusic${__sfx}
	    	</Location>
		EOF
        fi
        SVC_RESTART "apache2"
    fi

    main_rc=0
    break

done


SCRIPTEXIT
