#!/bin/sh

#
# Generic setup script for installing Realsoft3D for unix systems
#

origdir=`pwd`
sysname=`uname`
HOMEDIR=/home/$USER
INSTALLDIR=/usr/local/realsoft3d


#
# Read startup options
#

desktopicon=y
batch=0
binlink=/usr/bin
user=
company=
key=
package=
while [ 1 ]
do
  case $1 in
  '-b')
	batch=1
	shift
	;;
  '-l')
	shift
	binlink=$1
	shift
	;;
  '-d')
	shift
	INSTALLDIR=$1
	shift
	;;
  '-p')
	shift
	package=$1
	shift
	;;
  '-k')
	shift
	key=$1
	shift
	;;
  '-u')
	shift
	user=$1
	shift
	;;
  '-i')
	shift
	desktopicon=$1
	shift
	;;
  '-c')
	shift
	company=$1
	shift
	;;
  *)
    break
    ;;
  esac
done


#
# Welcome message
#

echo
echo "This script will set up the distribution of Realsoft 3D $sysname version"
if [ "$sysname" = "Linux" ]
then
    echo "The setup will also configure desktop icons and menus for the user $USER ($HOMEDIR)"
fi
echo

echo
if [ $batch != 1 ]
then
  echo "Press <Return> to continue..."
  read tmp
fi


#
# Ask target directory
#

echo 
if [ $batch != 1 ]
then
  echo "Enter a directory where to install realsoft3d [$INSTALLDIR]: "
  read INSTALLDIR
fi

if [ "$INSTALLDIR" = "" ]
then
    INSTALLDIR=/usr/local/realsoft3d
fi

#
# Ask folder for the startup links 
#

succ=0

if [ ! -z "$binlink" ]
then
    while [ "$succ" = "0" ]
      do
      if [ $batch != 1 ]
	  then
	  echo 
	  echo "Enter a directory in your path where a symbolic link"
	  echo "to realsoft3d programs will be created [$binlink]: "
	  read tmp
	  if [ "$tmp" = "" ]
	      then
	      tmp=/usr/bin
	  fi
      fi
      
      if [ -z "$tmp" ]
	  then
	  tmp=$binlink
      fi
      if [ ! -d $tmp ]
	  then
	  echo ERROR: Directory $tmp does not exist!
	  echo
      elif [ ! -w $tmp ]
	  then
	  echo ERROR: You do not have write permissions for $tmp
	  echo
      else
	  binlink=$tmp
	  succ=1
      fi
      if [ $batch = 1 ]
	  then
	  succ=1
      fi
    done
fi



#
# Ask license
#

demo=0
if [ "$package" != "" ]
then
  echo $package | grep -q "realsoft3d-.*-demo-" && demo=1
  echo $package | grep -q "realsoft3d-.*-se-" && demo=1
fi

if [ $batch = 0 -a $demo = 0 ]
then
  echo
  echo "Enter your install key if you have one."
  echo "If not you will only be able to run in demo mode:"
  read key
fi

if [  "$demo" = 0 ]
then
    if [ -z "$key" ]
	then
	echo "No install key, program will run in demo mode."
	echo "Note that you can activate the program later "
	echo "by entering the install key in Help/About."
	key="DEMO"
    fi
fi


#
# Ask if desktop menus should be installed
#

desktopfolders="Desktop"

# create desktop icon for linux

if [ $batch != 1  ]
then
    if [ "$sysname" = "Linux" ]
    then
	echo 
	echo "Create a startup Icon for the Desktop [y])? "
	read desktopicon
	
	if [ "$desktopicon" = "" ]
	    then
	    desktopicon=y
	fi
    fi
fi


#
# Questions asked, notify about the actual setup 
#
if [ $batch != 1 ]
then
    echo "Setup has all the information it nees to to install the software."
    echo "Press any key to start the installation."
    read tmp
fi


#
# Create target folder
#

succ=0

while [ $succ = 0 ]
do
  if [ ! -d $INSTALLDIR ]
  then
      mkdir -p $INSTALLDIR
      if [ $? != 0 ]
      then
	  echo Failed to create $INSTALLDIR
      else
	  echo "[$INSTALLDIR] created"
	  succ=1
      fi
  else
      echo "Installing over existing [$INSTALLDIR]"
      succ=1
  fi
done




#
# Extract the package in the target directory
#

cd $INSTALLDIR
if [ "$package" != "" ]
then
  echo -n "Extracting files ($package -extract) ..."
  $package -extract
  echo done.
fi


#
# Create app resource files
#

echo -n "Creating app resources"

sfiles="Realsoft3D BatchRen StartOpts RendD RendW BatchRenW ShowImg RsView RsThumbnailer r3auw"

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 (un)install scripts
#

echo -n Creating uninstall scripts

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




#
# Add install key to app resource files
#

echo -n Creating resource files in $INSTALLDIR/resources

sfiles="Realsoft3D BatchRen StartOpts RendD RendW ShowImg BatchRenW RsView RsThumbnailer r3auw"
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 desktop menus
#

mkdesktopicon() 
{
    echo "[Desktop Entry]" > $1/$2
    echo "Comment=Realsoft 3D modeling, animation and rendering software" >> $1/$2
    echo "Encoding=UTF-8" >> $1/$2
    echo "Exec=$INSTALLDIR/realsoft3d" >> $1/$2
    echo "Version=5.1" >> $1/$2
    echo "Type=Application" >> $1/$2
    echo "Icon=$INSTALLDIR/icons/realsoft3d.png" >> $1/$2
    echo "Terminal=false" >> $1/$2
    echo "Name[$LANG]=Realsoft 3D" >> $1/$2
    echo "GenericName[$LANG]=Realsoft 3D" >> $1/$2
    echo "Comment[$LANG]=Realsoft 3D modeling, animation and rendering software" >> $1/$2
    chown $USER "$1/$2"
    chmod 644 "$1/$2"

    echo "rm \"$1/$2\"" >>$INSTALLDIR/uninstallrs
    return 1
}

mkdesktopmenudirectory() 
{
    echo "[Desktop Entry]" > $1/$2.directory
    echo "Name=$2" >> $1/$2.directory
    echo "Encoding=UTF-8" >> $1/$2.directory
    echo "Type=Directory" >> $1/$2.directory
    echo "Icon=$INSTALLDIR/icons/$3" >> $1/$2.directory
    chmod 644 "$1/$2.directory"

    echo "rm $1/$2.directory" >>$INSTALLDIR/uninstallrs
    return 1
}

installdesktopmenudirectory() 
{
    cp "$INSTALLDIR/resources/realsoft.menu" $1
    echo "rm $1/realsoft.menu" >>$INSTALLDIR/uninstallrs
}


mkdesktopentry()
{
    echo "[Desktop Entry]" > $1/realsoft-$2.desktop
    echo "Name=$3" >>$1/realsoft-$2.desktop
    echo "Comment=$4" >> $1/realsoft-$2.desktop
    echo "Exec=$5" >> $1/realsoft-$2.desktop
    echo "Type=Application" >> $1/realsoft-$2.desktop
    echo "Icon=$INSTALLDIR/icons/$2.png" >> $1/realsoft-$2.desktop
    echo "Terminal=$7" >> $1/realsoft-$2.desktop
    echo "Categories=Application;Realsoft3D" >>$1/realsoft-$2.desktop
    echo "MimeType=$6" >>$1/realsoft-$2.desktop
    echo "rm $1/realsoft-$2.desktop" >>$INSTALLDIR/uninstallrs
    return 1
}


if [ "$desktopicon" = "y" -o "$desktopicon" = "Y" -o "$desktopicon" = "1" ]
then
    for f in $desktopfolders
    do
      if [ -d $HOMEDIR/$f ]
      then 
	  echo Creating desktop icon for the user...
	  mkdesktopicon "$HOMEDIR/$f" "Realsoft 3D.desktop"
      fi
    done

    deskdir=/usr/share/desktop-directories
    if [ -d $deskdir ]
    then
	echo Creating menu directory...
      	mkdesktopmenudirectory "$deskdir" "Realsoft" "realsoft.png" 
	mkdesktopmenudirectory "$deskdir" "Realsoft3D" "realsoft3d.png" 
    fi

    appdir=/usr/share/applications
    if [ -d $appdir ]
    then 
	echo Creating menus...
	mkdesktopentry "$appdir" "realsoft3d" "Realsoft 3D" "Realsoft 3D modeling, animation and rendering tool" "$INSTALLDIR/realsoft3d %F" "application/realsoft3d-project" "false"
	mkdesktopentry "$appdir" "showimg" "Show Image" "Realsoft image viewer" "$INSTALLDIR/showimg -file %f" "application/realsoft3d-image" "" "false"
	mkdesktopentry "$appdir" "rendw" "Rendering Server" "Distributed network rendering server" "$INSTALLDIR/rendw" "" "false"
	mkdesktopentry "$appdir" "batchrenw" "Batch Renderer" "Batch rendering utility" "$INSTALLDIR/batchrenw %F" "application/realsoft3d-project" "false"
	mkdesktopentry "$appdir" "startopts" "Startup Options" "Utility for configuring default startup options" "$INSTALLDIR/startopts" "" "false"
	mkdesktopentry "$appdir" "rsview" "Scene View" "Utility for viewing .r3d files" "$INSTALLDIR/rsview -file %f" "application/realsoft3d-project" "false"
	mkdesktopentry "$appdir" "rsautoupdater" "Software Updater" "Utility for updating Realsoft software" "$INSTALLDIR/rsautoupdater -file %f" "" "false"
	mkdesktopentry "$appdir" "manual" "User Manuals" "Realsoft 3D manuals" "htmlview $INSTALLDIR/realsoft/realsoft.html" "" "false"
    fi

    appmergdir=/etc/xdg/menus/applications-merged
    if [ ! -d $appmergdir ]
    then 
	mkdir -p $appmergdir
    fi
    echo Merging menus...
    installdesktopmenudirectory "$appmergdir"

    # install mime types
    mimedir=/usr/share/mime/packages
    if [ -d $mimedir ]
    then
	echo Installing mime types for .r3d and .r3i formats...
	cp $INSTALLDIR/resources/realsoft3d-*.xml $mimedir
	update-mime-database /usr/share/mime
    fi

    # thumbnailer keys

    schemadir=/etc/gconf/schemas 
    if [ -d $schemadir ]
    then
	echo Installing thumbnailer for the desktop...
	cp $INSTALLDIR/resources/realsoft-thumbnailer.schemas $schemadir/
	gconftool-2 --install-schema-file=$schemadir/realsoft-thumbnailer.schemas
    fi

    update-desktop-database
    killall gnome-panel
fi

#
# Create startup links
#

sfiles="realsoft3d startopts rendw batchren rendd showimg batchrenw rsview rsthumbnailer rsautoupdater"

if [ ! -z "$binlink" -a -w "$binlink" ]
then
    echo -n Creating startup links
    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
fi


# 
# Security Enhanced linux 
#

sharedlibs="lib plugins"

if [ "$sysname" = "Linux" ]
then
    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
fi


#
# Make sure we really got something installed
#

if [ ! -d $INSTALLDIR/bin ]
then
   echo *** FATAL: Cannot find $INSTALLDIR/bin folder.
   echo Something went wrong, sorry
   echo Program was not installed correctly!
fi


#
# Set permissions for the files users should be able to change
#

echo "Adjusting permissions..."
chmod -R go+w $INSTALLDIR/environments 
chmod -R go+w windows startup.r3d


cd $origdir

#
# terminate uninstall script
#

echo "rm -r -f \"$HOMEDIR/.realsoft\"" >>$INSTALLDIR/uninstallrs
echo "echo Realsoft 3D succesfully uninstalled" >>$INSTALLDIR/uninstallrs
echo "echo Thank you for using Realsoft 3D" >>$INSTALLDIR/uninstallrs
echo "echo Have a nice day \$USER!" >>$INSTALLDIR/uninstallrs

#
# Success message
#

echo
echo Setup done.
if [ $batch = 1 ]
then
  echo 
  echo "*****************"
  echo 
  echo Installation is now finished.
  echo Press the Quit button to exit the installation program.
fi
