#!/bin/bash COLOR_BLUE="\033[38;2;51;100;255m" COLOR_RESET="\033[0m" echo "${COLOR_BLUE}" echo "==========================================" echo " setting up coryd.dev locally " echo "==========================================" echo "${COLOR_RESET}" # step 1: retrieve and build .env file from 1password echo "${COLOR_BLUE}signing in to 1password...${COLOR_RESET}" eval $(op signin) echo "${COLOR_BLUE}fetching secrets from 1password...${COLOR_RESET}" SECRETS_JSON='{ "POSTGREST_URL": "{{ op://Private/coryd.dev secrets/POSTGREST_URL }}", "POSTGREST_API_KEY": "{{ op://Private/coryd.dev secrets/POSTGREST_API_KEY }}", "MASTODON_ACCESS_TOKEN": "{{ op://Private/coryd.dev secrets/MASTODON_ACCESS_TOKEN }}", "MASTODON_SYNDICATION_TOKEN": "{{ op://Private/coryd.dev secrets/MASTODON_SYNDICATION_TOKEN }}", "FORWARDEMAIL_API_KEY": "{{ op://Private/coryd.dev secrets/FORWARDEMAIL_API_KEY }}", "BOOK_IMPORT_TOKEN": "{{ op://Private/coryd.dev secrets/BOOK_IMPORT_TOKEN }}", "WATCHING_IMPORT_TOKEN": "{{ op://Private/coryd.dev secrets/WATCHING_IMPORT_TOKEN }}", "TMDB_API_KEY": "{{ op://Private/coryd.dev secrets/TMDB_API_KEY }}", "SEASONS_IMPORT_TOKEN": "{{ op://Private/coryd.dev secrets/SEASONS_IMPORT_TOKEN }}", "NAVIDROME_SCROBBLE_TOKEN": "{{ op://Private/coryd.dev secrets/NAVIDROME_SCROBBLE_TOKEN }}", "NAVIDROME_API_URL": "{{ op://Private/coryd.dev secrets/NAVIDROME_API_URL }}", "NAVIDROME_API_TOKEN": "{{ op://Private/coryd.dev secrets/NAVIDROME_API_TOKEN }}", "COOLIFY_REBUILD_TOKEN": "{{ op://Private/coryd.dev secrets/COOLIFY_REBUILD_TOKEN }}" }' SECRETS=$(echo "$SECRETS_JSON" | op inject) if [ -z "$SECRETS" ]; then echo "error: failed to retrieve secrets from 1password." exit 1 fi echo "${COLOR_BLUE}writing .env file...${COLOR_RESET}" echo "$SECRETS" | jq -r 'to_entries | .[] | "\(.key)=\(.value)"' > .env # load environment variables from .env export $(grep -v '^#' .env | xargs) # step 2: generate final config files from templates echo "${COLOR_BLUE}generating configuration files from templates...${COLOR_RESET}" mkdir -p generated for file in scripts/templates/*.template; do [ -e "$file" ] || continue new_file="generated/$(basename ${file%.template})" cp "$file" "$new_file" # use awk to replace placeholders safely awk -v POSTGREST_URL="$POSTGREST_URL" \ -v POSTGREST_API_KEY="$POSTGREST_API_KEY" \ -v FORWARDEMAIL_API_KEY="$FORWARDEMAIL_API_KEY" \ -v MASTODON_ACCESS_TOKEN="$MASTODON_ACCESS_TOKEN" \ -v MASTODON_SYNDICATION_TOKEN="$MASTODON_SYNDICATION_TOKEN" \ -v BOOK_IMPORT_TOKEN="$BOOK_IMPORT_TOKEN" \ -v WATCHING_IMPORT_TOKEN="$WATCHING_IMPORT_TOKEN" \ -v TMDB_API_KEY="$TMDB_API_KEY" \ -v NAVIDROME_SCROBBLE_TOKEN="$NAVIDROME_SCROBBLE_TOKEN" \ -v SEASONS_IMPORT_TOKEN="$SEASONS_IMPORT_TOKEN" \ -v NAVIDROME_API_URL="$NAVIDROME_API_URL" \ -v NAVIDROME_API_TOKEN="$NAVIDROME_API_TOKEN" \ -v ARTIST_IMPORT_TOKEN="$ARTIST_IMPORT_TOKEN" \ -v COOLIFY_REBUILD_TOKEN="$COOLIFY_REBUILD_TOKEN" \ '{gsub(/{{POSTGREST_URL}}/, POSTGREST_URL); gsub(/{{POSTGREST_API_KEY}}/, POSTGREST_API_KEY); gsub(/{{FORWARDEMAIL_API_KEY}}/, FORWARDEMAIL_API_KEY); gsub(/{{MASTODON_ACCESS_TOKEN}}/, MASTODON_ACCESS_TOKEN); gsub(/{{MASTODON_SYNDICATION_TOKEN}}/, MASTODON_SYNDICATION_TOKEN); gsub(/{{BOOK_IMPORT_TOKEN}}/, BOOK_IMPORT_TOKEN); gsub(/{{WATCHING_IMPORT_TOKEN}}/, WATCHING_IMPORT_TOKEN); gsub(/{{TMDB_API_KEY}}/, TMDB_API_KEY); gsub(/{{NAVIDROME_SCROBBLE_TOKEN}}/, NAVIDROME_SCROBBLE_TOKEN); gsub(/{{SEASONS_IMPORT_TOKEN}}/, SEASONS_IMPORT_TOKEN); gsub(/{{NAVIDROME_API_URL}}/, NAVIDROME_API_URL); gsub(/{{NAVIDROME_API_TOKEN}}/, NAVIDROME_API_TOKEN); gsub(/{{ARTIST_IMPORT_TOKEN}}/, ARTIST_IMPORT_TOKEN); gsub(/{{COOLIFY_REBUILD_TOKEN}}/, COOLIFY_REBUILD_TOKEN); print}' "$new_file" > tmpfile && mv tmpfile "$new_file" done echo "${COLOR_BLUE}all configurations generated in the 'generated' folder.${COLOR_RESET}" # step 3: ensure apache_modules.list exists MODULES_LIST="scripts/lists/apache_modules.list" if [ ! -f "$MODULES_LIST" ]; then echo "apache_modules.list not found, generating it..." a2query -m | awk '{print $1}' > "$MODULES_LIST" fi # step 4: ensure php_extensions.list exists PHP_EXTENSIONS_LIST="scripts/lists/php_extensions.list" if [ ! -f "$PHP_EXTENSIONS_LIST" ]; then echo "php_extensions.list not found, generating it..." dpkg --get-selections | awk '/php8.3/ {print $1}' > "$PHP_EXTENSIONS_LIST" fi # step 5: display manual installation instructions echo "${COLOR_BLUE}" echo "==========================================" echo " setup complete! " echo " your local environment is ready! 🚀 " echo "==========================================" echo "${COLOR_RESET}" echo "${COLOR_BLUE}next steps:${COLOR_RESET}" echo "1️⃣ move the coryd.dev.conf apache configuration to the correct location:" echo " sudo a2ensite coryd.dev.conf" echo " sudo systemctl reload apache2" echo "" echo "2️⃣ enable the required apache modules:" if [ -f "$MODULES_LIST" ]; then REQUIRED_MODULES=$(tr '\n' ' ' < "$MODULES_LIST" | sed 's/ *$//') if [ -n "$REQUIRED_MODULES" ]; then echo " sudo a2enmod $REQUIRED_MODULES && sudo systemctl restart apache2" else echo " no required modules found." fi else echo " error: apache_modules.list not found." fi echo "" echo "3️⃣ install the required php extensions:" if [ -f "$PHP_EXTENSIONS_LIST" ]; then REQUIRED_PHP_EXTENSIONS=$(tr '\n' ' ' < "$PHP_EXTENSIONS_LIST" | sed 's/ *$//') if [ -n "$REQUIRED_PHP_EXTENSIONS" ]; then echo " sudo apt install -y $REQUIRED_PHP_EXTENSIONS && sudo systemctl restart php8.3-fpm" else echo " no required php extensions found." fi else echo " error: php_extensions.list not found." fi echo "" echo "4️⃣ apply crontabs manually:" echo " root: crontab -e" echo " www-data: sudo crontab -u www-data -e" echo "${COLOR_BLUE}all done! 🎉${COLOR_RESET}"