#!/bin/bash source /opt/jekyll/.env export AUTHOR_EMAIL=$AUTHOR_EMAIL export BLOG_NAME=$BLOG_NAME export BASE_URL=$BASE_URL export URL=$URL export GITHUB_USERNAME=$GITHUB_USERNAME export LIVERELOAD_PORT=$LIVERELOAD_PORT export HOST_URL=$HOST_URL export SITE_TITLE=$SITE_TITLE export THEME=$THEME export TWITTER_USERNAME=$TWITTER_USERNAME export WWW_PORT=$WWW_PORT export GEM_HOME="/usr/local/bundle" export PATH="$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH" export SITE_PATH="/app/jekyll/$BLOG_NAME" update_config() { for ENV_VAR in 'AUTHOR_EMAIL' 'BASE_URL' 'GITHUB_USERNAME' \ 'SITE_TITLE' 'THEME' 'TWITTER_USERNAME' 'URL'; do case $ENV_VAR in "AUTHOR_EMAIL") param="email" ;; "BASE_URL") param="baseurl" ;; "GITHUB_USERNAME") param="github_username" ;; "SITE_TITLE") param="title" ;; "THEME") param="theme" ;; "TWITTER_USERNAME") param="twitter_username" ;; "URL") param="url" ;; esac initial_config=$(grep -E "^${param}: " ${SITE_PATH}/_config.yml) [[ "$initial_config" != "$param: ${!ENV_VAR}" && ! -z ${!ENV_VAR} ]] && { echo "Config change detected on $param: updating _config.yml" echo "New $param: ${!ENV_VAR}" [[ $ENV_VAR == "BASE_URL" || $ENV_VAR == "URL" ]] && { sed -i "s/\(${param}: \).*/\1\"${!ENV_VAR}\"/g" ${SITE_PATH}/_config.yml } || { sed -i "s/\(${param}: \).*/\1${!ENV_VAR}/g" ${SITE_PATH}/_config.yml } } || { echo "No change or empty variable for $param, continuing..." } done } update_description() { initial_description=$(sed -n '/^description/,/^baseurl/p' ${SITE_PATH}/_config.yml | sed '/^description/d' | sed '/^baseurl: ""/d' | sed 's/^[[:space:]]*//' | sed '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! { /^$/d; } }') current_description=$(sed 's/^[[:space:]]*//' /opt/jekyll/${BLOG_NAME}_description.txt) [[ "$initial_description" != "$current_description" && ! -z $current_description ]] && { echo "Config change detected on site description: updating _config.yml" sed -i "/^description/,/^baseurl:/!b;//!d;/^description/r /opt/jekyll/${BLOG_NAME}_description.txt" ${SITE_PATH}/_config.yml sed -i '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! s/^[^[:space:]]/ &/; }' ${SITE_PATH}/_config.yml } || { echo "No change or empty variable for site $BLOG_NAME description, continuing..." sed -i '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! s/^[^[:space:]]/ &/; }' ${SITE_PATH}/_config.yml sed '/^description:/,/^baseurl:/ { /^description:/n; /^baseurl:/! { /^$/d; } }' } } cd /app/jekyll && gem install jekyll jekyll-docs jekyll-compose bundler webrick [[ -f ${SITE_PATH}/_config.yml ]] && { cd $SITE_PATH bundle install update_config update_description bundle exec jekyll serve --host $HOST_URL } || { jekyll new $SITE_PATH cd $SITE_PATH bundle install update_config update_description bundle exec jekyll serve --host $HOST_URL }