#!/bin/sh
# postinst script for pymp
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

echo $0 $* > /tmp/`basename $0``date +%s`

linkFilesOneByOne()
{
  if [[ -d "/usr/share/icons/hicolor/" ]]
  then
    target="/usr/share/icons/hicolor/16x16/apps/pymp.png"
    source="/opt/pymp/data/icons/16x16/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
    target="/usr/share/icons/hicolor/26x26/apps/pymp.png"
    source="/opt/pymp/data/icons/26x26/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
    target="/usr/share/icons/hicolor/40x40/apps/pymp.png"
    source="/opt/pymp/data/icons/40x40/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
    target="/usr/share/icons/hicolor/48x48/apps/pymp.png"
    source="/opt/pymp/data/icons/48x48/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
    target="/usr/share/icons/hicolor/64x64/apps/pymp.png"
    source="/opt/pymp/data/icons/64x64/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
    target="/usr/share/icons/hicolor/scalable/apps/pymp.png"
    source="/opt/pymp/data/icons/scalable/pymp.png"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $source $target
      fi
    fi
  fi
}

linkFilesGeneric()
{
  optdir="/opt/pymp/data/icons"
  sharedir="/usr/share/icons/hicolor"
  for i in `find $optdir -type f`
  do
    fileName=`basename $i`
    dirName=`dirname $i`
    escaped=`echo $optdir | sed " s/\//\\\/g " `
    shortDirName=`echo $dirName | sed " s/$escaped//g " `
    target="$sharedir/$shortDirName/apps/$fileName"
    if [[ ! -f "$target" ]]
    then
      if [[ -f "$i" ]]
      then
        ln -s $i $target
      fi
    fi
  done
}

linkFiles()
{
  rc=`type sed`
  if [[ "$rc" != "0" ]]
  then
    linkFilesOneByOne
  else
    linkFilesGeneric
  fi
}

case "$1" in
    configure)
        linkFiles
        if [[ -d "/opt/pymp" ]]
        then
          chown -R user:users /opt/pymp
          chmod -R a+rwx /opt/pymp
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
      echo "postinst called with unknown argument \`$1'" >&2
      exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#

exit 0
