#!/bin/sh

#
# Postinstall script for linux rpm and debian setups
#

INSTALLDIR=/usr/share/realsoft3d-9.1
binlink=/usr/bin
user=
company=
key=

echo Realsoft 3D postinstall script started

#
# Create app resource files
#

echo -n Creating app resources
sfiles="Realsoft3D BatchRen StartOpts RendD RendW BatchRenW ShowImg RsView RsAutoUpdater"

for f in $sfiles
do
  sed "s|\$INSTALLDIR|$INSTALLDIR|g" $INSTALLDIR/resources/$f.ad > $INSTALLDIR/resources/$f
  echo "rm -f \"$INSTALLDIR/resources/$f\"" >>$INSTALLDIR/uninstallrs
  echo -n "."
done
echo done


#
# Create startup scripts
#

echo -n Creating startup scripts
sfiles="realsoft3d batchren startopts rendd rendw batchrenw showimg rsview rsthumbnailer rsautoupdater"

for f in $sfiles
do
  if [ -f $INSTALLDIR/$f ] 
  then
      sed "s|INSTALLDIR=\$|INSTALLDIR=$INSTALLDIR|" $INSTALLDIR/$f > $INSTALLDIR/$f.tmp
      cp $INSTALLDIR/$f.tmp $INSTALLDIR/$f
      rm $INSTALLDIR/$f.tmp
      chmod +x $INSTALLDIR/$f
      chown $USER "$INSTALLDIR/$f"
      echo -n "."
  fi
done
echo done


#
# Create uninstall script
#

echo -n Create uninstalls script
sfiles="uninstallrs"

for f in $sfiles
do
  sed "s|__INSTALLDIR__|$INSTALLDIR|" $INSTALLDIR/$f > $INSTALLDIR/$f.tmp
  cp $INSTALLDIR/$f.tmp $INSTALLDIR/$f
  rm $INSTALLDIR/$f.tmp
  chmod +x $INSTALLDIR/$f
  chown $USER "$INSTALLDIR/$f"
  echo -n "."
done
echo done


#
# Set permissions to environment files
#

echo -n Adjusting permissions for $INSTALLDIR/environments ...
chmod -R go+w $INSTALLDIR/environments 
echo done

succ=0


#
# Set licence to DEMO, user can active the program through Help/About
#

echo -n Creating resource files in $INSTALLDIR/resources
sfiles="Realsoft3D BatchRen StartOpts RendD RendW ShowImg BatchRenW RsView RsThumbnailer RsAutoUpdater"
key=DEMO

for f in $sfiles
do
  echo "$f.User.Id: $key" >> $INSTALLDIR/resources/$f
  echo "$f.User.Name: $user" >> $INSTALLDIR/resources/$f
  echo "$f.User.Company: $company" >> $INSTALLDIR/resources/$f
  echo -n "."
done
echo done


#
# Create links in /usr/bin
#

echo -n Creating links in $binlink
sfiles="realsoft3d startopts rendw batchren rendd showimg batchrenw rsview rsthumbnailer rsautoupdater"

for f in $sfiles
do
  rm -f $binlink/$f
  ln -s $INSTALLDIR/$f $binlink/$f
  echo "rm -f \"$binlink/$f\"" >>$INSTALLDIR/uninstallrs
  echo -n "."
done
echo done


#
# Security Enhanced linux stuff
#

sharedlibs="lib plugins"

selinux_enabled=`cat /proc/filesystems | grep selinuxfs`

if [ ! "$selinux_enabled" = "" ] 
then
    echo -n Security Enhanced Linux enabled, setting policy...
    for f in $sharedlibs
    do
      chcon -t texrel_shlib_t $INSTALLDIR/$f/*
      echo -n "."
    done
    echo done
fi


#
# Write permissions
#

echo -n Set write permissions for the startup.r3d file...
chmod -R go+w $INSTALLDIR/startup.r3d
echo done


#
# Terminate uninstall script
#

echo -n Terminating uninstall script...
echo "echo Realsoft 3D succesfully uninstalled" >>$INSTALLDIR/uninstallrs
echo "echo Thank you for using Realsoft 3D" >>$INSTALLDIR/uninstallrs

cp $INSTALLDIR/uninstallrs $INSTALLDIR/uninstrs
echo done

#
# update databases 
#

echo -n Updating databases...
update-mime-database /usr/share/mime
update-desktop-database
gconftool-2 --install-schema-file=/etc/gconf/schemas/realsoft-thumbnailer.schemas

# make sure the menu will update
killall gnome-panel

echo done

echo Realsoft 3D installed.
