#!/bin/bash
# builds portable client

# set -x

# add AppRun
cp portable/AppRun ${APPDIR}/

# add desktop file to main dir and canonical place
find ${APPDIR} -type f -name "*.desktop" -exec mv \{\} ${APPDIR}/org.armagetronad.${PACKAGE}.desktop \;
find ${APPDIR} -type l -name "*.desktop" -exec rm -f \{\} \;
mkdir -p ${APPDIR}/usr/share/applications/
cp ${APPDIR}/org.armagetronad.${PACKAGE}.desktop ${APPDIR}/usr/share/applications/ || exit $?

# add appinfo
mkdir -p ${APPDIR}/usr/share/metainfo || exit $?
cp ${APPDIR}/usr/share/games/${PACKAGE}/desktop/armagetronad.appdata.xml ${APPDIR}/usr/share/metainfo/org.armagetronad.${PACKAGE}.appdata.xml || exit $?
#sed -i -e 's,type="desktop-id">,type="desktop-id">../../../,' ${APPDIR}/usr/share/metainfo/org.armagetronad.${PACKAGE}.appdata.xml || exit $?

# hack Icon
for f in $(find ${APPDIR} -type d -name "desktop"); do
  cp $f/icons/48x48/*.png ${APPDIR}/${PACKAGE}.png
done

# correct absolute symlinks
APPDIR=${APPDIR} $(dirname $0)/fixlinks

lastpath=/usr/lib/*linux-gnu
otherpath=${lastpath}

function copylib()
{
    baselib=$1
    while test -n "${baselib}"; do
      if echo ${baselib} | grep -q \^/; then
        # absolute path, split
        path=$(dirname ${baselib})
        baselib=$(basename ${baselib})
        echo "Found in ${path}."
        if test "$lastpath" != "$path"; then
            otherpath=$lastpath
        fi
        lastpath=$path
      fi
      # copy libary
      echo Copying ${baselib}...
      cp -a "${path}/${baselib}" ${APPDIR}/usr/lib/ || exit $?
      # resolve links
      baselib=$(ls -l ${path}/${baselib} | grep -- "->" | sed -e "s,.*-> ,,")
    done
}

# dependency scrapyard
# libstdc++ \

#   include libraries
mkdir -p ${APPDIR}/usr/lib/
for library in \
libasound \
libasyncns \
libboost_thread \
libcaca \
libdbus-[0-9\.]* \
libFLAC \
libfreetype \
libftgl \
libGLEW \
libGLU \
libjbig \
libjpeg[0-9\.]* \
libncursesw \
libnsl \
libogg \
libpng[0-9]* \
libpulse \
libpulsecommon-[0-9\.]* \
libpulse-simple \
libprotobuf \
libSDL-1.2 \
libslang \
libsndfile \
libSDL_image-1.2 \
libSDL_mixer-1.2 \
libtiff \
libtinfo \
libvorbis \
libvorbisenc \
libwebp \
libwrap \
libxml2 \
libZThread-[0-9\.]* \
; do
  echo "Checking for library ${library}...."
  baselib=$(ldd ${APPDIR}/usr/bin/${PACKAGE} | grep "${library}\.so[0-9\.]*" | sed -e "s,.*=> ,," -e "s, (.*,,")
  if test -z "${baselib}"; then
    echo "Not linked."
  else
    copylib ${baselib}
  fi
done

# ldd ${APPDIR}/usr/bin/${PACKAGE}

if false && ! echo ${PACKAGE} | grep dedicated; then
# libraries we know are dynamically loaded later, so they don't appear in ldd's output
for library in \
libjpeg.so.8 \
; do
    echo "Installing library ${library} anyway, we know it's needed."
    if test -r ${lastpath}/${library}; then
        copylib ${lastpath}/${library}
    else
        copylib ${otherpath}/${library}
    fi
done
fi

# more libraries for 0.4, later
#libmikmod.so \
#libmikmod.so.2 \

echo $lastpath
