#!/bin/sh

update_cabal_config() {
    cabal_dir=~/.cabal
    cabal_config=${cabal_dir}/config
    cabal_config_new=${cabal_config}.platform
    cabal_config_newer=`find ${cabal_config_new} -newer $0 2>/dev/null`
    
    user_haskell_dir=~/Library/Haskell
    
    if [ -z "${cabal_config_newer}" ]
    then
        mkdir -m 755 ${cabal_dir} &>/dev/null

        cat > ${cabal_config_new} <<CONFIG_END
-- This is the configuration file for the 'cabal' command line tool.

-- The available configuration options are listed below.
-- Some of them have default values listed.

-- Lines (like this one) beginning with '--' are comments.
-- Be careful with spaces and indentation because they are
-- used to indicate layout for nested sections.

-- This configuration follows the layout of Haskell Platform on Mac OS X,
-- placing all installed package parts in the directories
--   /Library/Haskell for packages installed --global
--   ~/Library/Haskell for packages installed --user (the default)

-- === Built executables will be installed in:
--     ~/Library/Haskell/bin
-- 
-- You may wish to place this on your PATH by adding the following
-- line to your ~/.bash_profile:
--     export PATH="\$HOME/Library/Haskell/bin:\$PATH"

-- === When documentation is built, a master index to all documentation
-- will be placed in:
--     ~/Library/Haskell/doc/index.html
-- 
-- You may wish to bookmark that file


remote-repo: hackage.haskell.org:http://hackage.haskell.org/packages/archive
remote-repo-cache: ${user_haskell_dir}/repo-cache
-- local-repo:
-- logs-dir:
world-file: ${user_haskell_dir}/logs/world
-- verbose: 1
-- compiler: ghc
-- with-compiler:
-- with-hc-pkg:
-- scratchdir:
-- program-prefix: 
-- program-suffix: 
-- library-vanilla: True
library-profiling: True
-- shared: False
-- executable-profiling: False
-- optimization: True
-- library-for-ghci: True
-- split-objs: False
-- executable-stripping: True
-- user-install: True
-- package-db:
-- flags:
-- extra-include-dirs:
-- extra-lib-dirs:
-- constraint:
-- tests: False
-- cabal-lib-version:
-- preference:
documentation: True
doc-index-file: ${user_haskell_dir}/doc/index.html
-- upgrade-dependencies:
-- only-dependencies:
root-cmd: sudo
symlink-bindir: ${user_haskell_dir}/bin
build-summary: ${user_haskell_dir}/logs/build.log
-- build-log:
remote-build-reporting: anonymous
-- one-shot:
-- username:
-- password:
-- username:
-- password:

install-dirs user
  prefix: ${user_haskell_dir}/\$compiler/lib/\$pkgid
  -- bindir: \$prefix/bin
  -- libdir: \$prefix/lib
  libsubdir:  
  -- libexecdir: \$prefix/libexec
  -- datadir: \$prefix/share
  datasubdir: 
  docdir: \$prefix/doc
  -- htmldir: \$docdir/html
  -- haddockdir: \$htmldir

install-dirs global
  prefix: /Library/Haskell/\$compiler/lib/\$pkgid
  -- bindir: \$prefix/bin
  -- libdir: \$prefix/lib
  libsubdir:  
  -- libexecdir: \$prefix/libexec
  -- datadir: \$prefix/share
  datasubdir: 
  docdir: \$prefix/doc
  -- htmldir: \$docdir/html
  -- haddockdir: \$htmldir

CONFIG_END
    fi

    if [ \! -e ${cabal_config} ]
    then
        mkdir -m 755 ${user_haskell_dir} &>/dev/null
        mkdir -m 755 ${user_haskell_dir}/bin &>/dev/null
        mkdir -m 755 ${user_haskell_dir}/logs &>/dev/null
        mkdir -m 755 ${user_haskell_dir}/repo-cache &>/dev/null
        
        cp ${cabal_config_new} ${cabal_config}
        cat <<NOTICE_END

**********************************************************************

=== Configuration for cabal has been written to
    ${cabal_config}

=== Executables will be installed in:
    ${user_haskell_dir}/bin

    You may wish to place this on your PATH by adding the following
    line to your ~/.bash_profile:
    
    export PATH="\$HOME/Library/Haskell/bin:\$PATH"

=== When documentation is built, a master index to all documentation
    will be placed in:
    
    ${user_haskell_dir}/doc/index.html

    You may wish to bookmark that file once it gets built (after the
    first cabal install).

**********************************************************************

NOTICE_END
        /Library/Haskell/bin/cabal.real update  
    fi
}

update_cabal_config
exec -a cabal /Library/Haskell/bin/cabal.real "$@"

