#!/bin/bash

# This script should be run in the OSX subdirectory.
# It will then produce MacOS X Finder application diretory
# structure and make all the executables relocatable.

# Fink root
SW=/sw; export SW

# Location of Cocoadialog
CD=~/Applications; export CD

# Fix a given executable or library to be relocatable

fixlib () {

if [ ! -d $1 ]; then
  libs="`otool -L $1 | fgrep compatibility | cut -d\( -f1`"
  for lib in $libs; do
    base=`echo $lib | awk -F/ '{print $NF}'`
    first=`echo $lib | cut -d/ -f1-3`
    to=@executable_path/../lib/$base
    if [ $first != /usr/lib -a $first != /usr/X11R6 ]; then
      /usr/bin/install_name_tool -change $lib $to $1
      if [ "`echo $lib | fgrep libcrypto`" = "" ]; then
        /usr/bin/install_name_tool -id $to ../lib/$base
        for ll in $libs; do
          base=`echo $ll | awk -F/ '{print $NF}'`
          first=`echo $ll | cut -d/ -f1-3`
          to=@executable_path/../lib/$base
          if [ $first != /usr/lib -a $first != /usr/X11R6 -a "`echo $ll | fgrep libcrypto`" = "" ]; then
            /usr/bin/install_name_tool -change $ll $to ../lib/$base
          fi
        done
      fi
    fi
  done
fi
}

#
# First install everything in /tmp
# 
rm -rf /tmp/GNUnet
mkdir /tmp/GNUnet

cd ../..
# TODO: issues with gcrypt vs. openssl. The latter causes gnunetd to core dump
# in some cases. Do we still need to disable gettext?
# ./configure --with-gdbm --without-mysql --without-bdb --with-sqlite=$SW --with-crypto=$SW --disable-nls
./configure --with-gdbm --without-mysql --without-bdb --with-sqlite=$SW --disable-nls
make
make install prefix=/tmp/GNUnet
cd contrib/OSX

echo "Compilation done. Patching executables and libraries - wait -"

# TODO: set icon (SetFile?) - now must be done in Finder
rm -rf GNUnet.app
mkdir GNUnet.app
mkdir GNUnet.app/Contents
mkdir GNUnet.app/Contents/{MacOS,Resources}
mkdir GNUnet.app/Contents/MacOS/{bin,doc,etc,lib,man,hosts}
cp GNUnet.icns GNUnet.app/Contents/Resources
cp Info.plist GNUnet.app/Contents
cp GNUnet GNUnet-* GNUnet.app/Contents/MacOS
cp -r /tmp/GNUnet/man GNUnet.app/Contents/MacOS
cp /tmp/GNUnet/bin/* GNUnet.app/Contents/MacOS/bin
mv GNUnet.app/Contents/MacOS/bin/gnunet-gtk GNUnet.app/Contents/MacOS/bin/gnunet-gtk.wrap
mv GNUnet.app/Contents/MacOS/bin/gnunetd GNUnet.app/Contents/MacOS/bin/gnunetd.wrap
mv GNUnet.app/Contents/MacOS/bin/gnunet-peer-info GNUnet.app/Contents/MacOS/bin/gnunet-peer-info.wrap
cp {gnunet-gtk,gnunetd,gnunet-peer-info} GNUnet.app/Contents/MacOS/bin
cp /tmp/GNUnet/lib/* GNUnet.app/Contents/MacOS/lib
cp ../gnunet.root GNUnet.app/Contents/MacOS/etc/gnunet.root
cp ../gnunet.user GNUnet.app/Contents/MacOS/etc/gnunet.user
cp $SW/etc/pango/* GNUnet.app/Contents/MacOS/etc
pth=`fgrep ModulesPath $SW/etc/pango/pango.modules | cut -d\  -f4`
cp -r $pth GNUnet.app/Contents/MacOS/pango
cp ../../COPYING GNUnet.app/Contents/MacOS/doc
cp *.txt GNUnet.app/Contents/MacOS/doc
cp hosts/* GNUnet.app/Contents/MacOS/hosts > /dev/null 2>&1

# Include Cocoadialog 
cp -r $CD/CocoaDialog.app GNUnet.app/Contents/MacOS
cp GNUnet.icns GNUnet.app/Contents/MacOS/CocoaDialog.app/Contents/Resources/cocoaDialog.icns

# Find out libs we need from fink (e.g. $SW) - loop until no changes
a=1
nfiles=0
endl=true
while $endl; do
  echo "Looking for dependencies. Round " $a
  libs="`otool -L GNUnet.app/Contents/MacOS/{bin,lib}/* 2> /dev/null | fgrep compatibility | cut -d\( -f1 | grep $SW | sort | uniq`"
  cp -f $libs GNUnet.app/Contents/MacOS/lib
  let "a+=1"  
  nnfiles=`ls GNUnet.app/Contents/MacOS/lib | wc -l`
  if [ $nnfiles = $nfiles ]; then
    endl=false
  else
    nfiles=$nnfiles
  fi
done

# Libcrypto comes with the system + fink version does not allow install_name_tool
rm -f GNUnet.app/Contents/MacOS/lib/*.la GNUnet.app/Contents/MacOS/lib/libcrypto*

# Fix shared lib paths in executables and shared libs
cd GNUnet.app/Contents/MacOS/bin
for file in *; do
  fixlib $file
done
cd ../lib
for file in *; do
  fixlib $file
done
cd ../pango
for file in *.so; do
  fixlib $file
done
cd ../../../..

# create dmg image
/usr/bin/hdiutil create -srcfolder GNUnet.app GNUnet.dmg
echo "Done."
exit 0
