#!/bin/sh

##VGAG_VERSION##
##VGAG_PREFIX##

INCDIR="$VGAG_PREFIX/include"
LIBDIR="$VGAG_PREFIX/lib"
IOLIB_SCRIPT="$VGAG_PREFIX/share/vgagames4/libexec/iolib-script"
EXTRALIBS=`$IOLIB_SCRIPT --extralibs`

ARG0=`basename $0`

para=""
while test $# -gt 0
do
  if test "$1" = "--cflags"
  then
    # -I
    para="$para -I$INCDIR"

  elif test "$1" = "--libs" || test "$1" = "--libspath"
  then
    # -L and -l, library path hardcoded
    para="$para -L$LIBDIR -Wl,-rpath,vgag4/lib,-rpath,lib,-rpath,$LIBDIR -lvgagames4 $EXTRALIBS"

  elif test "$1" = "--extralibs"
  then
    # extra libraries
    echo "$EXTRALIBS"
    para=":"

  elif test "$1" = "--prefix"
  then
    # install prefix
    echo "$VGAG_PREFIX"
    para=":"

  elif test "$1" = "--version"
  then
    # version
    echo "$VGAG_VERSION"
    para=":"

  elif test "$1" = "--version-maior"
  then
    # maior version
    echo "$VGAG_VERSION" | cut -d. -f1
    para=":"

  elif test "$1" = "--version-minor"
  then
    # minor version
    echo "$VGAG_VERSION" | cut -d. -f2 | cut -dp -f1
    para=":"

  elif test "$1" = "--copylocal" || test "$1" = "--copylocal_with_midi"
  then
    # create directory "vgag4" and copy required files to it to use game pathindependent
    udir="vgag4"
    lib1=`readlink $LIBDIR/libvgagames4.so`
    lib2=""; test "x$lib1" != "x" && lib2=`readlink $LIBDIR/$lib1`
    test "x$lib2" = "x" && { echo "libvgagames4.so not found" >&2; exit 1; }
    mkdir $udir || exit 1
    cd $udir || exit 1
    mkdir lib share || exit 1
    ln -s $lib1 lib/libvgagames4.so
    ln -s $lib2 lib/$lib1
    cp $LIBDIR/$lib2 lib/
    cp -a $VGAG_PREFIX/share/vgagames4 share/
    rm -f share/vgagames4/libexec/iolib-script
    test -f "$VGAG_PREFIX/bin/vg4_nw_server" && cp $VGAG_PREFIX/bin/vg4_nw_server .
    for tag in `$IOLIB_SCRIPT --list`
    do
      io_lib=`$IOLIB_SCRIPT $tag --lib`
      test "x$io_lib" != "x" && cp "$io_lib" share/vgagames4/libexec/
    done
    if test "$1" != "--copylocal_with_midi"
    then
      rm -f share/vgagames4/libexec/vgag-midi
      rm -rf share/vgagames4/libexec/tmidi-pats
    fi
    echo
    echo "$udir/ created"
    echo
    echo "The executable game must be called from the directory"
    echo "containing $udir/ (i.e. the current directory)"
    echo "and must not change it with chdir(),"
    echo "because files are searched in the relative path \"$udir\"."
    test -f "$VGAG_PREFIX/bin/vg4_nw_server" && echo "\"$udir/vg4_nw_server\" is a program for the network-server on the shell."
    echo
    para=":"

  elif test "$1" = "--pc"
  then
    # output pc-file for pkgconf
    vgag_maior=`echo "$VGAG_VERSION" | cut -d. -f1`
    echo "Name: VgaGames$vgag_maior"
    echo "Version: $VGAG_VERSION"
    echo "Description: VgaGames is a development library for creating games with a resolution of 320x200/320x240 or 640x480/800x600 pixels."
    echo "URL: https://www.vgagames.de/"
    echo "Cflags: -I$INCDIR"
    echo "Libs: -L$LIBDIR -Wl,-rpath,vgag4/lib,-rpath,lib,-rpath,$LIBDIR -lvgagames4 $EXTRALIBS"
    para=":"
  fi
  shift
done

if test "$para" != ""
then
  test "$para" != ":" && echo " $para"
else
  echo "Usage: $ARG0 --cflags               # CFLAGS: flags for c-compiler" >&2
  echo "       $ARG0 --libs                 # LDFLAGS and LIBS: libraries to be included" >&2
  echo "       $ARG0 --extralibs            # external libraries: just the external ones" >&2
  echo "       $ARG0 --prefix               # PREFIX: installation prefix" >&2
  echo "       $ARG0 --version              # version: <maior release>.<minor release>[p<patchlevel]" >&2
  echo "       $ARG0 --version-maior        # version: <maior release>" >&2
  echo "       $ARG0 --version-minor        # version: <minor release>" >&2
  echo "       $ARG0 --copylocal            # copy required files to vgag4/ for local game-execution" >&2
  echo "       $ARG0 --copylocal_with_midi  # same as --copylocal but with Midi-File support" >&2
  echo "       $ARG0 --pc                   # output of pc-file for pkgconf" >&2
  exit 1
fi
exit 0
