#!/bin/bash -e
# The assemble script builds the application artifacts from source and
# places them into appropriate directories inside the image.

# Required to install `ffi` separately due to an installation issue with ffi v-1.9.21
echo "---> Installing ffi ..."
gem install ffi --version 1.9.18
# Required to install `asciidoctor-diagram` separately due to a "undefined method `enable_dsl'" error with asciidoctor-diagram v-1.5.17
echo "---> Installing asciidoctor-diagram ..."
gem install asciidoctor-diagram --version 1.5.16
# `gem install` is required because `bundle install` does not properly place `asciibinder` into $PATH
echo "---> Installing AsciiBinder ..."
# gem install ascii_binder --version 0.1.15.1
gem install ascii_binder

# Move git repository to local working directory
shopt -s dotglob
echo "---> Installing application source ..."
mv /tmp/src/* ./

# Fetch and locally add all remote branches to ensure AsciiBinder is able to build the necessary branches
echo "---> Fetching remote branches"
# Because the s2i builder only makes the remote branch applicable to the specified branch,
# it's necessary to enforce a * ref so that all branches are referenced
sed -i 's%fetch = +refs.*%fetch = +refs/heads/*:refs/remotes/origin/*%' .git/config
git fetch --all --quiet
for remote in $(git branch -r | egrep -v "(>|$(git rev-parse --abbrev-ref HEAD))$|master$"); do git checkout --force --track $remote; done
git checkout master

# Fixes incompatible character encodings: US-ASCII and UTF-8 error
export LANG="en_US.UTF-8"

echo "---> AsciiBinder packaging commercial content ..."
# AsciiBinder uses git to some extent and requires `user.email` to be properly set
git config user.email "devel@openshift.com"
# Package assets for commercial site only, without Minishift content
asciibinder package --site=commercial
# Move commercial content to its own directory, including commercial specific redirects and 404 page

mkdir commercial_package
mv _package/commercial commercial_package
git checkout master
mkdir commercial_package/commercial/httpd-cfg
mv .s2i/httpd-cfg/01-commercial.conf commercial_package/commercial/httpd-cfg
mv 404-commercial.html commercial_package/commercial/404.html

# minishift docs only need to be on 3.x

git checkout enterprise-3.11
echo "---> Installing Minishift content ..."
mkdir minishift
cd minishift
if wget http://minishift.io/minishift-adoc.tar ; then
    tar -xvf minishift-adoc.tar --strip 1
    cat _topic_map.yml >> ../_topic_map.yml
    rm minishift-adoc.tar
    cd ..
    git add minishift/
    git commit -am "Minishift build-time commit"
else
    echo "WARNING: Could not retrieve minishift-adoc.tar"
    cd ..
    rmdir minishift
fi

# checking back into master before resuming the packaging of the community content
git checkout master

echo "---> AsciiBinder packaging community content ..."
# Package assets for community site only, with Minishift content
asciibinder package --site=community
# Move community content to its own directory, including community specific redirects and 404 page

mkdir community_package
mv _package/community community_package
git checkout master
mkdir community_package/community/httpd-cfg
mv .s2i/httpd-cfg/01-community.conf community_package/community/httpd-cfg
mv 404-community.html community_package/community/404.html

# Optionally install .htaccess files to gate access to staging content
# Define a HTACCESS_DIRS environment variable with ':' delimited directories
# Reference directory like "commercial_package/commercial/container-platform/3.10:community_package/community/latest" for docs.openshift.com/container-platform/3.10 and docs.okd.io/latest
if [[ ! -z "$HTACCESS_DIRS" ]]; then
    echo "---> Creating .htaccess/.htpasswd files"
    IFS=':'; directories=($HTACCESS_DIRS); unset IFS;
    for dir in "${directories[@]}"; do
        echo "--->     Adding .htpasswd protection to $dir"

        # brute force separate password for container-platform-ocp distribution
        if [ $dir = "commercial_package/commercial/container-platform-ocp/4.3" ] || [ $dir = "commercial_package/commercial/container-platform-ocp/4.4" ] || [ $dir = "commercial_package/commercial/container-platform-ocp/4.7" ]; then
          echo "--->     separate .htpasswd protection to $dir"

          echo -e 'AuthType Basic\nAuthName "Access to the stage docs"\nAuthUserFile /opt/app-root/src/.htpasswdocp\nRequire valid-user' > $dir/.htaccess

          echo 'openshift:$apr1$c41fpuxh$jHe/W0gYLffn6501Cx2TS/' > commercial_package/commercial/.htpasswdocp
        else
          echo -e 'AuthType Basic\nAuthName "Access to the stage docs"\nAuthUserFile /opt/app-root/src/.htpasswd\nRequire valid-user' > $dir/.htaccess
        fi

    done
    echo 'redhat:$apr1$1HYe8rB6$6pa5OVd01quYUYl8ymyqK0' > commercial_package/commercial/.htpasswd
    echo 'redhat:$apr1$1HYe8rB6$6pa5OVd01quYUYl8ymyqK0' > community_package/community/.htpasswd
fi

# Optionally restrict crawlers from indexing the content
# Define a DISALLOW_CRAWL_DIRS environment variable with ':' delimited directories
# Reference directory like "commercial_package/commercial/container-platform/3.10:community_package/community/latest" for docs.openshift.com/container-platform/3.10 and docs.okd.io/latest
if [[ ! -z "$DISALLOW_CRAWL_DIRS" ]]; then
    echo "---> Creating robot.txt file for disallowing crawlers"
    IFS=':'; directories=($DISALLOW_CRAWL_DIRS); unset IFS;
    for dir in "${directories[@]}"; do
        echo "--->     Creating robots.txt to prevent crawling dir $dir"
        PACKAGE=$(echo $dir | cut -d / -f 1)
        TYPE=$(echo $dir | cut -d / -f 2)
        URL_DIR=$(echo $dir | cut -d / -f 3)
        URL_VERSION=$(echo $dir | cut -d / -f 4)
        touch $PACKAGE/$TYPE/robots.txt
        echo -e "User-agent: *\nDisallow: /$URL_DIR/$URL_VERSION" >> $PACKAGE/$TYPE/robots.txt
    done
fi


# Fix source directory permissions
echo "---> Fixing permissions ..."
fix-permissions ./
