#!/bin/sh
#
# This script generate some valid Slackware packages
#
#
# Some variables.
#
CWD=`pwd`
BUILD_NUM=1
SLCK=$CWD/slack
PREFIX=${PREFIX:-"/usr/local"} 
PKG=$CWD/slktmp 
TMPBUILD=$CWD/tmpbuild
#DOINSTDIR=/install

#
# Create post-install shell script.
#
do_doinst() {
  (mkdir -p install 
   cd install
  cat > doinst.sh <<EOF

SKINDIR=/usr/local/share/xine/skins
DESKDIR=/usr/local/share/xine/desktop
DATADIR=/usr/local/share
SYSCONFDIR=/usr/local/etc

## Clear old
DIR_HI=/opt/kde2/share/icons/hicolor
DIR_LO=/opt/kde2/share/icons/locolor
DIR_HI3=/opt/kde3/share/icons/hicolor
DIR_LO3=/opt/kde3/share/icons/locolor
# icon dimensions
HIRES='48x48 32x32 22x22'
LORES='32x32 22x22 16x16'
# files to remove
REMOVALS='\$DATADIR/icons/xine.xpm \$SYSCONFDIR/X11/wmconfig/xine \$SYSCONFDIR/X11/applnk/Multimedia/xine.desktop /opt/gnome/share/pixmaps/xine.xpm /opt/gnome/share/gnome/apps/Multimedia/xine.desktop \$DATADIR/gnome/apps/Multimedia/xine.desktop /opt/kde2/share/applnk/Multimedia/xine.desktop /opt/kde3/share/applnk/Multimedia/xine.desktop  \$DATADIR/applnk/Multimedia/xine.desktop /opt/kde/share/icons/xine.xpm /opt/kde/share/applnk/Multimedia/xine.desktop \$DATADIR/applnk/Multimedia/xine.desktop'
# remove files in \$REMOVALS
for remove in \$REMOVALS; do
  rm -f \$remove
done
# KDE2 icons
for hires in \$HIRES; do
  rm -f \$DIR_HI/\$hires/apps/xine.png
done
for lores in \$LORES; do
  rm -f \$DIR_LO/\$lores/apps/xine.png
done
# KDE3 icons
for hires in \$HIRES; do
  rm -f \$DIR_HI3/\$hires/apps/xine.png
done
for lores in \$LORES; do
  rm -f \$DIR_LO3/\$lores/apps/xine.png
done


#install new ones
if test -d \$SKINDIR/metal; then rm -rf \$SKINDIR/metal; fi
if test -d \$SKINDIR/default; then rm -rf \$SKINDIR/default && ln -s \$SKINDIR/xinetic \$SKINDIR/default; fi
if test ! -d \$DATADIR/icons; then
  /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DATADIR/icons
fi
/usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine.xpm \$DATADIR/icons/xine.xpm
if test ! -d \$DATADIR/pixmaps; then
  /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DATADIR/pixmaps
fi
if test -d \$SYSCONFDIR/X11/wmconfig; then
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.wmconfig \$SYSCONFDIR/X11/wmconfig/xine
fi
if test -d \$SYSCONFDIR/X11/applnk; then
  if test ! -d \$SYSCONFDIR/X11/applnk/Multimedia; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d \$SYSCONFDIR/X11/applnk/Multimedia
  fi
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop \$SYSCONFDIR/X11/applnk/Multimedia/xine.desktop
fi
# End Icons
# Gnome
if test -d /opt/gnome; then
  if test ! -d /opt/gnome/share/pixmaps; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/gnome/share/pixmaps
  fi
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine.xpm /opt/gnome/share/pixmaps/xine.xpm
  if test -d /opt/gnome/share/gnome/apps/Multimedia; then
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop /opt/gnome/share/gnome/apps/Multimedia/xine.desktop
  else
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/gnome/share/gnome/apps/Multimedia
     /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop /opt/gnome/share/gnome/apps/Multimedia/xine.desktop
  fi
else
  if test -d \$DATADIR/gnome/apps; then
    if test -d \$DATADIR/gnome/apps/Multimedia; then
      /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop \$DATADIR/gnome/apps/Multimedia/xine.desktop
    else
      /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DATADIR/gnome/apps/Multimedia
      /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop \$DATADIR/gnome/apps/Multimedia/xine.desktop
    fi
  fi
fi
# End Gnome
# Kde3
if test -d /opt/kde3/; then
  DIR_HI3=/opt/kde3/share/icons/hicolor
  DIR_LO3=/opt/kde3/share/icons/locolor
  HIRES3='48x48 32x32 22x22'
  LORES3='32x32 22x22 16x16'
  for hires in \$HIRES3; do
    /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DIR_HI3/\$hires/apps
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine_\$hires.png \$DIR_HI3/\$hires/apps/xine.png
  done
  for lores in \$LORES3; do
    /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DIR_LO3/\$lores/apps
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine_\$lores.png \$DIR_LO3/\$lores/apps/xine.png
  done
  if test ! -d /opt/kde3/share/applnk/Multimedia; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/kde3/share/applnk/Multimedia
  fi
  cat \$DESKDIR/xine.desktop | sed -e "s/Icon=xine.xpm/Icon=xine/g" -e "s/Exec=xine/Exec=xine %U/g" > \$DESKDIR/xine.desktop.kde
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop.kde /opt/kde3/share/applnk/Multimedia/xine.desktop
  rm \$DESKDIR/xine.desktop.kde
fi
# End Kde3
# Kde2
if test -d /opt/kde2/; then
  DIR_HI=/opt/kde2/share/icons/hicolor
  DIR_LO=/opt/kde2/share/icons/locolor
  HIRES='48x48 32x32 22x22'
  LORES='32x32 22x22 16x16'
  for hires in \$HIRES; do
    /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DIR_HI/\$hires/apps
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine_\$hires.png \$DIR_HI/\$hires/apps/xine.png
  done
  for lores in \$LORES; do
    /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DIR_LO/\$lores/apps
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine_\$lores.png \$DIR_LO/\$lores/apps/xine.png
  done
  if test ! -d /opt/kde2/share/applnk/Multimedia; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/kde2/share/applnk/Multimedia
  fi
  cat \$DESKDIR/xine.desktop | sed -e "s/Icon=xine.xpm/Icon=xine/g" -e "s/Exec=xine/Exec=xine %U/g" > \$DESKDIR/xine.desktop.kde
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop.kde /opt/kde2/share/applnk/Multimedia/xine.desktop
  rm \$DESKDIR/xine.desktop.kde
else
  if test -d \$DATADIR/applnk/Multimedia; then
    if test ! -d \$DATADIR/applnk/Multimedia; then
      /usr/bin/install -c -m 755 -o 0 -g 0 -d \$DATADIR/applnk/Multimedia
    fi
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop \$DATADIR/applnk/Multimedia/xine.desktop
  fi
fi
# End Kde2
# Kde
if test -d /opt/kde; then
  if test ! -d /opt/kde/share/icons; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/kde/share/icons
  fi
  if test ! -d /opt/kde/share/applnk/Multimedia; then
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /opt/kde/share/applnk/Multimedia
  fi
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DATADIR/pixmaps/xine.xpm /opt/kde/share/icons/xine.xpm
  /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop /opt/kde/share/applnk/Multimedia/xine.desktop
else
  if test -d \$DATADIR/applnk/Multimedia; then
    /usr/bin/install -c -m 644 -o 0 -g 0 \$DESKDIR/xine.desktop \$DATADIR/applnk/Multimedia/xine.desktop
  fi
fi
# End Kde
EOF
)
}

#
# Create package description for pkgtool.
#
do_descr() {
  (mkdir -p install 
   cd install
  cat > slack-desc <<EOF
xine-ui: xine-ui 0.99.14.
xine-ui:
xine-ui: xine is a fully-featured free audio/video player for unix-like 
xine-ui: systems which uses libxine for audio/video decoding and playback.
xine-ui: For more informations on what formats are supported, please 
xine-ui: refer to the libxine documentation.
xine-ui:
xine-ui:
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 -xf $CWD/xine-ui-0.99.14.tar.bz2 && \
  cd xine-ui-0.99.14 && \
  DIE=1 && \
  echo "./configure --prefix=$PREFIX $CONFIG_OPT && make && DESTDIR=$PKG make install-strip "  && \
  ./configure --prefix=$PREFIX $CONFIG_OPT && make && DESTDIR=$PKG make install-strip && \
  cd $PKG && \
  do_doinst && do_descr && export _PACKAGE=xine-ui-0.99.14-$BUILD_ARCH-$BUILD_NUM.tgz \
  echo "n" | /sbin/makepkg $_PACKAGE && \
  mv $_PACKAGE $SLCK && export _PACKAGE="" && \
  cd $SLCK && DIE=0
}

#
# Cleaning building directory
#
do_clean() {
  rm -rf $TMPBUILD
  rm -f xine-ui*.tgz
  rm -rf $PKG
  cd $CWD
}

#
# Build for PPro
# 
build_pentiumpro() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.99.14"
  echo 
  echo "current architecture:pentiumpro"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export BUILD_ARCH=i686
  export XINE_BUILD=$BUILD_ARCH-pc-linux-gnu
  do_build
  do_clean
}

#
# Build for Pentium
#
build_pentium() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.99.14"
  echo 
  echo "current architecture:pentium"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export BUILD_ARCH=i586
  export XINE_BUILD=$BUILD_ARCH-pc-linux-gnu
  do_build
  do_clean
}

#
# Build for K6
#
build_k6() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.99.14"
  echo 
  echo "current architecture:k6"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export BUILD_ARCH=k6
  export XINE_BUILD=$BUILD_ARCH-pc-linux-gnu
  do_build
  do_clean
}

#
# Build for K7
#
build_k7() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.99.14"
  echo 
  echo "current architecture:k7"
  echo "slackware package will be copied to ./slack directory"
  echo
  echo "*****************************************************"
  rm -rf $PKG
  export BUILD_ARCH=athlon
  export XINE_BUILD=$BUILD_ARCH-pc-linux-gnu
  do_build
  do_clean
}

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


#
# Handle arguments if available.
#
build_arch() {
      rm -rf $SLCK
      mkdir -p $SLCK
      rm -f config.cache && ./autogen.sh && make dist
      $barch
      cp -f $CWD/xine-ui-0.99.14.tar.bz2 $SLCK
}

if [ "`id -u`" -ne 0 ]; then
    echo "You should be root to create packages. Exiting."
    exit 1
fi

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
