More adventures into self-hosting Ghost. I have Disqus up and running again, all the comments are still there. Since Disqus was based on URL, and the URL didn't change, everything just started working once the Disqus universal code was added. I did run into the same problem as I did with the security.txt file (which is the reason I'm documenting this).

The section of code I need to add this to is post.hbs in /var/www/ghost/versions/1.23.1/content/themes/casper/ (1.23.1 is the version number, and will change). So, like the location for security.txt, this will change every time Ghost is updated.

Another short script, that you can either run on a cron job, every update, etc. fixes this. I reused the same first few lines to enumerate and move into the version directories. Then I test to see if the word "disqus" is in post.hbs, if it is, we've already fixed this file, skip it. If not, backup post.hbs, and then the real magic happens.

sed 'N;/{{content}}\n.*<\/div>$/ r /home/ubuntu/disqus_universal.txt' $dir/post.hbs.bak > $dir/post.hbs: Sed finds "{{content}}" and the </div> in the following line in post.hbs.bak and, directly after that, adds the content of the file "disqus_universal.txt", directing the output into post.hbs.

disqus_universal.txt contains, of course, the universal embed code.

#!/bin/bash

for f in /var/www/ghost/versions/*; do
    if [ -d ${f} ]; then
		# echo $f
        dir="$f/content/themes/casper"
		# echo $dir
		
		if grep -q disqus $dir/post.hbs; then
			echo "Disqus already set in $dir"
		else
			echo "Provisioning Disqus in $dir/post.hbs"
            mv $dir/post.hbs $dir/post.hbs.bak
            sed 'N;/{{content}}\n.*<\/div>$/ r /home/ubuntu/disqus_universal.txt' $dir/post.hbs.bak > $dir/post.hbs
        fi
    fi
done

Update 7/28/2018: Around v1.25.0 Ghost made a change to post.hbs

Previous code:

            <section class="post-full-content">
                {{content}}
            </section>

New Code:

            <section class="post-full-content">
                <div class="post-content">
                    {{content}}
                </div>
            </section>

Placing the Disqus universal code directly after the content tag would now place it within the "post-content" div, causing issues (it doesn't exactly not render, but it might as well, it almost appears 1 pixel wide). So we now have to place it after the </div> and before the </section>. Therefore, we modify our sed command. Per this comment:

sed normally works on one line at a time. If you want to work on several lines in the same command, you have to add them to the pattern buffer with the N command:".

I've replaced the original sed command with the new multiline version. We still look for the {{content}} tag, but now add a line break (\n) followed by matching anything (.*) to account for spaces/tabs leading up to the </div> tag, which comes at the end of the line ($).

This appears to work correctly, but I won't be absolutly sure until I try it on the next update.