#!/bin/bash

# Run: curl -s https://raw.githubusercontent.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 INSTALLATION SCRIPT"
echo "*"
echo "* This script will install all the necessary packages to run Danbooru on a   "
echo "* Debian server."
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

export RUBY_VERSION=2.1.5
export GITHUB_INSTALL_SCRIPTS=https://raw.githubusercontent.com/r888888888/danbooru/master/script/install

# Install packages
echo "* Installing packages..."
apt-get update
apt-get -y install build-essential automake libssl-dev libxml2-dev libxslt-dev ncurses-dev sudo libreadline-dev flex bison ragel memcached libmemcached-dev git curl libcurl4-openssl-dev imagemagick libmagickcore-dev libmagickwand-dev sendmail-bin sendmail postgresql postgresql-contrib libpq-dev nginx ssh

if [ $? -ne 0 ]; then
  echo "* Error installing packages; aborting"
  exit 1
fi

# Create user account
useradd -m danbooru
chsh -s /bin/bash danbooru
usermod -G danbooru,sudo danbooru

# Install rbenv
echo "* Installing rbenv..."
cd /
sudo -u danbooru git clone git://github.com/sstephenson/rbenv.git ~danbooru/.rbenv
sudo -u danbooru touch ~danbooru/.bash_profile
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~danbooru/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~danbooru/.bash_profile
sudo -u danbooru mkdir -p ~danbooru/.rbenv/plugins
sudo -u danbooru git clone git://github.com/sstephenson/ruby-build.git ~danbooru/.rbenv/plugins/ruby-build
sudo -u danbooru bash -l -c "rbenv install $RUBY_VERSION"
sudo -u danbooru bash -l -c "rbenv global $RUBY_VERSION"

# Generate secret token and secret key
echo "* Generating secret keys..."
sudo -u danbooru mkdir ~danbooru/.danbooru/
sudo -u danbooru openssl rand -hex 32 > ~danbooru/.danbooru/secret_token
sudo -u danbooru openssl rand -hex 32 > ~danbooru/.danbooru/session_secret_key
chmod 600 ~danbooru/.danbooru/*

# Install gems
echo "* Installing gems..."
sudo -u danbooru bash -l -c 'gem install --no-ri --no-rdoc bundler'

echo "* Install configuration scripts..."

# Update PostgreSQL
curl -L -s $GITHUB_INSTALL_SCRIPTS/postgresql_hba_conf -o /etc/postgresql/9.1/main/pg_hba.conf
/etc/init.d/postgresql restart
sudo -u postgres createuser -s danbooru
sudo -u danbooru createdb danbooru2

# Setup nginx
curl -L -s $GITHUB_INSTALL_SCRIPTS/nginx.danbooru.conf -o /etc/nginx/conf.d/danbooru.conf
sed -i -e "s/__hostname__/$HOSTNAME/g" /etc/nginx/conf.d/danbooru.conf
/etc/init.d/nginx restart

# Setup logrotate
curl -L -s $GITHUB_INSTALL_SCRIPTS/danbooru_logrotate_conf -o /etc/logrotate.d/danbooru.conf

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

echo "* Setting up SSH keys for the danbooru account"
sudo -u danbooru ssh-keygen

echo "* Setting up danbooru app..."
sudo -u danbooru git clone https://github.com/r888888888/danbooru ~danbooru/danbooru
sudo -u danbooru bash -l -c 'cd ~/danbooru ; bundle'

echo "* Creating a new local git branch called develop"
sudo -u danbooru bash -l -c 'cd ~/danbooru ; git checkout -b develop'

mkdir -p /var/www/danbooru2/shared/config
mkdir -p /var/www/danbooru2/shared/data
chown -R danbooru:danbooru /var/www/danbooru2
sudo -u danbooru bash -l -c 'cp ~/danbooru/script/install/database.yml.templ /var/www/danbooru2/shared/config/database.yml'
sudo -u danbooru bash -l -c 'cp ~/danbooru/script/install/danbooru_local_config.rb.templ /var/www/danbooru2/shared/config/danbooru_local_config.rb'

echo "* Almost done! The code has been checked out at ~danbooru/danbooru. You can  "
echo "* now login as the danbooru user and run the following commands to deploy to "
echo "* the server:"
echo "*"
echo "*   cd ~/danbooru"
echo "*   bundle exec cap development deploy"
