#!/bin/bash
#***************************************************************************
#*   Copyright (C) 2008 by Juergen Thies                                   *
#*   layout@juergenthies.de                                                *
#*                                                                         *
#*   This program is free software; you can redistribute it and/or modify  *
#*   it under the terms of the GNU General Public License as published by  *
#*   the Free Software Foundation; either version 2 of the License, or     *
#*   (at your option) any later version.                                   *
#*                                                                         *
#*   This program is distributed in the hope that it will be useful,       *
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#*   GNU General Public License for more details.                          *
#*                                                                         *
#*   You should have received a copy of the GNU General Public License     *
#*   along with this program; if not, write to the                         *
#*   Free Software Foundation, Inc.,                                       *
#*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
#***************************************************************************
#  
#    Generates the menu entry and file types
#
#

DIR="$( cd "$( dirname "$0" )" && pwd )"


 BASE="$( cd "$( dirname "$0" )/.." && pwd )"
 FILE="/usr/bin/layout"

 if [ -f "$BASE/bin/layout" ]; then
     BASE=$BASE
     FILE=$BASE"/start"
 else 
     if [ -f "$BASE/layout" ]; then
        BASE=$BASE"/.."
        FILE=$BASE"/start"
     else
        if [ -f "/opt/layout/bin/layout" ]; then
          BASE="/usr/bin"
          FILE="/usr/bin/layout"
        fi
     fi
 fi

 if [ -f "$DIR/layout.png" ]; then
     DIR=$DIR
 else
     DIR="/opt/layout/install"
 fi

 if [ -L $BASE"/oa" ]; then
     echo
 else
     ln -s $BASE"/plugins/liboa22.50" $BASE"/oa"
 fi

#echo $FILE  
FILE="$( echo $FILE | sed -e "s/\//\\\\\//g" )"
Dir="$( echo $DIR | sed -e "s/\//\\\\\//g" )"

cd $DIR
 
cat layout_src.desktop | sed -e "s/path/$FILE/g" | sed -e "s/installdir/$Dir/g" > layout.desktop
cat layout3d_src.desktop | sed -e "s/path/$FILE/g" | sed -e "s/installdir/$Dir/g" > layout3d.desktop
cat schematic_src.desktop | sed -e "s/path/$FILE/g" | sed -e "s/installdir/$Dir/g" > schematic.desktop
cat layouttextedit_src.desktop | sed -e "s/path/$FILE/g" | sed -e "s/installdir/$Dir/g" > layouttextedit.desktop
 
export PATH=$PATH:$DIR/xdg-utils

xdg-icon-resource install --novendor --size 48 $DIR/layout.png || exit 0
xdg-icon-resource install --novendor --size 48 $DIR/schematic.png || exit 0
xdg-icon-resource install --novendor --size 48 $DIR/layout3d.png || exit 0
xdg-icon-resource install --novendor --size 48 $DIR/layouttextedit.png || exit 0
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout.png application-cif
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout.png application-gds
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout.png application-dxf
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout.png application-oasis
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/schematic.png application-les
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layouttextedit.png application-txt
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layouttextedit.png application-tcl
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layouttextedit.png application-layout
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layouttextedit.png application-py
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout3d.png application-gds3d
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout3d.png application-stl
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout3d.png application-dxf
xdg-icon-resource install --context mimetypes --novendor --size 48 $DIR/layout3d.png application-pov
xdg-mime install --novendor $DIR/layout-mimetypes.xml
xdg-desktop-icon install --novendor $DIR/layout.desktop 2> /dev/null
xdg-desktop-menu install --novendor $DIR/layout.desktop 2> /dev/null
xdg-desktop-icon install --novendor $DIR/schematic.desktop 2> /dev/null
xdg-desktop-menu install --novendor $DIR/schematic.desktop 2> /dev/null
xdg-desktop-icon install --novendor $DIR/layout3d.desktop 2> /dev/null
xdg-desktop-menu install --novendor $DIR/layout3d.desktop 2> /dev/null
xdg-desktop-icon install --novendor $DIR/layouttextedit.desktop 2> /dev/null
xdg-desktop-menu install --novendor $DIR/layouttextedit.desktop 2> /dev/null

if [[ $EUID -eq 0 ]]; then
if [ -f /usr/bin/desktop-file-install ]; then
   desktop-file-install layout.desktop 2> /dev/null
   desktop-file-install layout3d.desktop 2> /dev/null
   desktop-file-install layouttextedit.desktop 2> /dev/null
   desktop-file-install schematic.desktop 2> /dev/null
fi
fi


test -d /opt/layout/plugins && {
                 chmod a+x /opt/layout/plugins/libplugin*.so
                }

