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

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

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

# Icons
if test ! -d /usr/share/icons; then 
  /usr/bin/install -c -m 755 -o 0 -g 0 -d /usr/share/icons
fi
/usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.xpm /usr/share/icons/xine.xpm
if test ! -d /usr/share/pixmaps; then 
  /usr/bin/install -c -m 755 -o 0 -g 0 -d /usr/share/pixmaps
fi
/usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.xpm /usr/share/pixmaps/xine.xpm
if test -d /etc/X11/wmconfig; then 
  /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.wmconfig /etc/X11/wmconfig/xine
fi
if test -d /etc/X11/applnk; then
  if test ! -d /etc/X11/applnk/Multimedia; then 
    /usr/bin/install -c -m 755 -o 0 -g 0 -d /etc/X11/applnk/Multimedia
  fi
  /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /etc/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 /home/guenter/usr/x86//share/xine/desktop/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 /home/guenter/usr/x86//share/xine/desktop/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 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /opt/gnome/share/gnome/apps/Multimedia/xine.desktop
  fi
else
  if test -d /usr/share/gnome/apps; then
    if test -d /usr/share/gnome/apps/Multimedia; then
      /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /usr/share/gnome/apps/Multimedia/xine.desktop
    else
      /usr/bin/install -c -m 755 -o 0 -g 0 -d /usr/share/gnome/apps/Multimedia
      /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /usr/share/gnome/apps/Multimedia/xine.desktop
    fi
  fi
fi
# End Gnome
# 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 /home/guenter/usr/x86//share/xine/desktop/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 /home/guenter/usr/x86//share/xine/desktop/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
  /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /opt/kde2/share/applnk/Multimedia/xine.desktop
else
  if test -d /usr/share/applnk/Multimedia; then
    if test ! -d /usr/share/applnk/Multimedia; then 
      /usr/bin/install -c -m 755 -o 0 -g 0 -d /usr/share/applnk/Multimedia
    fi
    /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /usr/share/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 /home/guenter/usr/x86//share/xine/desktop/xine.xpm /opt/kde/share/icons/xine.xpm
  /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /opt/kde/share/applnk/Multimedia/xine.desktop
else
  if test -d /usr/share/applnk/Multimedia; then 
    /usr/bin/install -c -m 644 -o 0 -g 0 /home/guenter/usr/x86//share/xine/desktop/xine.desktop /usr/share/applnk/Multimedia/xine.desktop
  fi
fi

/sbin/ldconfig
EOF
)
}

#
# Create package description for pkgtool.
#
do_descr() {
cat > package_descriptions << EOF
xine-ui: xine-ui 0.9.8.
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() {
  echo "cd to $CWD" && cd $CWD && \
  echo "rm -rf $TMPBUILD" && rm -rf $TMPBUILD && \
  echo "mkdir -p $TMPBUILD" && mkdir -p $TMPBUILD && \
  echo "cd $TMPBUILD" && cd $TMPBUILD && \
  echo "tar -xzf $CWD/xine-ui-0.9.8.tar.gz" && tar -xzf $CWD/xine-ui-0.9.8.tar.gz && \
  echo "cd xine-ui-0.9.8" && cd xine-ui-0.9.8 && \
  DIE=1 && echo "./configure --prefix=$PREFIX && make && mkdir -p $PKG && make install-strip DESTDIR=$PKG" && \
  ./configure --prefix=$PREFIX && make && mkdir -p $PKG && make install-strip DESTDIR=$PKG && \
  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.8"
  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.8-i686.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for Pentium
#
build_pentium() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.8"
  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.8-i586.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for K6
#
build_k6() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.8"
  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.8-k6.tar.gz $PACKAGE package_descriptions
  fi
  do_clean
}

#
# Build for K7
#
build_k7() {
  echo "*****************************************************"
  echo
  echo "building slack for xine-ui 0.9.8"
  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.8-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.8.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.8.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
