Skip to content

A self-hosted media server and file management system

8.8 KB
Raw Blame
@echo off

SET "SHARED_VOLUME_NAME=mediaserver-shared-env"
SET "VOLUME_UID=1000"
SET "VOLUME_GID=1000"
SET "ENV_FILE=./.env"
SET "FALLBACK_DEFAULT_DOMAIN=app.test"
echo.
echo ============================================
echo           mediaServer Docker Setup
echo ============================================
echo.

SET "IS_CI_MODE=false"
IF /I "%~1"=="--auto-default" (
    SET "IS_CI_MODE=true"
    call :ColorText "[INFO]" Blue
    echo Auto-default mode enabled via command-line argument.
    SHIFT /1
) ELSE IF DEFINED CI (
    SET "IS_CI_MODE=true"
    call :ColorText "[INFO]" Blue
    echo Auto-default mode enabled - CI/CD environment detected - CI variable.
) ELSE IF DEFINED GITHUB_ACTIONS (
    SET "IS_CI_MODE=true"
    call :ColorText "[INFO]" Blue
    echo Auto-default mode enabled - CI/CD environment detected - GITHUB_ACTIONS variable.
)
echo.

call :ColorText "[STEP 1/5]" Yellow
echo Verifying required files and folders...
echo.

:: Check for docker-compose.yaml
if not exist docker-compose.yaml (
    call :ColorText "[ERROR]" Red
    echo Missing 'docker-compose.yaml' file.
    echo Please ensure this file is present in the root directory.
    pause
    goto :end
) else (
    call :ColorText "[FOUND]" Green
    echo 'docker-compose.yaml' file.
)
echo.
rem --------------------------------------------------

:: Check for .env.docker
if not exist "docker\.env.docker" (
    call :ColorText "[ERROR]" Red
    echo Missing 'docker/.env.docker' file.
    echo Please ensure this file is present in the correct directory.
    pause
    goto :end
)

:: Create .env if it doesn't exist
if not exist ".env" (
    call :ColorText "[INFO]" Blue
    echo '.env' file not found. Creating from '.env.docker'...
    copy ".\docker\.env.docker" ".\.env"
    if errorlevel 1 (
        call :ColorText "[ERROR]" Red
        echo Failed to create '.env' file.
        pause
        goto :end
    )
    call :ColorText "[SUCCESS]" Green
    echo '.env' file created.
) else (
    call :ColorText "[FOUND]" Green
    echo '.env' file.
)
echo.

REM Ensure data directories exists

mkdir data
mkdir data\thumbnails
mkdir data\media
mkdir app
echo.

call :ColorText "[READY]" Green
echo 'data' and 'app' directories are ready.
echo.

:: Ensure logs directory exists
if not exist "logs" (
    call :ColorText "[INFO]" Blue
    echo Missing 'logs' directory. Creating it...
    echo.
    mkdir "logs"
    mkdir "logs\mediaServer"
    mkdir "logs\nginx"
    if errorlevel 1 (
        call :ColorText "[ERROR]" Red
        echo Failed to create 'logs' directory.
        pause
        goto :end
    )
    call :ColorText "[SUCCESS]" Green
    echo 'logs' directory created.
) else (
    call :ColorText "[FOUND]" Green
    echo 'logs' directory.
)
echo.
rem --------------------------------------------------

call :ColorText "[STEP 2/5] " Yellow
echo Setting up user config...
echo.

setlocal ENABLEDELAYEDEXPANSION
SET "CURRENT_APP_HOST="
SET "CURRENT_APP_PORT="

IF EXIST "%ENV_FILE%" (
    FOR /F "tokens=1* delims==" %%a IN ('findstr /b "APP_HOST=" "%ENV_FILE%"') DO (
        SET "CURRENT_APP_HOST=%%b"
    )
)

IF EXIST "%ENV_FILE%" (
    FOR /F "tokens=1* delims==" %%a IN ('findstr /b "APP_PORT=" "%ENV_FILE%"') DO (
        SET "CURRENT_APP_PORT=%%b"
    )
)

SET "CURRENT_DOMAIN_DEFAULT=%FALLBACK_DEFAULT_DOMAIN%"
SET "CURRENT_PORT_DEFAULT=8080"

IF NOT "!CURRENT_APP_PORT!"=="" (
    REM Remove quotes
    SET "TEMP_PORT=!CURRENT_APP_PORT:"=!"
    IF NOT "!TEMP_PORT!"=="" (
        SET "CURRENT_PORT_DEFAULT=!TEMP_PORT!"
    )
)

IF NOT "!CURRENT_APP_HOST!"=="" (
    REM Remove quotes
    SET "TEMP_DOMAIN_FROM_HOST=!CURRENT_APP_HOST:"=!"
    IF NOT "!TEMP_DOMAIN_FROM_HOST!"=="" (
        SET "CURRENT_DOMAIN_DEFAULT=!TEMP_DOMAIN_FROM_HOST!"
    )
)
ENDLOCAL & (
    SET "CURRENT_DOMAIN_DEFAULT=%CURRENT_DOMAIN_DEFAULT%"
    SET "CURRENT_PORT_DEFAULT=%CURRENT_PORT_DEFAULT%"
)

REM Ask for domain name, or use default in CI/CD mode
IF "%IS_CI_MODE%"=="true" (
    SET "USER_DOMAIN=%CURRENT_DOMAIN_DEFAULT%"
    call :ColorText "[INFO] " BLUE
    echo Automatically using default domain: %USER_DOMAIN%
) ELSE (
    call :ColorText "[CONFIG] " YELLOW
    echo "Enter your app domain (URL)..."
    echo "Current value: %CURRENT_DOMAIN_DEFAULT% (Press Enter to use this)..."
    echo.
    set /p USER_DOMAIN="Domain: "
)
echo.

REM Use current default if user input is empty
IF "%USER_DOMAIN%"=="" (
    SET "APP_HOST=%CURRENT_DOMAIN_DEFAULT%"
) ELSE (
    SET "APP_HOST=%USER_DOMAIN%"
)

REM Use default port if found port is empty
IF "%CURRENT_PORT_DEFAULT%"=="" (
    SET "APP_PORT=8080"
) ELSE (
    SET "APP_PORT=%CURRENT_PORT_DEFAULT%"
)

call :ColorText "[INFO] " BLUE
echo Setting APP_HOST to: %APP_HOST%
echo.

REM Update APP_HOST in .env file

SET "TEMP_ENV_FILE=%ENV_FILE%.tmp"
(
    for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%ENV_FILE%"') do (
        set "line=%%b"
        setlocal ENABLEDELAYEDEXPANSION
        if "!line:~0,9!"=="APP_HOST=" (
            echo APP_HOST="%APP_HOST%"
        ) else (
            echo(!line!
        )
        endlocal
    )
) > "%TEMP_ENV_FILE%"

IF %ERRORLEVEL% NEQ 0 (
    echo.
    call :ColorText "[ERROR]" RED
    echo Failed to create temporary .env file.
    echo Please check script permissions.
    pause
    goto :end
)

MOVE /Y "%TEMP_ENV_FILE%" "%ENV_FILE%" >nul

IF %ERRORLEVEL% NEQ 0 (
    echo.
    call :ColorText "[ERROR]" RED
    echo Failed to replace original .env file.
    echo Please check script permissions.
    pause
    goto :end
) ELSE (
    call :ColorText "[SUCCESS] " GREEN
    echo APP_HOST updated in %ENV_FILE%.
)
echo.

call :ColorText "[STEP 3/5] " Yellow
echo Stopping and cleaning up existing mediaServer Docker containers...
echo.

docker compose down
if errorlevel 1 (
    echo.
    call :ColorText "[ERROR]" Red
    echo Failed to bring down Docker containers.
    echo Please check your Docker setup and try again.
    pause
    goto :end
)
echo.
call :ColorText "[SUCCESS]" Green
echo Docker containers stopped and cleaned up.
echo.
rem --------------------------------------------------

echo Docker volumes no longer pruned...
echo.
rem --------------------------------------------------

call :ColorText "[STEP 4/5] " Yellow
echo Pulling latest Docker images...
echo.
docker compose pull
if errorlevel 1 (
    echo.
    call :ColorText "[ERROR]" Red
    echo Failed to pull Docker images.
    echo Please check your internet connection and Docker setup.
    pause
    goto :end
) else (
    echo.
    call :ColorText "[SUCCESS]" Green
    echo Docker images pulled successfully.
    echo.
)

call :ColorText "[STEP 5/5] " Yellow
echo Building docker compose...
echo.

call :ColorText "[INFO] " BLUE
echo Checking for shared Docker volume '%SHARED_VOLUME_NAME%'...

docker volume create %SHARED_VOLUME_NAME% >nul 2>&1
docker volume inspect %SHARED_VOLUME_NAME% >nul 2>&1

IF %ERRORLEVEL% NEQ 0 (
    call :ColorText "[ERROR]" RED
    echo.
    echo Shared volume '%SHARED_VOLUME_NAME%' could not be created or found.
    echo.
    pause
    goto :end
)
echo.
call :ColorText "[SUCCESS] " GREEN
echo Shared volume '%SHARED_VOLUME_NAME%' ready.
echo.

call :ColorText "[INFO] " BLUE
echo Setting permissions on shared volume '%SHARED_VOLUME_NAME%'...

docker run --rm -v %SHARED_VOLUME_NAME%:/shared alpine sh -c "chown -R %VOLUME_UID%:%VOLUME_GID% /shared"
docker run --rm --user %VOLUME_UID%:%VOLUME_GID% -v %SHARED_VOLUME_NAME%:/shared alpine sh -c "echo initialized > /shared/.init"

IF %ERRORLEVEL% NEQ 0 (
    echo.
    call :ColorText "[ERROR]" RED
    echo Failed to set shared volume permissions.
    echo Please check your Docker volume and user/group ID configurations.
    pause
    goto :end
)
echo.

call :ColorText "[SUCCESS] " GREEN
echo Shared volume permissions set.
echo.

docker compose up -d

if errorlevel 1 (
    echo.
    call :ColorText "[ERROR]" Red
    echo Failed to build docker compose.
    echo Please check your configuration.
    pause
    goto :end
)
echo.
echo ============================================
echo          SETUP COMPLETED SUCCESSFULLY!
echo ============================================
echo.
echo Your mediaServer will be available at https://%APP_HOST% or http://%APP_HOST%:%APP_PORT%
echo.
echo To add audio or video to your server, put the files in ./data/media organised by /LIBRARY/FOLDER/VIDEO.mp4
echo.
IF "%APP_HOST%"=="app.test" (
    call :ColorText "[WARNING]" Yellow
    echo The domain '%APP_HOST%' is not publicly resolvable. You may need to add the following line to your hosts file:
    echo.
    echo 127.0.0.1    %APP_HOST%
    echo.
    echo Run the provided powershell script 'add-hosts-entry.ps1' to automate this process
    echo.
    goto :end
)
:end

set arg0=%0
if [%arg0:~2,1%]==[:] pause
exit /b

REM Function to print colored text
:ColorText
set "text=%~1"
set "color=%~2"

if "%color%"=="" (
    echo [ERROR] Color not specified in :ColorText
    exit /b 1
)

powershell -NoProfile -Command "Write-Host '%text%' -ForegroundColor %color%"
exit /b 0