#!/bin/bash
# Make a mu2Linux/Slackware package out of the source

# Variables
DIRS="bin,lib/X11/alloywm-icons"
VERSION="0.4.0"
FILES=""
TYPE="mu2linux"

fail() {
  echo $1
  exit 1
}

copy_file() {
  echo -n "copying $1 to $2... "
  cp $1 $2 || (echo -n "Failed" && fail "Copying failed.")
  echo "OK"
}

prepare() {
  IFS=","
  for i in $DIRS
  do
    echo "creating $i/... "
    mkdir -p $TARGET/$i || (echo -n "Failed" && fail "Could not create directory $i.")
  done
  IFS=" "
  
  mkdir -p etc/mu2/deps etc/mu2/desc
  cp README etc/mu2/desc/mu2-alloywm-$VERSION.dsc
  echo "mu2-X11R6" > etc/mu2/deps/mu2-alloywm-$VERSION.dps
  FILES="etc/mu2/deps/mu2-alloywm-$VERSION.dps"
  FILES="$FILES etc/mu2/desc/mu2-alloywm-$VERSION.dsc"  

  copy_file src/alloywm $TARGET/bin
  copy_file scriptdir/x-panel $TARGET/bin
  copy_file scriptdir/mu2-alloywm $TARGET/bin 
  copy_file goodies/xaw-panel $TARGET/bin
  copy_file goodies/xaw-palette $TARGET/bin
  copy_file goodies/xaw-switch $TARGET/bin
  if [ "$GTKCONFIG" != "" ]
  then
    copy_file goodies/gtk-palette $TARGET/bin
    copy_file goodies/gtk-panel-menu $TARGET/bin
    copy_file goodies/gtk-switch $TARGET/bin
    copy_file goodies/gtk-switch-menu $TARGET/bin
  fi
  cp alloywm-icons/* $TARGET/lib/X11/alloywm-icons
  chmod a+rx $TARGET/lib/X11/alloywm-icons
  chmod a+r $TARGET/lib/X11/alloywm-icons/*
  chmod a+rx $TARGET/bin/*
}

build_pkg() {
  case $TYPE in
  mu2linux) # mu2Linux-named package
    tar -p -cO $FILES $TARGET/bin/* $TARGET/lib/X11/alloywm-icons | gzip > mu2-alloywm-$VERSION.tgz
    echo "mu2-alloywm-$VERSION.tgz has been created."
  ;;
  esac
}

cleanup() {
  case $TYPE in
  "mu2linux")
    rm -rf etc
  ;;
  esac
  
  IFS="/"
  set -- ${PREFIX#/}
  rm -rf $1
  
  rm make.log
  make clean > /dev/null || (echo -n "Failed" && fail "Cannot clean binaries.")
  make distclean > /dev/null || (echo -n "Failed" && fail "Cannot cleanup after configure.")
  rm Makefile src/Makefile goodies/Makefile || (echo -n "failed" && fail "Could not remove Makefiles.")
}

# Main
for i in $@
do
  case $i in
  "--mu2linux"|"") # Create mu2Linux package
    echo "building for target mu2Linux... "
    TYPE="mu2linux"
  ;;
  "--slackware") # Create Slackware package  
    echo "target 'Slackware' is not supported yet."
    exit 1
  ;;
  --prefix=*) # Run the configure script with the specified prefix
    echo "configuring... "
    ./configure $i || (echo -n "Failed" && fail "Configuration failed.  Read config.log for more info.")
  ;;
  esac
done  

test -f config.cache || fail "Please run the configure script before you begin."
. config.cache

TARGET=${PREFIX#/}

echo "building alloywm... " && gmake > make.log || (echo -n "Failed" && fail "Build failed.  Please check make.log for any make errors.")
echo "preparing... " && prepare || (echo -n "Failed" && fail "Preparation failed.")
echo "making package... " && build_pkg || (echo -n "Failed" && fail "Unable to build tarball.")
echo "cleaning up..." && cleanup || (echo -n "Failed" && fail "Cleanup failed.")

