#!/bin/sh
#Adam Schreiber <sadam@clemson.edu>

#This script takes portions from Patrick Volkerding's makepkg script here is
#the required copright notice for that code:

# Copyright 1994, 1998  Patrick Volkerding, Moorhead, Minnesota USA 
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Wed Mar 18 15:32:33 CST 1998
# Patched to avoid possible symlink attacks in /tmp.

NAME=Wine
LCNAME=wine
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-$NAME
ARCH=i686

# Change these as required. The first is the main source code.
VERSION=20030508
# This is the latest patch version in the patches/ directory.
PATCHVERSION=20030508

# If a patch version is not set, make the version the same as
# the main source code tarball.
[ ! $PATCHVERSION ] && PATCHVERSION == $VERSION 2>/dev/null

# Create the build directory if it doesn't exist.
[ ! -d $PKG ] && mkdir -p $PKG

(
cd $PKG

# Explode the controller tarball, if it exists.
[ -e $CWD/_$NAME.tar.gz ] && explodepkg $CWD/_$NAME.tar.gz

echo "+=======+=======+"
echo "| $NAME-$PATCHVERSION |"
echo "+========+======+"

cd $TMP

# Explode the main source code tarball.
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $LCNAME-$VERSION

# Sequentially apply any patches in the patches/ directory
for PATCH in `ls $CWD/patches/*` ; do
  zcat $PATCH | patch -p1
done

# Build the package:
./configure --prefix=/usr --enable-opengl
make depend && make
make install prefix=$PKG/usr

mkdir -p        $PKG/usr/doc/$NAME-$PATCHVERSION $PKG/install

# Copy the Documentation

cp -R documentation/* $PKG/usr/doc/$NAME-$PATCHVERSION/
cp README $PKG/usr/doc/$NAME-$PATCHVERSION/


#Create the slack-desc file
cat > $PKG/install/slack-desc <<EOF
Wine: Wine (Client, server, and winelib)
Wine:
Wine: Wine Is Not an Emulator, it is an alternative implementation of the
Wine: Windows 3.x and Win32 APIs. Wine provides both a development toolkit
Wine: (Winelib) for porting legacy Windows sources to Unix and a program
Wine: loader, allowing unmodified Windows 3.1/95/NT binaries to run under
Wine: Intel Unixes. Please, PLEASE read the README or else Wine probably
Wine: won't work for you.
Wine:
Wine:
Wine:
EOF

cd $PKG

# Change VERSION to PATCHVERSION to account for any patching up to a
# later release.
#tar czvf $TMP/$NAME-$PATCHVERSION.tgz .


#insert appropriate makepkg code here

make_install_script() {
  COUNT=1
  LINE="`sed -n "$COUNT p" $1`"
  while [ ! "$LINE" = "" ]; do
   LINKGOESIN="`echo "$LINE" | cut -f 1 -d " "`" 
   LINKGOESIN="`dirname $LINKGOESIN`" 
   LINKNAMEIS="`echo "$LINE" | cut -f 1 -d ' '`"
   LINKNAMEIS="`basename "$LINKNAMEIS"`"
   LINKPOINTSTO="`echo "$LINE" | cut -f 3 -d ' '`"
   echo "( cd $LINKGOESIN ; rm -rf $LINKNAMEIS )"
   echo "( cd $LINKGOESIN ; ln -sf $LINKPOINTSTO $LINKNAMEIS )"
   COUNT=`expr $COUNT + 1`
   LINE="`sed -n "$COUNT p" $1`"
  done
}

LINKADD=y
CHOWN=y
PACKAGE_NAME=$NAME-$PATCHVERSION-$ARCH-S9.tgz
TARGET_NAME="`dirname $PACKAGE_NAME`"
PACKAGE_NAME="`basename $PACKAGE_NAME`"
TAR_NAME="`basename $PACKAGE_NAME .tgz`"
echo
echo "Searching for symbolic links:"
# Get rid of possible pre-existing trouble:
INST=`mktemp $TMP/makepkg.XXXXXX`
find . -type l -exec ls -l {} \; | while read foo ; do echo $foo ; done | cut -f 9- -d ' ' | cut -b3- | tee $INST
if [ ! "`cat $INST`" = "" ]; then
  echo
  echo "Making symbolic link creation script:"
  make_install_script $INST | tee doinst.sh
fi
echo
if [ ! "`cat $INST`" = "" ]; then
  if [ -r install/doinst.sh ]; then
    echo "Unless your existing installation script already comtains the code"
    echo "to create these links, you should append these lines to your existing"
    echo "install script. Now's your chance. :^)"
    echo
    echo "Would you like to add this stuff to the existing install script and"
    echo -n "remove the symbolic links ([y]es, [n]o)? "
  else
    echo "It is recommended that you make these lines your new installation script."
    echo
    echo "Would you like to make this stuff the install script for this package"
    echo -n "and remove the symbolic links ([y]es, [n]o)? "
  fi
  if [ ! "$LINKADD" ]; then
    read LINKADD;
    echo
  else
    echo $LINKADD
    echo
  fi
  if [ "$LINKADD" = "y" ]; then
    if [ -r install/doinst.sh ]; then
      UPDATE="t"
      cat doinst.sh >> install/doinst.sh
    else
      mkdir install
      cat doinst.sh > install/doinst.sh
    fi
    echo
    echo "Removing symbolic links:"
    find . -type l -exec rm -v {} \;
    echo
    if [ "$UPDATE" = "t" ]; then
      echo "Updating your ./install/doinst.sh..."
    else
      echo "Creating your new ./install/doinst.sh..."
    fi
  fi
else
  echo "No symbolic links were found, so we won't make an installation script."
  echo "You can make your own later in ./install/doinst.sh and rebuild the"
  echo "package if you like."
fi
rm -f doinst.sh $INST
echo
echo "This next step is optional - you can set the directories in your package"
echo "to some sane permissions. If any of the directories in your package have"
echo "special permissions, then DO NOT reset them here!"
echo 
echo "Would you like to reset all directory permissions to 755 (drwxr-xr-x) and"
echo -n "directory ownerships to root.root ([y]es, [n]o)? "
if [ ! "$CHOWN" ]; then
  read CHOWN;
  echo
else
  echo $CHOWN
  echo
fi
if [ "$CHOWN" = "y" ]; then
  find . -type d -exec chmod -v 755 {} \; 
  find . -type d -exec chown -v root.root {} \;
fi
echo
echo "Creating tar file $TAR_NAME.tar..."
echo
/bin/tar  cvf $TAR_NAME.tar .
# Warn of zero-length files:
find . -type f -size 0c | while read file ; do
  echo "WARNING: zero length file $file"
done
find . -type f -name '*.gz' -size 20c | while read file ; do
  echo "WARNING: possible empty gzipped file $file"
done
echo
echo "Gzipping $TAR_NAME.tar..."
gzip -9 $TAR_NAME.tar
echo
echo "Renaming $TAR_NAME.tar.gz to $PACKAGE_NAME..."
mv $TAR_NAME.tar.gz $PACKAGE_NAME
if [ ! "$TARGET_NAME" = "." ]; then
  echo
  echo "Moving $PACKAGE_NAME to $TARGET_NAME..."
  mv $PACKAGE_NAME $TARGET_NAME
fi
echo
echo "Package creation complete."
echo

# Move the completed package into this directory
if [ "$1" != "--nomove" -a "$2" != "--nomove" ]; then
  mkdir -p $CWD/package
  mv $TMP/package-$NAME/*.tgz $CWD/package/ 2>/dev/null
fi

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-{$VERSION,$PATCHVERSION}
  rm -rf $PKG
fi

# Fork screen output to a log
) 2>&1 | tee $CWD/$NAME.build.log
