#!/bin/sh
#
# Script to create the initial WINEPREFIX directory
#
# Copyright 1999 Ove Kåven
# Copyright 2004 Chris Morgan
# Copyright 2004 Alexandre Julliard
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#

usage()
{
    echo "Usage: $0 [options]"
    echo ""
    echo "Options:"
    echo "  -h, --help                 Display this message"
    echo "      --prefix <dir>         Directory to create (default: \$WINEPREFIX or ~/.wine)"
    echo "  -q, --quiet                Don't print status messages"
    echo "  -w, --wait                 Wait for the wineserver to exit before returning"
    echo ""
}

set -e

bindir=""
case "$0" in
  */*)
    # $0 contains a path, use it
    bindir=`dirname "$0"`
    ;;
  *)
    # no directory in $0, search in PATH
    saved_ifs=$IFS
    IFS=:
    for d in $PATH
    do
      IFS=$saved_ifs
      if [ -x "$d/$0" ]
      then
        bindir="$d"
        break
      fi
    done
    ;;
esac

bindir=`cd "$bindir" && pwd`
dlldir="$bindir/../lib/wine"
datadir="$bindir/../share/wine"

do_wait=0
quiet=0

if [ ! -f "$dlldir/ntdll.dll.so" -a \
     -x "$bindir/../server/wineserver" -a \
     -f "$bindir/../dlls/ntdll/ntdll.dll.so" ]
then
    # running from the wine source tree
    topdir=`cd "$bindir/.." && pwd`
    WINELOADER="$topdir/wine"
    WINESERVER="$topdir/server/wineserver"
    if [ -n "$LD_LIBRARY_PATH" ]
    then
        LD_LIBRARY_PATH="$topdir/libs:$LD_LIBRARY_PATH"
    else
        LD_LIBRARY_PATH="$topdir/libs"
    fi
    export LD_LIBRARY_PATH

    # find the source directory
    link=`readlink "$WINELOADER"`
    if [ -z "$link" ]
    then
        topsrcdir="$topdir"
    else
        link=`dirname "$link"`
        case "$link" in
            /*) topsrcdir=`cd "$link/.." && pwd` ;;
             *) topsrcdir=`cd "$topdir/$link/.." && pwd` ;;
        esac
    fi
    dlldir="$topdir/programs"
    datadir="$topsrcdir/tools"
fi

while [ $# -gt 0 ]
do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        --prefix)
            WINEPREFIX="$2"
            shift 2
            ;;
        -q|--quiet)
            quiet=1
            shift
            ;;
        -w|--wait)
            do_wait=1
            shift
            ;;
        --use-wine-tree)
            # ignored, autodetected now
            shift 2
            ;;
        *)
            echo "Unknown option $1"
            usage
            exit 1
            ;;
  esac
done

WINEPREFIX="${WINEPREFIX:-$HOME/.wine}"

if [ -d "$WINEPREFIX" ] || mkdir "$WINEPREFIX"; then :
else
    echo "Could not create $WINEPREFIX, aborting"
    exit 1
fi

WINEPREFIX=`cd "$WINEPREFIX" && pwd`

# Create the drive symlinks

if [ ! -d "$WINEPREFIX/dosdevices" ]
then
    mkdir "$WINEPREFIX/dosdevices"
    [ -d "$WINEPREFIX/drive_c" ] || mkdir "$WINEPREFIX/drive_c"
    ln -s "../drive_c" "$WINEPREFIX/dosdevices/c:"
    ln -s "/" "$WINEPREFIX/dosdevices/z:"
fi

CROOT="$WINEPREFIX/dosdevices/c:"

# Create the directory tree

for i in \
    "$CROOT/windows" \
    "$CROOT/windows/command" \
    "$CROOT/windows/fonts" \
    "$CROOT/windows/inf" \
    "$CROOT/windows/system" \
    "$CROOT/windows/system32" \
    "$CROOT/windows/system32/drivers" \
    "$CROOT/windows/temp"
do
    [ -d "$i" ] || mkdir "$i"
done

# Copy the .inf script and run it

cp "$datadir/wine.inf" "$CROOT/windows/inf/wine.inf"
export WINEPREFIX
"${WINELOADER:-$bindir/wine}" rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 wine.inf

# Wait for the wineserver to finish

if [ $do_wait = 1 ]
then
    "${WINESERVER:-$bindir/wineserver}" -w
fi

if [ $quiet = 0 ]
then
    echo "$WINEPREFIX updated successfully."
fi
