#!/bin/sh
#  Build and install script for end users or package makers.
#  Execute in directory where the tar file was unpacked.
#  Prerequisites for build: g++ and libgtk2.0-dev
#
#  Usage:
#     $ ./build build         # build only
#     $ ./build install       # install only (after build)
#     $ ./build [ both ]      # do both build and install
#
#  If $PREFIX is defined beforehand then application files will
#  go automatically to standard locations under $PREFIX.
#  If not defined, file locations are queried interactively.
#
#  For standard install ($PREFIX="/usr") root privileges are required.
#  Run script as root or use "su -c ./build" or "sudo ./build".
#
#  For end-user install DO NOT run as root, else file ownerships
#  will be root instead of user.

APPNAME=fotox

opt="$1"
if [ -z $opt ]; then opt="both"; fi
if [ $opt != "build" -a $opt != "install" -a $opt != "both" ];
   then echo "Usage: ./build [ build | install | both ]"
   exit 1;
fi

case $opt in
   "build")    build=1; install=0;;
   "install")  build=0; install=1;;
   "both")     build=1; install=1;;
esac

# check prerequisites: g++ and libgtk2.0-dev

error=0
g++ -v  > /dev/null 2>&1
if [ $? -ne 0 ]; then
   echo "*** You must install the GCC compiler package"
   echo "    (C/C++ software development, g++ command)"
   error=1
fi

pkg-config --cflags gtk+-2.0 --libs gtk+-2.0 gthread-2.0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
   echo "*** You must install the GTK+ development package"
   echo "    (GTK+ software development, libgtk2.0-dev)"
   error=1
fi

if [ $error -ne 0 ]; then exit 1; fi

# if $PREFIX is defined, assume standard install locations
# else set default $PREFIX to /usr if root, $HOME if not root

if [ -n "$PREFIX" ]; then
   BINDIR="$PREFIX/bin"
   DATADIR="$PREFIX/share/$APPNAME"
   DOCDIR="$PREFIX/share/doc/$APPNAME"
else
   if [ $(id -u) -eq 0 ]; then  PREFIX=/usr
   else  PREFIX="$HOME"; fi
fi

# if not predefined, query install locations

if [ -z "$BINDIR" ]; then 
   BINDIR="$PREFIX/bin"
   read -p "binary directory: [ $BINDIR ] " dir
   if [ -n "$dir" ]; then BINDIR="$dir"; fi
fi
if [ -z "$DATADIR" ]; then 
   DATADIR="$PREFIX/share/$APPNAME"
   read -p "data directory: [ $DATADIR ] " dir
   if [ -n "$dir" ]; then DATADIR="$dir"; fi
fi
if [ -z "$DOCDIR" ]; then 
   DOCDIR="$PREFIX/share/doc/$APPNAME"
   read -p "doc directory: [ $DOCDIR ] " dir
   if [ -n "$dir" ]; then DOCDIR="$dir"; fi
fi

# build executable in current directory

if [ $build -eq 1 ]; then
   g++ -O -Wall -o $APPNAME $APPNAME.cpp zfuncs.cpp                  \
    $(pkg-config --cflags gtk+-2.0 --libs gtk+-2.0 gthread-2.0)      \
    -D "DATADIR=\"$DATADIR\"" -D "DOCDIR=\"$DOCDIR\""
   if [ $? -ne 0 ]; then exit 1; fi
   echo "$APPNAME build successful"
fi

# move application files to target locations

if [ $install -eq 1 ]; then
   mkdir -p "$DATADIR" "$DOCDIR" "$BINDIR"
   chmod 0755 "$DATADIR" "$DOCDIR" "$BINDIR"
   if [ $? -ne 0 ]; then exit 1; fi
   cp -f  *.pdf  "$DOCDIR"
   cp -f  README  "$DOCDIR"
   cp -f  COPYING  "$DOCDIR"
   cp -f  changelog  "$DOCDIR"
   cp -f -r  icons  "$DATADIR"
   cp -f  *.xtext  "$DATADIR"
   cp -f  parameter*  "$DATADIR" > /dev/null 2>&1
   cp -f  *.conf  "$DATADIR" > /dev/null 2>&1
   cp -f  $APPNAME  "$BINDIR"
   if [ $? -ne 0 ]; then exit 1; fi
   echo "$APPNAME installation successful"
fi

# create desktop icon/launcher if end-user install

if [ -z "$USERNAME" ]; then USERNAME="$USER"; fi
if [ -n "$USERNAME" -a "$USERNAME" != "root" ]; then
   if [ $install -eq 1 ]; then
      home=$(grep "$USERNAME" /etc/passwd | cut -f 6 -d ":")
      LAUNCHER="$home/Desktop/$APPNAME.desktop"
      if [ ! -f "$LAUNCHER" ]; then
         echo "[Desktop Entry]" > "$LAUNCHER"
         echo "Name=$APPNAME" >> "$LAUNCHER"
         echo "Type=Application" >> "$LAUNCHER"
         echo "Terminal=false" >> "$LAUNCHER"
         echo "Exec=$BINDIR/$APPNAME" >> "$LAUNCHER"
         echo "Icon=$DATADIR/icons/$APPNAME.png" >> "$LAUNCHER"
         chown "$USERNAME" "$LAUNCHER"
      fi
   fi
fi

exit 0


