#!/bin/bash

####################
# Settings section #
####################

# Osqa settings
# Whether development version should be used
# Svn revision 602 used by meta.osqa.net is used if this is set to 'no'
OSQA_USE_DEV_VERSION=yes

# Where to put osqa folder
OSQA_DIR_BASE="/home"

# Folder to hold osqa files
OSQA_DIR="osqa"

# Sqlite3 database file
OSQA_DB_FILE="osqa_sqlite3.db"

# Single-space separated list of unused modules
# See http://wiki.osqa.net/display/docs/OSQA+Installation+and+Upgrade+Guides
# IMPORTANT: at least one badges module should be enabled
OSQA_DISABLED_MODULES=""

# Localization-related settings
# Supported locales: de/en/es/fr/pt/zh_CN
OSQA_LOCALE=
OSQA_TIMEZONE=

# Nginx settings
# Domain name used for osqa site
OSQA_SERVER_DOMAIN="example.com"

# Number of nginx worker processes
# Tweak this according to your host hardware specification
NGINX_PROCESS=2

# Uwsgi settings
# Number of uwsgi worker processes
UWSGI_PROCESS=4

###################################
# Dependency-installation section #
###################################

# Update system
apt-get install -y python-software-properties
add-apt-repository ppa:stevecrozz/ppa
apt-get update
apt-get upgrade -y

# Install utilities and dependencies
for i in busybox-static \
         syslog-ng \
         sqlite3 \
         subversion \
         python-setuptools \
         python-pip \
         python-software-properties \
         ; do
  apt-get install $i -yf
done

# Uncomment to use postfix as mail server
# Select 'Internet Site' and set mail name to OSQA_SERVER_DOMAIN
apt-get install postfix -yf

# OPTIONAL
# Uncomment to disable inbound mail
#sed -i "s%^\(inet_interfaces\ =\ \).*%\1loopback-only%" /etc/postfix/main.cf

# Install osqa python dependencies
for i in django \
         elementtree \
         south \
         markdown \
         html5lib \
         python-openid \
         sphinx \
         sphinxsearch \
         ; do
  pip install $i
done

#############################
# Osqa installation section #
#############################

# Install osqa
mkdir -p $OSQA_DIR_BASE
cd $OSQA_DIR_BASE
rm -rf $OSQA_DIR
if [ "$OSQA_USE_DEV_VERSION" = no ]; then
  svn co -r 602 http://svn.osqa.net/svnroot/osqa/trunk $OSQA_DIR
else
  svn co http://svn.osqa.net/svnroot/osqa/trunk $OSQA_DIR
fi

# Unicode safety fix
sed -i "s/mark_safe\((settings.*\)/mark_safe(unicode\1)/g" \
  $OSQA_DIR/forum/skins/default/templates/notifications/*.html

# Configure osqa
cat $OSQA_DIR/settings_local.py.dist | \
  sed -e "s%^\(DATABASE_NAME\ =\ \).*%\1'$OSQA_DIR_BASE/$OSQA_DIR/$OSQA_DB_FILE'%" \
      -e "s%^\(DATABASE_ENGINE\ =\ \).*%\1'sqlite3'%" \
      -e "s%^\(APP_URL\ =\ 'http://\).*%\1$OSQA_SERVER_DOMAIN'%" \
  > $OSQA_DIR/settings_local.py

[ -n "$OSQA_LOCALE" ] && sed -i "s%^\(LANGUAGE_CODE\ =\ \).*%\1'$OSQA_LOCALE'%" \
  $OSQA_DIR/settings_local.py

[ -n "$OSQA_TIMEZONE" ] && \
  sed -i "s%^\(TIME_ZONE\ =\ \).*%\1'$OSQA_TIMEZONE'%" $OSQA_DIR/settings_local.py

for i in $OSQA_DISABLED_MODULES; do
  sed -i "s%^\(DISABLED_MODULES.*\)]%\1,\ '$i']%" $OSQA_DIR/settings_local.py
done

# Setup symlink to django admin interface
ln -s /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media \
  $OSQA_DIR/admin_media

# OPTIONAL
# Uncomment to hide post content in rss feeds for anti-scraping purpose
#sed -i "s/item\.html/\"\"/g" $OSQA_DIR/forum/feed.py
#sed -i "s/question\.html/\"\"/g" $OSQA_DIR/forum/feed.py

cat $OSQA_DIR/osqa.wsgi.dist | \
  sed -e "s%/path/to_dir_above%$OSQA_DIR_BASE%g" \
      -e "s%osqa\(\.settings\)%$OSQA_DIR\1%" > $OSQA_DIR/osqa_wsgi.py

# It's not necessary to create 'superuser' at this step
python $OSQA_DIR/manage.py syncdb --all || exit 1
python $OSQA_DIR/manage.py migrate forum --fake

# Set ownership and permissions
chown -R www-data $OSQA_DIR
chmod -R g+w $OSQA_DIR/forum/upfiles $OSQA_DIR/log $OSQA_DIR/$OSQA_DB_FILE

####################################
# Nginx/Uwsgi installation section #
####################################

# Install and configure nginx and uwsgi
apt-get install uwsgi nginx -yf

# Create uwsgi config file
cat > /etc/uwsgi.xml << EOF
<uwsgi>
  <gid>www-data</gid>
  <uid>www-data</uid>
  <vhost/>
  <disable-logging/>
  <socket>127.0.0.1:9090</socket>
  <processes>$UWSGI_PROCESS</processes>
  <master/>
  <harakiri>20</harakiri>
  <limit-as>128</limit-as>
  <no-orphans/>
  <reaper/>
  <max-requests>1000</max-requests>
</uwsgi>
EOF

# Set nginx worker process number
sed -i "s%\(worker_processes\).*%\1\ \ $NGINX_PROCESS;%" /etc/nginx/nginx.conf

# Create nginx site
cat > /etc/nginx/sites-enabled/$OSQA_SERVER_DOMAIN << EOF
server {
  listen          80;
  server_name     $OSQA_SERVER_DOMAIN;

  location /m/ {
    alias $OSQA_DIR_BASE/$OSQA_DIR/forum/skins/;
  }

  location /upfiles/ {
    alias $OSQA_DIR_BASE/$OSQA_DIR/forum/upfiles/;
  }

  location /admin_media/ {
    alias $OSQA_DIR_BASE/$OSQA_DIR/admin_media/;
  }

  location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:9090;
    uwsgi_param UWSGI_CHDIR $OSQA_DIR_BASE/$OSQA_DIR;
    uwsgi_param UWSGI_PYHOME $OSQA_DIR_BASE;
    uwsgi_param UWSGI_SCRIPT osqa_wsgi;
  }
}
EOF

# OPTIONAL: disable nginx default site for security
rm -f /etc/nginx/sites-enabled/default

# Start services
service uwsgi restart || exit 1
service nginx restart || exit 1

echo "OSQA has been successfully installed."
echo "Visit $OSQA_SERVER_DOMAIN now, create superuser and customize OSQA."

exit 0

