#!/bin/sh

if [ "$1" = "-h" -o "$1" = "--help" -o "$1" = "-help" ]; then
  echo
  echo " Options: --prefix=/usr/local (default)"
  echo "          --prefix=/usr"
  echo
  echo "          --config=src/config.h (default)"
  echo '          --config=$HOME/.config/evilvte/config.h'
  echo
  exit
fi

rm -f src/config.o src/custom.h src/evilvte.h src/uninstall.sh

DO_SED=sed
DO_ECHO=echo
prefix="/usr/local"
CONF_FILE="src/config.h"

for opt do
  case "$opt" in
  --quiet) DO_SED=true
           DO_ECHO=true
  ;;
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --config=*) CONF_FILE=`echo $opt | cut -d '=' -f 2`
  ;;
  esac
done

if [ ! -e $CONF_FILE ] || [ -d $CONF_FILE ]; then
  $DO_ECHO
  $DO_ECHO "[1m[31mWARNING:[m parsing error: $CONF_FILE"
  HOME_ERR=`echo $CONF_FILE | grep '~'`
  if [ "$HOME_ERR" != "" ]; then
    $DO_ECHO '         Please do not use "~".'
  else
    $DO_ECHO "         Please make sure your configuration file is correct."
  fi
  $DO_ECHO
  exit
fi

PROG=`grep PROGRAM_NAME $CONF_FILE | grep -v ^\/\/ | awk '{print $3}'`
if [ "$PROG" = "" ]; then
  PROG=evilvte
fi

SUSE_LD=`ld -v 2>&1 | tr A-Z a-z | grep suse`

if [ "$SUSE_LD" != "" ]; then
  SUSE_DETECTED=TRUE
else
  SUSE_DETECTED=FALSE
fi

ARCH_UNAME=`uname -a | grep -- '-ARCH '`

if [ "$ARCH_UNAME" != "" ]; then
  ARCH_DETECTED=TRUE
else
  ARCH_DETECTED=FALSE
fi

GNU_GOLD=`ld -v | tr A-Z a-z | tr -s ' ' ' ' | grep 'gnu gold'`

if [ "$GNU_GOLD" != "" ]; then
  LDLIBS="-L/usr/local/lib -lvte -lgtk-x11-2.0 -lgdk-x11-2.0 -lgobject-2.0 -lglib-2.0"
else
  LDLIBS="-L/usr/local/lib -lvte"
fi

VTEINC=`pkg-config --cflags vte`
if [ $? != 0 ]; then
  exit
else
  $DO_ECHO
  $DO_ECHO Prefix: [1m[33m$prefix[m
  $DO_ECHO [1m[33mSUSE system detected[m $SUSE_DETECTED | sed 's/TRUE//' | grep -v FALSE$
  $DO_ECHO [1m[33mArch Linux detected[m $ARCH_DETECTED | sed 's/TRUE//' | grep -v FALSE$
  $DO_ECHO -n "Configuration: "
  DEFINE_LINES=`sed 's/\t/ /g' $CONF_FILE | tr -s ' ' ' ' | sed -e 's/^ //' -e 's~/\*~\n~g' | grep ^\#define | wc | awk '{print $1}'`
  if [ "$DEFINE_LINES" = "0" ]; then
    $DO_ECHO [1m[31mDisable everything.[m
  else
    $DO_ECHO
  fi
  $DO_SED 's/\t/ /g' $CONF_FILE | tr -s ' ' ' ' | sed 's/^ //' | grep ^\#define | sed 's~/\*~\n~g' | grep -v \*\/ | \
  sed -e 's/$/[m/' \
      -e 's/"/[1m[32m"/' \
      -e 's/ 0/[1m[32m 0/' \
      -e 's/ 1/[1m[32m 1/' \
      -e 's/ 2/[1m[32m 2/' \
      -e 's/ 3/[1m[32m 3/' \
      -e 's/ 4/[1m[32m 4/' \
      -e 's/ 5/[1m[32m 5/' \
      -e 's/ 6/[1m[32m 6/' \
      -e 's/ 7/[1m[32m 7/' \
      -e 's/ 8/[1m[32m 8/' \
      -e 's/ 9/[1m[32m 9/' \
      -e 's/TRUE/[1m[32mTRUE/' \
      -e 's/FALSE/[1m[31mFALSE/' \
      -e 's/ AUTO/[1m[32m AUTO/' \
      -e 's/ BACKSPACE/[1m[32m BACKSPACE/g' \
      -e 's/BACKSPACE_KEY/[mBACKSPACE_KEY/' \
      -e 's/ DELETE/[1m[32m DELETE/g' \
      -e 's/DELETE_KEY/[mDELETE_KEY/' \
      -e 's/ VTE_FIXED/[1m[32m VTE_FIXED/' \
      -e 's/ LINUX/[1m[32m LINUX/' \
      -e 's/ RXVT/[1m[32m RXVT/' \
      -e 's/ TANGO/[1m[32m TANGO/' \
      -e 's/ XTERM/[1m[32m XTERM/' \
      -e 's/ g_/[1m[32m g_/' \
      -e 's/ RIGHT/[1m[32m RIGHT/' \
      -e 's/ LEFT/[1m[32m LEFT/' \
      -e 's/ TOP/[1m[32m TOP/' \
      -e 's/ BOTTOM/[1m[32m BOTTOM/' \
      -e 's/ CTRL /[1m[32m CTRL /'
  $DO_ECHO
  $DO_ECHO Please edit [1m[33m$CONF_FILE[m to customize your options.
  $DO_ECHO
fi

bindir=$prefix/bin
mandir=$prefix/share/man/man1
deskdir=$prefix/share/applications
ICON_DIR=$prefix/share/pixmaps
GNOME_DEF_APP=$prefix/share/gnome-control-center/default-apps
EVILVTE_VERSION=`head -n 1 Changelog`

echo "VTEINC=$VTEINC" >> src/config.o
echo "prefix=$prefix" >> src/config.o
echo "bindir=\$(DESTDIR)$bindir" >> src/config.o
echo "mandir=\$(DESTDIR)$mandir" >> src/config.o
echo "deskdir=\$(DESTDIR)$deskdir" >> src/config.o
echo "ICON_DIR=$ICON_DIR" >> src/config.o
echo "ICON_DIR_INSTALL=\$(DESTDIR)$ICON_DIR" >> src/config.o
echo "GNOME_DEF_APP=\$(DESTDIR)$GNOME_DEF_APP" >> src/config.o
echo "LDLIBS=$LDLIBS" >> src/config.o
echo "PROG=$PROG" >> src/config.o
echo "EVILVTE_VERSION=$EVILVTE_VERSION" >> src/config.o
echo "SUSE_DETECTED=$SUSE_DETECTED" >> src/config.o
echo "ARCH_DETECTED=$ARCH_DETECTED" >> src/config.o
echo "CONF_FILE=$CONF_FILE" >> src/config.o
head -n 1 $0 > src/uninstall.sh
echo "echo rm -f $bindir/$PROG" >> src/uninstall.sh
echo "rm -f $bindir/$PROG" >> src/uninstall.sh
echo "echo rm -f $bindir/showvte" >> src/uninstall.sh
echo "rm -f $bindir/showvte" >> src/uninstall.sh
echo "echo rm -f $deskdir/evilvte.desktop" >> src/uninstall.sh
echo "rm -f $deskdir/evilvte.desktop" >> src/uninstall.sh
echo "echo rm -f $GNOME_DEF_APP/evilvte.xml" >> src/uninstall.sh
echo "rm -f $GNOME_DEF_APP/evilvte.xml" >> src/uninstall.sh
echo "echo rm -f $mandir/evilvte.1" >> src/uninstall.sh
echo "rm -f $mandir/evilvte.1" >> src/uninstall.sh
echo "echo rm -f $mandir/showvte.1" >> src/uninstall.sh
echo "rm -f $mandir/showvte.1" >> src/uninstall.sh
echo "echo rm -f $ICON_DIR/evilvte.png" >> src/uninstall.sh
echo "rm -f $ICON_DIR/evilvte.png" >> src/uninstall.sh
echo "echo rmdir $ICON_DIR" >> src/uninstall.sh
echo "rmdir $ICON_DIR" >> src/uninstall.sh
echo "echo rmdir $mandir" >> src/uninstall.sh
echo "rmdir $mandir" >> src/uninstall.sh
echo "echo rmdir $prefix/share/man" >> src/uninstall.sh
echo "rmdir $prefix/share/man" >> src/uninstall.sh
echo "echo rmdir $GNOME_DEF_APP" >> src/uninstall.sh
echo "rmdir $GNOME_DEF_APP" >> src/uninstall.sh
echo "echo rmdir $prefix/share/gnome-control-center" >> src/uninstall.sh
echo "rmdir $prefix/share/gnome-control-center" >> src/uninstall.sh
echo "echo rmdir $deskdir" >> src/uninstall.sh
echo "rmdir $deskdir" >> src/uninstall.sh
echo "echo rmdir $prefix/share" >> src/uninstall.sh
echo "rmdir $prefix/share" >> src/uninstall.sh
echo "echo rmdir $bindir" >> src/uninstall.sh
echo "rmdir $bindir" >> src/uninstall.sh
echo "echo rmdir $prefix" >> src/uninstall.sh
echo "rmdir $prefix" >> src/uninstall.sh
echo "true" >> src/uninstall.sh
