#!/bin/sh
#
# This script generate some valid Slackware packages
#
#
# Some variables.
#
OLDCWD=`pwd`

CWD=`pwd`
PACKAGE=xine-ui.tgz
SLCK=$CWD/slack
PREFIX=/usr/local
PKG=$CWD/slktmp
TMPBUILD=$CWD/tmpbuild

#
# Create post-install shell script.
#
do_doinst() {
  (mkdir install 
   cd install
  cat > doinst.sh <<EOF
if [ -d /usr/local/share/xine/skins/default ]; then
  echo "Removing old <default> skin, <xinetic> is the new default skin."
  rm -rf /usr/local/share/xine/skins/default && \
  ln -s /usr/local/share/xine/skins/xinetic /usr/local/share/xine/skins/default;
fi
/sbin/ldconfig
EOF
)
}

#
# Create package description for pkgtool.
#
do_descr() {
cat > package_descriptions << EOF
xine-ui: xine-ui 0.9.0.
xine-ui:
xine-ui: xine is a free gpl-licensed video player for unix-like systems.
xine-ui: We support mpeg-2 and mpeg-1 system (audio + video multiplexed) streams,
xine-ui: eventually mpeg-4 and other formats might be added.

xine-ui: xine plays the video and audio data of mpeg-2 videos and synchronizes
xine-ui: the playback of both. Depending on the properties of the mpeg stream,
xine-ui: playback will need more or less processor power, 100% frame rate
xine-ui: has been seen on a 400 MHz P II system.
EOF
}

#
# Building binaries process, then install and move package in right place
#
do_build() {
  cd $CWD
  rm -rf $TMPBUILD
  mkdir -p $TMPBUILD
  cd $TMPBUILD && tar -xzf $CWD/xine-ui-0.9.0.tar.gz
  cd xine-ui-0.9.0
  DIE=1
  ./configure --prefix=$PREFIX && make && make install-strip prefix=$PKG/$PREFIX && \
  cd $PKG && \
  do_doinst && \
  echo "n" | makepkg $PACKAGE && \
  mv $PACKAGE $SLCK && \
  cd $SLCK && DIE=0
  do_descr
}

#
# Cleaning building directory
#
do_clean() {
  rm -rf $TMPBUILD
  rm -f $PACKAGE package_descriptions
  rm -rf $PKG
  cd $CWD
}

#
# Build for PPro
# 
build_pentiumpro() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.0"
  echo 
  echo "current architecture:pentiumpro"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export XINE_BUILD=i686-pc-linux-gnu
  do_build
  if test "$DIE" -eq 0; then 
    tar -czvf xine-ui-0.9.0-i686.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for Pentium
#
build_pentium() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.0"
  echo 
  echo "current architecture:pentium"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export XINE_BUILD=i586-pc-linux-gnu
  do_build
  if test "$DIE" -eq 0; then 
    tar -czvf xine-ui-0.9.0-i586.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for K6
#
build_k6() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.0"
  echo 
  echo "current architecture:k6"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export XINE_BUILD=k6-pc-linux-gnu
  do_build
  if test "$DIE" -eq 0; then 
    tar -czvf xine-ui-0.9.0-k6.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for K7
#
build_k7() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.0"
  echo 
  echo "current architecture:k7"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export XINE_BUILD=athlon-pc-linux-gnu
  do_build
  if test "$DIE" -eq 0; then 
    tar -czvf xine-ui-0.9.0-k7.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Main function
#
main() {
  rm -rf $SLCK
  mkdir -p $SLCK
  rm -f config.cache && ./cvscompile.sh && make dist
  build_pentiumpro
  build_pentium
  build_k6
  build_k7
  mv -f $CWD/xine-ui-0.9.0.tar.gz $SLCK
}


#
# Handle arguments if available.
#
build_arch() {
      rm -rf $SLCK
      mkdir -p $SLCK
      rm -f config.cache && ./cvscompile.sh && make dist
      $barch
      mv -f $CWD/xine-ui-0.9.0.tar.gz $SLCK
}
case "$1" in
    pentiumpro | ppro | i686 | 686)
      barch=build_pentiumpro
      build_arch
    ;;
    pentium | i586 | 586)
      barch=build_pentium
      build_arch
    ;;
    k6)
      barch=build_k6
      build_arch
    ;;
    k7 | athlon)
      barch=build_k7
      build_arch
    ;;
    *)
      main
    ;;
esac

cd $OLDCWD
