#!/bin/sh

quiet=no
verbose=no
dryrun=no
skip=no

msg=""

prefix="/usr/local"
ghcRoot=""

set -e


###
### Decode arguments
###

usage() {
    echo "$0 [-q | -v] [-n] [-p dir] [-s] [ghc-root]"
    echo "    -q | --quiet      supress informational output"
    echo "    -v | --verbose    print actions as they are taken"
    echo "    -n | --dryrun     don't actually do anything (implies -v)"
    echo "    -p | --prefix     tree to sym-link executables, man pages, etc"
    echo "                      (defaults to /usr/local)"
    echo "    -s | --skip       skip linking (register packages only)"
    echo ""
    echo "    ghc-root    directory containing ghc tree"
}

originalArgs="$*"

while [ $# -gt 0 ]
do
    case "$1" in
        -q|--quiet)     quiet=yes ; verbose=no ;;
        -v|--verbose)   quiet=no ; verbose=yes ;;
        -n|--dryrun)    dryrun=yes ;;
        -p|--prefix)    prefix="$2" ; shift ;;
        -s|--skip)      skip=yes ;;
        -\?|--help)     usage ; exit 0 ;;
        -*)             echo "Unknown flag $1" ; usage ; exit 1 ;;
        *)  if [ -z "$ghcRoot" ] ; then
                ghcRoot="$1"
            else
                echo "Too many non-option arguments." ; usage ; exit 1
            fi
            ;;
    esac
    shift
done

if [ -z "$ghcRoot" ] ; then
    bindir=`dirname $0`
    ghcRoot=`dirname $bindir`
    if [ "$ghcRoot" = '.' ] ; then
        echo "Couldn't deduce location of ghc root." ; usage ; exit 1
        exit 1
    fi
fi

ghcRoot=${ghcRoot%/}

if [ -x "$ghcRoot"/lib/ghc-*/bin/ghc ] 2>/dev/null ; then
    :
else
    echo "$ghcRoot doesn't appear to be a ghc tree."
    exit 1
fi

if [ \! -d "$prefix/bin" ] ; then
    echo "$prefix doesn't appear to be a good prefix dir."
    echo "A prefix dir should have bin & share subdirs."
    echo "Common prefix dirs are /usr/local and /usr."
    exit 1
fi

###
### Root check
###

if [ "$dryrun" = "no" -a `id -u` -ne 0 ]
then
    echo "You must be root to activate a particular Haskell Platform."
    echo "Please rerun this command sudo:"
    echo "    sudo $0 $originalArgs"
    exit 1
fi


###
### Get ready to actually do stuff
###

if [ "$dryrun" = "yes" -a "$verbose" = "yes" ] ; then
    echo "Dry run mode enabled, would have run the following commands:"
fi

run() {
    if [ "$verbose" = "yes" ] ; then
        echo "    $*"
    fi
    if [ "$dryrun" = "no" ] ; then
        "$@"
    fi
}

symLinkInto() {
    directory="$1"
    shift

    # Make sure directory exists before creating symlinks in it.
    run mkdir -p "$directory"
    run ln -s --force --target-directory="$directory" "$@"
}

###
### Set up bindir links
###

if [ "$skip" = "no" ] ; then
    symLinkInto "$prefix/bin" "$ghcRoot"/bin/*
    symLinkInto "$prefix/share/man/man1" "$ghcRoot"/share/man/man1/*
    symLinkInto "$prefix/share/doc" "$ghcRoot"/share/doc/ghc
fi


###
### Register platform packages
###

for conf in "$ghcRoot"/etc/registrations/*
do
    run "$ghcRoot"/bin/ghc-pkg register --verbose=0 --force $conf 2>/dev/null
done


###
### Report
###

if [ "$quiet" = "no" ] ; then
    if [ "$dryrun" = "no" ] ; then
        tense=""
    else
        tense="would be "
    fi
    echo
    echo "Haskell ${tense}set to:"
    echo "    GHC         $ghcRoot"
    echo "    Haddocks    file://$ghcRoot/doc/frames.html"
    echo "    Other doc   file://$ghcRoot/share/doc/ghc/html/index.html"
    echo
    if [ "$skip" = "no" ] ; then
        echo "Symlinks for command line tools (ghc, cabal, etc..) ${tense}added to:"
        echo "    $prefix/bin"
        echo
    fi
fi
