#!/bin/bash

# Run: curl -s https://raw.github.com/r888888888/danbooru/master/INSTALL.debian > install.sh ; chmod +x install.sh ; ./install.sh

if [[ "$(whoami)" != "root" ]] ; then
  echo "You must run this script as root"
  exit 1
fi

echo "Danbooru Install"
echo "This script will install Ruby, Rails, PostgreSQL, and Nginx. By the end,"
echo "you should be able to connect to the server and create an account."
echo
echo "It will create a new user called danbooru which will run the Danbooru"
echo "processes. It will download the latest copy and install it in"
echo "/var/www/danbooru."
echo
echo -n "Enter the hostname for this server (ex: danbooru.donmai.us): "
read HOSTNAME

if [[ -z "$HOSTNAME" ]] ; then
  echo "Must enter a hostname"
  exit 1
fi

echo -n "Enter a name for the site (default: Danbooru): "
read SITENAME

if [[ -z "$SITENAME" ]] ; then
  SITENAME=Danbooru
fi

# Install packages
echo "Installing packages..."
apt-get update
apt-get -y install build-essential automake openssl libssl-dev libyaml-dev libxml2-dev libxslt-dev autoconf ncurses-dev sudo gcc g++ libreadline-dev zlib1g-dev flex bison bzip2 ragel memcached libmemcache-dev git-core curl libcurl4-openssl-dev emacs imagemagick libmagickcore-dev libmagickwand-dev sendmail psmisc ffmpegthumbnailer

# Install PostgreSQL 9.1
apt-get -y install python-software-properties
add-apt-repository ppa:pitti/postgresql
apt-get update
apt-get install -y postgresql-9.1 postgresql-contrib-9.1 libpq-dev

if [ $? -ne 0 ]; then
  exit 1
fi

# Install RVM
echo "Installing RVM..."
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
echo "source /usr/local/rvm/scripts/rvm" >> /etc/bash.bashrc
echo "source /usr/local/rvm/scripts/rvm" >> /etc/profile
source /usr/local/rvm/scripts/rvm
rvm install ruby-1.9.2-p290 --with-openssl-dir=/usr/local
rvm 1.9.2 --default

# Install gems
gem install --no-ri --no-rdoc capistrano
gem install --no-ri --no-rdoc capistrano-ext
gem install --no-ri --no-rdoc bundler

# Install Passenger
gem install --no-ri --no-rdoc -v 3.0.9 passenger
bash -c '/usr/local/rvm/bin/rvm exec passenger-install-nginx-module --auto --auto-download --prefix=/opt/nginx'
rm -f /opt/nginx/conf/*.default

# Create user account
useradd -m danbooru
chsh -s /bin/bash danbooru
echo "source /usr/local/rvm/scripts/rvm" > /home/danbooru/.bashrc
chmod g-wx,o-wx /home/danbooru/.bashrc
chown danbooru:danbooru /home/danbooru/.bashrc
echo "export rvm_path=/usr/local/rvm" > /etc/rvmrc
addgroup wheel
usermod -G root,wheel root
usermod -G danbooru,wheel danbooru

# Update PostgreSQL
PG_HBA_FILE="/etc/postgresql/9.1/main/pg_hba.conf"
echo "local    all         postgres,danbooru                              trust" > $PG_HBA_FILE
echo "host     all         postgres,danbooru          127.0.0.1/32        trust" >> $PG_HBA_FILE

/etc/init.d/postgresql restart
sudo -u postgres createuser -s danbooru

mkdir -p /var/log/www

# Setup nginx
curl -s https://raw.github.com/r888888888/danbooru/master/script/install/init_d.templ > /etc/init.d/nginx
chmod +x,g-w /etc/init.d/nginx
update-rc.d -f nginx defaults
mkdir -p /opt/nginx/conf/sites
curl -s https://raw.github.com/r888888888/danbooru/master/script/install/nginx.conf > /opt/nginx/conf/nginx.conf
curl -s https://raw.github.com/r888888888/danbooru/master/script/install/nginx.danbooru.conf > /opt/nginx/conf/sites/danbooru.conf
sed -i -e 's/__hostname__/$HOSTNAME/g' /opt/nginx/conf/sites/danbooru.conf
/etc/init.d/nginx start

echo "%wheel  ALL=(ALL)       ALL" >> /etc/sudoers

# Setup logrotate
LOGROTATE_CONF_FILE="/etc/logrotate.conf"
echo >> $LOGROTATE_CONF_FILE
echo "/var/log/www/danbooru.access.log" >> $LOGROTATE_CONF_FILE
echo "/var/log/www/danbooru.error.log">> $LOGROTATE_CONF_FILE
echo "/var/www/danbooru/current/log/production.log {" >> $LOGROTATE_CONF_FILE
echo "  daily" >> $LOGROTATE_CONF_FILE
echo "  rotate 3" >> $LOGROTATE_CONF_FILE
echo "  copytruncate" >> $LOGROTATE_CONF_FILE
echo "}" >> $LOGROTATE_CONF_FILE

# Setup danbooru account
echo
echo "*************************************************"
echo "* Enter a new password for the danbooru account *"
echo "*************************************************"
echo
passwd danbooru

sudo -u danbooru createdb danbooru2
cd /home/danbooru
sudo -u danbooru mkdir /home/danbooru/apps
cd /home/danbooru/apps
git clone git://github.com/r888888888/danbooru.git danbooru
chown -R danbooru:danbooru danbooru

echo
echo
echo "**************************************************************"
echo "* Danbooru is now ready for deployment.                      *"
echo "*                                                            *"
echo "* A local copy has been checked out into /home/danbooru/apps *"
echo "* but you may want to clone your own copy from               *"
echo "* http://github.com/r888888888/danbooru.git                  *"
echo "*                                                            *"
echo "* You will probably want to create your own branch and       *"
echo "* modify the config/deploy/staging.rb and production.rb      *"
echo "* files to point to the correct servers. Then run:           *"
echo "* cap deploy:setup && cap deploy                             *"
echo "*                                                            *"
echo "* The first account will automatically be made an admin.     *"
echo "* You can edit the configuration files by editing            *"
echo "* /var/www/danbooru/shared/config/local_config and           *"
echo "* /var/www/danbooru/shared/config/database.yml               *"
echo "**************************************************************"
