#! /bin/bash

if [ $1 == "start" ]; then 
    # Add ssh key just in case we need to log into Mongo. 
    cat /service/keys/id_rsa.pub >> /root/.ssh/authorized_keys

    # Change the file ownership back to ferry user. This is 
    # necessary since the host may not have the ferry user. 
    chown -R ferry:ferry $MONGO_DATA
    chown -R ferry:ferry $(dirname $MONGO_LOG)

    # Start the mongo server
    su ferry -c '/usr/bin/mongod --unixSocketPrefix /service/com --fork --logpath $MONGO_LOG --dbpath $MONGO_DATA --smallfiles'

    # Start the ssh server in the foreground
    /usr/sbin/sshd -D
elif [ $1 == "stop" ]; then 
    # Shutdown MongoDB. 
    su ferry -c '/usr/bin/mongod --shutdown'

    # Killing the ssh daemon should automatically exit the container,
    # since the sshd is the only thing running in the foreground. 
    pkill -f sshd
fi