#!/bin/sh set -eu # Fail early with a clear message if required envs are missing if [ -z "${INSTANCE:-}" ]; then echo "ERROR: required environment variable INSTANCE is not set. Add it via docker-compose 'env_file: .env' or 'environment:'." >&2 exit 1 fi if [ -z "${HOSTNAME:-}" ]; then echo "ERROR: required environment variable HOSTNAME is not set. Ensure the container hostname is provided or set HOSTNAME via 'environment:'." >&2 exit 1 fi # Create necessary statistics files if they do not exist for file in \ "/stats/${HOSTNAME}@${INSTANCE}.csv" \ "/stats/${HOSTNAME}@${INSTANCE}.failed_conversions.log" \ "/stats/${HOSTNAME}@${INSTANCE}.ignored_conversion.log" \ "/stats/${HOSTNAME}@${INSTANCE}.successful_conversions.log" do if [ ! -f "$file" ]; then touch "$file" chmod 644 "$file" chown 1000:1000 "$file" fi done if [ -f /config/machine-id ]; then # Download HandBrake presets.json wget -O /config/ghb/presets.json https://static.aronmgv.com/scripts/handbrake/assets/presets.json chmod 755 /config/ghb/presets.json chown 1000:1000 /config/ghb/presets.json # Download HandBrake hooks mkdir -p /config/hooks wget -O /config/hooks/hb_custom_args.sh https://static.aronmgv.com/scripts/handbrake/hb_custom_args.sh wget -O /config/hooks/post_conversion.sh https://static.aronmgv.com/scripts/handbrake/post_conversion.sh wget -O /config/hooks/post_watch_folder_processing.sh https://static.aronmgv.com/scripts/handbrake/post_watch_folder_processing.sh wget -O /config/hooks/pre_conversion.sh https://static.aronmgv.com/scripts/handbrake/pre_conversion.sh chmod 755 /config/hooks/*.sh chown 1000:1000 /config/hooks/*.sh # Download statistics file if it does not exist if [ ! -f /config/statistics ]; then wget -O /config/statistics https://static.aronmgv.com/scripts/handbrake/assets/statistics chmod 755 /config/statistics chown 1000:1000 /config/statistics fi # Replace first line of statistics CSV with first line from remote file only if they differ remote_first_line=$(wget -qO- https://static.aronmgv.com/scripts/handbrake/assets/statistics | head -n 1) if [ -f "/stats/${HOSTNAME}@${INSTANCE}.csv" ]; then local_first_line=$(head -n 1 "/stats/${HOSTNAME}@${INSTANCE}.csv") # Use Python CSV parsing to determine insert positions and rebuild rows. # Python handles quoted fields correctly (commas inside quotes). tail -n +2 "/stats/${HOSTNAME}@${INSTANCE}.csv" > /tmp/${HOSTNAME}@${INSTANCE}.csv_rest if [ "$remote_first_line" != "$local_first_line" ]; then # Run external fixer script hosted at static.aronmgv.com to keep init.sh small. # Download to a temp file then execute to preserve behaviour on /bin/sh. wget -qO- "https://static.aronmgv.com/scripts/handbrake/assets/fixcsv.py" | python3 - "$remote_first_line" "$local_first_line" "/tmp/${HOSTNAME}@${INSTANCE}.csv_rest" "/tmp/${HOSTNAME}@${INSTANCE}.csv_rest_mod" # Check if any new columns were added by the Python script if [ -s "/tmp/${HOSTNAME}@${INSTANCE}.csv_rest_mod" ]; then echo "Header changed: adding empty column(s) (python)"; true fi # Rebuild CSV: new header then modified rest echo "$remote_first_line" > "/stats/${HOSTNAME}@${INSTANCE}.csv" cat /tmp/${HOSTNAME}@${INSTANCE}.csv_rest_mod >> "/stats/${HOSTNAME}@${INSTANCE}.csv" rm -f /tmp/${HOSTNAME}@${INSTANCE}.csv_rest /tmp/${HOSTNAME}@${INSTANCE}.csv_rest_mod else # No new columns found, just replace header. echo "$remote_first_line" > "/stats/${HOSTNAME}@${INSTANCE}.csv" cat "/tmp/${HOSTNAME}@${INSTANCE}.csv_rest" >> "/stats/${HOSTNAME}@${INSTANCE}.csv" echo "Updating statistics file header" fi fi fi