--- /opt/dports/finance/bitcoin/Portfile	2016-03-03 22:24:21.000000000 +0100
+++ ./Portfile	2016-04-10 13:19:09.000000000 +0200
@@ -6,11 +6,11 @@
 name                bitcoin
 categories          finance crypto
 version             0.12.0
-revision            1
+revision                2
 platforms           darwin
 license             MIT
 maintainers         easieste yopmail.com:sami.laine openmaintainer
-description         client user interface for a peer-to-peer digital currency
+description             server daemon and client user interface for a peer-to-peer digital currency
 long_description    Bitcoin is a peer-to-peer digital currency. By peer-to-peer,  \
                     we mean that there is no central authority to issue \
                     new Bitcoins or keep track of transactions. Instead, these \
@@ -23,6 +23,8 @@
 checksums           rmd160  aa6a5601ee4e778a2e52cb16b79e5a5e89ca92bb \
                     sha256  0f1cda66c841a548a07cc37e80b0727354b1236d9f374c7d44362acdb85eb3e1
 
+patchfiles              patch-issue7845.diff
+
 depends_build       port:pkgconfig \
                     port:autoconf \
                     port:automake \
@@ -36,55 +38,131 @@
                     port:protobuf-cpp \
                     port:zmq
 
-use_parallel_build  no
+use_parallel_build      yes
 
 configure.args      --disable-ccache \
                     --disable-silent-rules
 
-# TODO: Restore GUI: needs to move to qt5, as it doesn't look like qt4
-# is going to get fixed.
-default_variants     +daemon
+configure.args-append   --with-daemon=no
+configure.args-append   --with-gui=no
+configure.args-append   --enable-wallet=no
+
+build.target            all
+
+test.run                yes
+test.target             check
+
+default_variants        +daemon +wallet
+
+set bitcoin_user        "bitcoin"
+set bitcoin_group       "${bitcoin_user}"
+set bitcoin_real_name   "Bitcoin Core"
+
+add_users               ${bitcoin_user} group=${bitcoin_group} realname="${bitcoin_real_name}"
+
+set daemon_path         "${prefix}/bin/bitcoind"
+set daemon_working_dir  "${prefix}/var/run/bitcoind/"
+set daemon_conf_file    "${prefix}/etc/bitcoin/bitcoin.conf"
+set daemon_data_dir     "${prefix}/var/lib/bitcoind/"
+set daemon_pid_file     "${prefix}/var/run/bitcoind/bitcoind.pid"
+
+set gui_app_name        "Bitcoin-Qt.app"
+set gui_app_path        "${applications_dir}/${gui_app_name}"
+set gui_conf_dir        "\$HOME/Library/Application Support/Bitcoin/"
+set gui_conf_file       "${gui_conf_dir}bitcoin.conf"
+set gui_data_dir        "${gui_conf_dir}"
+set gui_pid_file        "${gui_conf_dir}bitcoin.pid"
+
+set launchd_label               "org.macports.${name}"
+set launchd_plist_path          "${prefix}/etc/LaunchDaemons/${launchd_label}/${launchd_label}.plist"
+set launchd_plist_symlink_path  "/Library/LaunchDaemons/${launchd_label}.plist"
+
+variant daemon description {Build and install the daemon} {
+    configure.args-replace  --with-daemon=no    --with-daemon=yes
+}
 
-variant gui description {NOT WORKING Build with qt5} {
-    configure.cmd-append    --with-gui=qt5
+variant gui description {Build the Qt5 GUI} {
     depends_lib-append      port:qt5
+    configure.args-replace --with-gui=no --with-gui=qt5
+    build.target-replace all appbundle
 }
 
-variant daemon description {Build and install only the bitcoind daemon} {
-    configure.cmd-append    --with-gui=no
+variant wallet description {Build with support for wallet} {
+    configure.args-replace --enable-wallet=no --enable-wallet=yes
 }
 
-destroot {
-#    if {![variant_isset daemon]} {
-#        copy ${worksrcpath}/Bitcoin-Qt.app ${destroot}${applications_dir}
-#    }
-    xinstall -W ${worksrcpath}/src bitcoin-cli bitcoind ${destroot}${prefix}/bin
+post-destroot {
+    set config_args "-conf=\"${daemon_conf_file}\" -datadir=\"${daemon_data_dir}\" -pid=\"${daemon_pid_file}\""
+
+    if {[variant_isset daemon]} {
+        if { [tbool startupitem.install] } {
+            # Install launchd plist for daemon
+            xinstall -d [file dirname "${destroot}/${launchd_plist_path}"]
+            xinstall -m 644 "${filespath}/bitcoind.launchd.plist" "${destroot}/${launchd_plist_path}"
+            reinplace -E "
+                s|@@label@@|${launchd_label}|g;
+                s|@@user_name@@|${bitcoin_user}|g;
+                s|@@group_name@@|${bitcoin_group}|g;
+                s|@@program@@|\"${daemon_path}\" ${config_args}|g;
+                s|@@working_dir@@|${daemon_working_dir}|g;
+                " \
+                "${destroot}/${launchd_plist_path}"
+        }
+    }
+
+    if {[variant_isset gui]} {
+        # Install GUI app
+        copy "${worksrcpath}/${gui_app_name}" "${destroot}/${gui_app_path}"
+    }
+}
 
-    set docdir ${prefix}/share/doc/${name}
-    xinstall -d ${destroot}${docdir}
-    xinstall -m 444 -W ${worksrcpath} {*}[glob ${worksrcpath}/doc/*.md] ${destroot}${docdir}
+post-activate {
+    if {[variant_isset daemon]} {
+        xinstall -d [file dirname "${daemon_conf_file}"]
+        xinstall -d -o ${bitcoin_user} -g ${bitcoin_group} "${daemon_data_dir}"
+        xinstall -d -o ${bitcoin_user} -g ${bitcoin_group} [file dirname "${daemon_pid_file}"]
+
+        file mkdir -p [file dirname "${launchd_plist_symlink_path}"]
+        ln -sf "${launchd_plist_path}" "${launchd_plist_symlink_path}"
+    }
 }
 
-# TODO
-#
-## Automate creation of nonce RPC credentials
-## Add launchctl hooks for daemon
+pre-deactivate {
+    if {[variant_isset daemon]} {
+        file delete "${launchd_plist_symlink_path}"
+    }
+}
 
 notes "
 
-1.  Sanity in scripting:
+1.  A launch daemon entry has been created for ${name}. It is disabled by default. To enable and load the daemon:
+
+    cmd$ sudo launchctl load -w \"${launchd_plist_symlink_path}\"
+
+2.  Start the daemon manually in the background:
+
+    cmd$ ${daemon_path} -daemon
+
+3.  Symlink the GUI configuration into your home directory:
 
-    cmd$ ln -s \"~/Library/Application Support/Bitcoin\" ~/.bitcoin
+    cmd$ ln -s \"${gui_data_dir}\" ~/.bitcoin
 
-2.  View progress with:
+4.  View the configuration:
 
-    cmd$ tail -F ~/.bitcoin/debug.log
+    # Daemon:
+    cmd$ less \"${daemon_conf_file}\"
+    # GUI:
+    cmd$ less \"${gui_conf_file}\"
 
-3.  Edit ~/.bitcoin/bitcoin.conf with RPC credentials
+5.  View the log:
 
-4.  Start in background via:
+    # Daemon:
+    cmd$ tail -F \"${daemon_data_dir}debug.log\"
+    # GUI:
+    cmd$ tail -F \"${gui_data_dir}debug.log\"
     
-    cmd$ ${prefix}/sbin/bitcoind -daemon
+6.  Edit \"${daemon_conf_file}\" and \"${gui_conf_file}\" to include the RPC credentials.
+    See https://github.com/bitcoin/bitcoin/blob/master/doc/build-osx.md#running for more information.
 
 "
 
