#!/bin/sh
echo $#
if [ $# -lt 3 ]
then
  echo ;
  echo Insufficient Arguments
  echo ;
  echo run ./rbsetup to start the Graphical installation
  echo ;
  echo install [install type shared/static] [install directory ] [Menu name ]
  echo;
  echo eg. ./install 1 /opt RapidBuilder    for installing shared RBv1.0
  echo     ./install 0 $HOME RapidBuilder   for installing static version
  
  exit 7
fi;
echo shared  $1
echo Install Directory $2
echo Install Menu Dir    $3

export INSTALL_PATH=$2
export MENUDIR=$3

echo $INSTALL_PATH
#Change the name of INSTALL_PKG to whatever is the name of the current version
#the tar file name should start with this.
export INSTALL_PKG=RapidBuilder1.0demo
####################################test##########
if [ "$4" -eq 0 ]
then
   echo player not to be installed
   export INSTALLPLAYER=0
else
   echo java path is $4
   export INSTALLPLAYER=1
   export JAVAPATH="$4"
fi
#exit 0
####################################
#check if user is super user /Administrator
if [ $UID -ne 0 ]
then
	echo Running as user $USER
#setting folder for menu entries
   export KDEMENUDIR=$HOME/.kde
   export GNOMEMENUDIR=$HOME/.gnome
   export EXEC_PATH="$HOME/bin"
   if [ -e $EXEC_PATH ]
   then
    echo $EXEC_PATH found      .. [OK]
   else
     mkdir $EXEC_PATH
   fi

  if [ -e $KDEMENUDIR/share/applnk-mdk ]
  then
    export APPLNK=applnk-mdk
  else
    export APPLNK=applnk
  fi  
#if root then install globally
else

  if [ 1 -eq ${KDEDIR}1 ]
  then
     export KDEDIR=/usr
     echo setting KDEDIR as $KDEDIR
  fi

  if [ -e $KDEDIR/share/applnk-mdk ]
  then
    export APPLNK=applnk-mdk
  else
    export APPLNK=applnk
  fi  
  export KDEMENUDIR=$KDEDIR
  export GNOMEMENUDIR=$KDEDIR/share/$APPLNK/gnome
  export EXEC_PATH=/usr/bin
fi
echo ----------------------------
echo $INSTALL_PATH/$INSTALL_PKG
echo ----------------------------
echo -n checking if the install directory already exists
if [ -e "$INSTALL_PATH/$INSTALL_PKG" ]
then
   echo \ndirectory already exists removing
   echo deleting older version $INSTALL_PATH/$INSTALL_PKG
    rm -fR "$INSTALL_PATH/$INSTALL_PKG"
else
   echo ..[OK]
fi
#Uncompressing installation
#1 means shared else static
if [ $1 -eq 1 ] 
then
  export INSTALLTAR="$PWD/$INSTALL_PKG.shared.tar.gz"
else
  export INSTALLTAR="$PWD/$INSTALL_PKG.static.tar.gz"
fi
export PLAYERFILES="$PWD"
echo Installation package is $INSTALLTAR
if [ -e "$INSTALLTAR" ]
then
   echo Installation Package found.
else 
   echo Installation Package not found. Aborting Setup
   exit 4;
fi
#change to the directory to install
if cd "$INSTALL_PATH"
then
  echo change directory successful
else
 echo could not change directory
 if [ $UID -ne 0 ]
 then
   export INSTALL_PATH=$HOME
 else
  export INSTALL_PATH=/opt
 fi
 echo Installation will occur in $INSTALL_PATH
 if cd $INSTALL_PATH
 then
   echo continuing installation
 else
   echo Installation aborted, cannot continue
   exit 7
 fi
fi
if tar zxvfp "$INSTALLTAR";
then
   echo Uncompress Successful
   if [ $INSTALLPLAYER -eq 1 ]
   then
    if  cp "$PLAYERFILES/RPPlayer1.0.jar" "$INSTALL_PATH/$INSTALL_PKG/";
    then
      cp "$PLAYERFILES/playerImage.xpm" "$INSTALL_PATH/$INSTALL_PKG/"
      echo copied Player Files ..
    else
      export INSTALLPLAYER=0
      echo could not copy Player Files! 
      echo Player will not be installed.
    fi
   fi
else
   echo Uncompress of $INSTALLTAR failed
   exit 5;
fi
#Menu Folder Creation
	if [ -e $KDEMENUDIR ]
	then
	   echo $KDEMENUDIR              .. [OK]
        else
	   mkdir $KDEMENUDIR
       	fi
	if [ -e $KDEMENUDIR/share ]
	then
	   echo $KDEMENUDIR/share        .. [OK]
	else
	   mkdir $KDEMENUDIR/share
	fi
	if [ -e $KDEMENUDIR/share/$APPLNK ]
	then
	   echo $KDEMENUDIR/share/$APPLNK .. [OK]
	else
	   mkdir $KDEMENUDIR/share/$APPLNK
	fi
	export KDEMENUDIR=$KDEMENUDIR/share/$APPLNK

	if [ -e $GNOMEMENUDIR ]
	then
	   echo $GNOMEMENUDIR            .. [OK]
	else
	   mkdir $GNOMEMENUDIR
	fi
        if [ $UID -ne 0 ]
	then
	  if [ -e $GNOMEMENUDIR/apps ]
	  then
	   echo $GNOMEMENUDIR/apps	  .. [OK]
	  else
	   mkdir $GNOMEMENUDIR/apps
	  fi
          export GNOMEMENUDIR=$GNOMEMENUDIR/apps
	fi
echo Finished creating menu folders

#create the executable rapidbuilder script
 echo checking if $EXEC_PATH/rapidbuilderdemo exist
 if [ -e "$EXEC_PATH/rapidbuilderdemo" ]
 then
    echo removing $EXEC_PATH/rapidbuilderdemo
    rm -f "$EXEC_PATH/rapidbuilderdemo"
 fi
echo export LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:$INSTALL_PATH/$INSTALL_PKG/lib\" > "$EXEC_PATH/rapidbuilderdemo"; 
echo export RBHOME=\"$INSTALL_PATH/$INSTALL_PKG\" >> "$EXEC_PATH/rapidbuilderdemo"; 
  echo CUR_PATH=\$PWD >> "$EXEC_PATH/rapidbuilderdemo";
  echo cd \"$INSTALL_PATH/$INSTALL_PKG\" >> "$EXEC_PATH/rapidbuilderdemo";
  echo \"$INSTALL_PATH/$INSTALL_PKG/RapidBuilder\" >> "$EXEC_PATH/rapidbuilderdemo";
 echo Created $EXEC_PATH/rapidbuilderdemo
 chmod +x "$EXEC_PATH/rapidbuilderdemo";

#create executable rapidplayer script
if [ $INSTALLPLAYER -eq 1 ]
then
 echo checking if $EXEC_PATH/rapidplayerdemo exist
 if [ -e "$EXEC_PATH/rapidplayerdemo" ]
 then
    echo removing $EXEC_PATH/rapidplayerdemo
    rm -f "$EXEC_PATH/rapidplayerdemo"
 fi
  echo CUR_PATH=\$PWD >> "$EXEC_PATH/rapidplayerdemo";
  echo cd \"$INSTALL_PATH/$INSTALL_PKG\" >> "$EXEC_PATH/rapidplayerdemo";
  echo \"$JAVAPATH\" -jar "$INSTALL_PATH/$INSTALL_PKG/RPPlayer1.0.jar" "\$1">>"$EXEC_PATH/rapidplayerdemo";
   echo Created $EXEC_PATH/rapidplayerdemo
chmod +x "$EXEC_PATH/rapidplayerdemo"
fi;

echo create the directory and if successfull create .kdelnk files
if [ -e $KDEMENUDIR/RapidBuilderDemo ]
then
   echo removing $KDEMENUDIR/RapidBuilderDemo
   rm -fR $KDEMENUDIR/RapidBuilderDemo
fi
if mkdir $KDEMENUDIR/RapidBuilderDemo
then
     export MENU_FILE=$KDEMENUDIR/RapidBuilderDemo/RapidBuilderv1.0.kdelnk
     echo Creating KDE Panel Menu Entries...
     echo ;
     echo "# KDE Config File" >$MENU_FILE
     echo [KDE Desktop Entry] >>$MENU_FILE
     echo Name=RapidBuilder   >>$MENU_FILE
     echo Exec=\"$EXEC_PATH/rapidbuilderdemo\"  >>$MENU_FILE
     echo Icon="$INSTALL_PATH/$INSTALL_PKG/titleImage.xpm"  >>$MENU_FILE
     echo Type=Application    >>$MENU_FILE
     echo Comment=RapidBuilder For Linux Demo>>$MENU_FILE
     echo Terminal=0          >>$MENU_FILE
 
     export MENU_FILE=$KDEMENUDIR/RapidBuilderDemo/.directory
 
     echo "# KDE Config File" >$MENU_FILE
     echo [KDE Desktop Entry]     >>$MENU_FILE
     echo Comment=RapidBuilder For Linux Demo >>$MENU_FILE
     echo Icon="$INSTALL_PATH/$INSTALL_PKG/titleImage.xpm" >>$MENU_FILE
     echo Name=$MENUDIR   >>$MENU_FILE               	
     echo Type=Directory  >>$MENU_FILE

if [ $INSTALLPLAYER -eq 1 ]
then  
  echo Creating RapidPlayer link
     export MENU_FILE=$KDEMENUDIR/RapidBuilderDemo/RapidPlayerv1.0.kdelnk
     echo Creating KDE Panel Menu Entries...
     echo ;
     echo "# KDE Config File" >$MENU_FILE
     echo [KDE Desktop Entry] >>$MENU_FILE
     echo Name=RapidPlayer   >>$MENU_FILE
     echo Exec=\"$EXEC_PATH/rapidplayerdemo\"  >>$MENU_FILE
     echo Icon="$INSTALL_PATH/$INSTALL_PKG/playerImage.xpm"  >>$MENU_FILE
     echo Type=Application    >>$MENU_FILE
     echo Comment=RapidPlayer For Linux Demo>>$MENU_FILE
     echo Terminal=0          >>$MENU_FILE
fi
echo Creating Uninstall Link
     export MENU_FILE=$KDEMENUDIR/RapidBuilderDemo/Uninstall.kdelnk
     echo "# KDE Config File" >$MENU_FILE
     echo [KDE Desktop Entry] >>$MENU_FILE
     echo Name=Uninstall      >>$MENU_FILE
     echo Exec=\"$INSTALL_PATH/$INSTALL_PKG/Uninstall\"   >>$MENU_FILE
     echo Icon=$INSTALL_PATH/$INSTALL_PKG/deleting.xpm  >>$MENU_FILE
     echo Type=Application    >>$MENU_FILE
     echo Comment=Uninstall RapidBuilder For Linux Demo >>$MENU_FILE
     echo Terminal=1          >>$MENU_FILE
echo Creating sym links for gnome and linux menu entries
     echo deleting old links if present
	rm -fR $GNOMEMENUDIR/RapidBuilderDemo
	rm -fR /etc/X11/applnk/RapidBuilderDemo
        ln -s $KDEMENUDIR/RapidBuilderDemo $GNOMEMENUDIR/
	export ISKDE2DIR=0
        if [ $UID -ne 0 ]
	then
	 export KDE2DIR=$HOME/.kde2
	else
	 export KDE2DIR=/opt/kde2
	fi
	if [ -e $KDE2DIR ]
	then  
	   if ln -s $KDEMENUDIR/RapidBuilderDemo $KDE2DIR/share/applnk
	   then
		export ISKDE2DIR=1
		export KDE2DIR=$KDE2DIR/share/applnk
		# a recursive link was found to be created couldnt find
		# why it occurs any way its bcos of th if ln -s ... command
		# so am going the easy way out deleteing the unwanted link
		rm -f $KDEMENUDIR/RapidBuilderDemo/RapidBuilderDemo
	    fi
	 fi 
        if [ $UID -ne 0 ]
	then
	  echo ; 
	else
	   if [ -e /etc/X11/applnk ]
	   then
	     echo /etc/X11/applnk exists ..[OK]
	   else
	      mkdir /etc/X11/applnk
	   fi
	    ln -s $KDEMENUDIR/RapidBuilderDemo /etc/X11/applnk/
        fi
else
  echo Error. Menus not created..
  exit 6
fi
#for uninstallation
if [ $UID -eq 0 ]
then
  echo if \[ \$UID -ne 0 \] >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
  echo then >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
  echo echo This program was installed by root >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
  echo echo Enter the password for root  >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
  echo su - -c "$INSTALL_PATH/$INSTALL_PKG/Uninstall" >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
  echo fi >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
fi

echo cd \"$INSTALL_PATH\" >>    "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" \"$EXEC_PATH/rapidbuilderdemo\" >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"

if [ $INSTALLPLAYER -eq 1 ]
then
  echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" \"$EXEC_PATH/rapidplayerdemo\" >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
fi
echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" $KDEMENUDIR/RapidBuilderDemo >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" $GNOMEMENUDIR/RapidBuilderDemo >>"$INSTALL_PATH/$INSTALL_PKG/Uninstall"
#if a link was created in $KDE2DIR then delete
if [ $ISKDE2DIR -eq 1 ]
then
  echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" $KDE2DIR/RapidBuilderDemo >>"$INSTALL_PATH/$INSTALL_PKG/Uninstall"
fi
if [ $UID -eq 0 ]
then
   echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" /etc/X11/applnk/RapidBuilderDemo >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
fi
echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" \"$HOME/rbhome\" >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
echo \"$INSTALL_PATH/$INSTALL_PKG/uninst\" \"$INSTALL_PATH/$INSTALL_PKG\" >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"
echo exit 0  >> "$INSTALL_PATH/$INSTALL_PKG/Uninstall"

chmod +x "$INSTALL_PATH/$INSTALL_PKG/Uninstall" 
echo Installation Completed Successfully
exit 0
