# Maintainer: fauno <fauno@kiwwwi.com.ar>
# Maintainer: Hilton Medeiros <medeiros.hilton at gmail dot com>
# Contributor: Jonne Haß <me@mrzyx.de>
# Contributor: Splashy <splash at open-web dot fr>

# After installing, see the Diaspora wiki page for more information:
# https://wiki.archlinux.org/index.php/Diaspora

pkgbase=diaspora-git
pkgname=('diaspora-mysql-git' 'diaspora-postgresql-git')
pkgver=20120105
pkgrel=2
pkgdesc="A privacy aware, personally controlled, do-it-all, open source social network"
arch=('i686' 'x86_64')
url="https://diasporafoundation.org/"
license=('AGPL3')
depends=('ruby' 'redis' 'imagemagick' 'libxslt' 'net-tools')
makedepends=('git' 'libmysqlclient' 'postgresql-libs')
provides=('diaspora')
install=diaspora.install
source=('diaspora.bin'
        'diaspora.rc'
        'diaspora.logrotate'
        'diaspora.pam'
        'diaspora.bashrc')
md5sums=('547b0015c83da22bd739dc85fd79f9b9'
         'b7a893271d9ba778f74569eff4190c06'
         '60f6b3972c73cbc6b1c9ab87c88fb655'
         '96f82c38f3f540b53f3e5144900acf17'
         '53867bfd389dd267eefcefb053fecc5b')

_gitroot="git://github.com/diaspora/diaspora.git"
_gitname="diaspora"

build() {
  cd "$srcdir"

  msg "Connecting to the Git server..."

  if [[ -d $_gitname ]] ; then
    pushd $_gitname
    git pull origin
    msg "The local files are updated."
    popd
  else
    git clone $_gitroot
    msg "Git clone done."
  fi

  msg "Start making..."

  [[ -d $_gitname-build ]] && rm -fr $_gitname-build
  git clone $_gitname $_gitname-build
  cd ${srcdir}/${_gitname}-build

  # Symlink configs 
  ln -s /etc/webapps/$_gitname/application.yml config/application.yml
  ln -s /etc/webapps/$_gitname/database.yml config/database.yml
  ln -s /etc/webapps/$_gitname/script_server.yml config/script_server.yml
  ln -s /etc/webapps/$_gitname/oauth_keys.yml config/oauth_keys.yml

  # Set version header
  sed "s|git_cmd = \`git log -1 --pretty=\"format:%H %ci\"\`|git_cmd = \"$(git log -1 --pretty="format:%H %ci")\"|" \
      -i config/initializers/version_header.rb

  # Preconfigure config/script_server.yml.example
  sed 's|rails_env: "development"|rails_env: "production"|' \
      -i config/script_server.yml.example
  
  # Preconfigure right ca_file
  sed "s|ca_file: '/etc/pki/tls/certs/ca-bundle.crt' # CentOS|ca_file: '/etc/ssl/certs/ca-certificates.crt' # Arch|" \
      -i config/application.yml.example

  # We make this now because we do not install the git history
  # Comply with AGPLv3
  tar czf public/source.tar.gz  `git ls-tree -r master | awk '{print $4}'`

  # Remove git repo
  rm -fr .git

  # Create gem cache
  mkdir -p ${srcdir}/gem-cache
}

# Helper
_package() {
  cd "${srcdir}"

  usrdir="${pkgdir}/usr/share/webapps"

  install -d "${usrdir}"
  cp -a ${_gitname}-build "${usrdir}/${_gitname}"

  install -Dm755 $_gitname.bin "$pkgdir/usr/bin/$_gitname"
  install -Dm755 $_gitname.rc "$pkgdir/etc/rc.d/$_gitname"
  install -Dm644 $_gitname.logrotate "$pkgdir/etc/logrotate.d/$_gitname"
  install -Dm644 $_gitname.pam "$pkgdir/etc/pam.d/$_gitname"
  install -Dm644 $_gitname.bashrc "$usrdir/$_gitname/.bashrc"

# Set the DB in the environment
  sed -i "s/-DB-/$DB/g" "$usrdir/$_gitname/.bashrc"
  sed -i "s/-DB-/$_db_rc/g" "$pkgdir/etc/rc.d/$_gitname"

  install -Dm644 $_gitname-build/config/application.yml.example "$pkgdir/etc/webapps/$_gitname/application.yml"
  install -Dm644 $_gitname-build/config/database.yml.example "$pkgdir/etc/webapps/$_gitname/database.yml"
  install -Dm644 $_gitname-build/config/script_server.yml.example "$pkgdir/etc/webapps/$_gitname/script_server.yml"
  install -Dm644 $_gitname-build/config/oauth_keys.yml.example "$pkgdir/etc/webapps/$_gitname/oauth_keys.yml"

  # Include all gems
  cd "${usrdir}/${_gitname}"
  mkdir -p ${usrdir}/${_gitname}/vendor/ruby/1.9.1/
  ln -s ${srcdir}/gem-cache ${usrdir}/${_gitname}/vendor/cache
  ln -s ${srcdir}/gem-cache ${usrdir}/${_gitname}/vendor/ruby/1.9.1/cache

  export GEM_HOME="${PWD}/vendor"
  export GEM_PATH="${PWD}/vendor"
  export PATH="${GEM_PATH}/bin:${PATH}"

# TODO extract mysql2 version from Gemfile
  case $DB in
    mysql)
      gem install mysql2 -v "0.2.17" -- --with-mysql-config=/usr/bin/mysql_config

      ;;
    posgres)
      gem install pg -- --with-pg-config=/usr/bin/pg_config

      # Preconfigure right subsection in database.yml.example
      sed -e "s/<<: \*mysql/#<<: \*mysql/" \
          -e "s/#<<: \*postgres/<<: \*postgres/" \
          -i $_gitname-build/config/database.yml.example
      
      # Preconfigure right db in script_server.yml
      sed "s/db: \"mysql\"/db: \"postgres\"/" \
          -i $_gitname-build/config/script_server.yml.example
      
  esac

  gem install bundler
  bundle install --path ${GEM_PATH} --without development test heroku

  install -d $pkgdir/usr/share/licenses/$_gitname/
  install -Dm644 ${srcdir}/$_gitname-build/{AUTHORS,COPYRIGHT,GNU-AGPL-3.0} \
                 $pkgdir/usr/share/licenses/$_gitname/

  msg "Removing gem cache"
  rm -vr ${usrdir}/${_gitname}/vendor/{,ruby/1.9.1/}cache
}

package_diaspora-mysql-git() {
    pkgdesc="A privacy aware, personally controlled, do-it-all, open source social network (MySQL version)"
    provides=('diaspora-mysql' 'diaspora')
    depends=('ruby' 'redis' 'imagemagick' 'libxslt' 'net-tools' 'mysql' 'libmysqlclient')
    options=(!strip)

    export DB="mysql"
    export _db_rc="mysqld"

    _package
}

package_diaspora-postgresql-git() {
    pkgdesc="A privacy aware, personally controlled, do-it-all, open source social network (PostgreSQL version)"
    provides=('diaspora-postgresql' 'diaspora')
    depends=('ruby' 'redis' 'imagemagick' 'libxslt' 'net-tools' 'postgresql')
    options=(!strip)

    export DB="postgres"
    export _db_rc="postgresql"

    _package
}