#!/bin/bash
# WINE Installation script
# Can do almost everything from compiling to configuring...
#
# Copyright 1999 Ove K�ven
#
# 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
#

#--- defaults (change these if you are a packager)
CONFARGS=""                   # configure args, e.g. --prefix=/usr

# functions

function std_sleep {
  sleep 1
}

function conf_yesno_answer {
  unset ANSWER
  while [ "$ANSWER" != 'yes' ] && [ "$ANSWER" != 'no' ]
  do {
    echo -n "$1"
    read ANSWER
  }
  done
}

# startup...

echo "Wine Installer v1.0"
echo

if ! [ -f configure ]
then
    if [ -f ../configure ]
    then {
      cd ..
    }
    else {
      echo "You're running this from the wrong directory."
      echo "Change to the Wine source's main directory and try again."
      exit 1
    }
    fi
fi

if [ `whoami` = 'root' ]
then
    echo "You are running wineinstall as root, this is not advisable. Please rerun as a user."
    echo "Aborting."
    exit 1
fi

if [ ! -w . ]
then
    echo "The source directory is not writable. You probably extracted the sources as root."
    echo "You should remove the source tree and extract it again as a normal user."
    exit 1
fi

# check whether RPM installed, and if it is, remove any old wine rpm.
hash rpm &>/dev/null
RET=$?
if [ $RET -eq 0 ]; then
    if [ -n "`rpm -qi wine 2>/dev/null|grep "^Name"`" ]; then
      echo "Warning: Old Wine RPM install detected. Do you want to remove it first?"
      conf_yesno_answer "(yes/no) "
      if [ "$ANSWER" = 'yes' ]; then
        echo "We need to remove the rpm as root, please enter your root password"
        echo
        echo Starting wine rpm removal...
        su -c "rpm -e wine; RET=$?"
        if [ $RET -eq 0 ]; then
          echo Done.
        else
          echo "FAILED. Probably you aren't installing as root."
          echo "Expect problems (library conflicts with existing install etc.)."
        fi
      else
        echo "Sorry, I won't install Wine when an rpm version is still installed."
        echo "(Wine support suffered from way too many conflicts between RPM"
        echo "and source installs)"
        echo "Have a nice day !"
        exit 1
      fi
    fi
fi

# check whether wine binary still available
if [ -n "`wine --version 2>/dev/null`" ]
then
    echo "Warning !! wine binary (still) found, which may indicate"
    echo "a (conflicting) previous installation."
    echo "You might want to abort and uninstall Wine first."
    echo "(If you previously tried to install from source manually, "
    echo "run 'make uninstall' from the wine root directory)"
    std_sleep
fi

# run the configure script, if necessary

if [ -f Makefile ]
then
    echo "I see that Wine has already been configured, so I'll skip that."
    std_sleep
else
    echo "Running configure..."
    echo
    if ! ./configure $CONFARGS
    then {
      echo
      echo "Configure failed, aborting install."
      exit 1
    }
    fi
fi

# now do the compilation and install, we need to always do this because we
# don't want the 'make install' command we might run to run 'make' as root

# ask the user if they want to build and install wine
echo
echo "We need to install wine as root user, do you want us to build wine,"
echo "'su root' and install Wine?  Enter 'no' to continue without installing"
conf_yesno_answer "(yes/no) "
ROOTINSTALL="$ANSWER"

if [ "$ROOTINSTALL" = "yes" ]
then sucommand="make install"
fi

echo
echo "Compiling Wine. Grab a lunch or two, rent a video, or whatever,"
echo "in the meantime..."
echo
std_sleep

# try to just make wine, if this fails 'make depend' and try to remake
if ! { make; }
then
    if ! { make depend && make; }
    then
      echo
      echo "Compilation failed, aborting install."
      exit 1
    fi
fi

if [ "$ROOTINSTALL" = "no" ]
then
    exit 0
fi

echo
echo "Performing 'make install' as root to install binaries, enter root password"

if ! su root -c "$sucommand"
then
    echo
    echo "Incorrect root password. If you are running Ubuntu or a similar distribution,"
    echo "'make install' needs to be run via the sudo wrapper, so trying that one now"
    if ! sudo su root -c "$sucommand"
    then
         echo
         echo "Either you entered an incorrect password or we failed to"
         echo "run '$sucommand' correctly."
         echo "If you didn't enter an incorrect password then please"
         echo "report this error and any steps to possibly reproduce it to"
         echo "http://bugs.winehq.org/"
         echo
         echo "Installation failed, aborting."
         exit 1
    fi
fi

# it's a wrap
echo
echo "Installation complete."
echo "If you have problems with Wine, please read the documentation first,"
echo "as many kinds of potential problems are explained there."

exit 0