#!/bin/ksh -p
#
# sun_mozilla
#
# Copyright 2000-2003 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#
# ident "05/07/03 @(#)mozilla	1.121"


mozargs=""
OS_VERSION=`uname -r`
MACH_CLASS=`uname -m`
MOZ_VERSION="1.2.1"
SUN_BLD_DATE="(2003.07.21)"
SUN_BLD_NO="1.2.1_12"
SUN_BLD_MOZTAG="mozilla20030721-19:00:00"
MOZ_VERSION_STRING="Mozilla for Solaris $MOZ_VERSION $SUN_BLD_DATE Bld$SUN_BLD_NO [$SUN_BLD_MOZTAG]"

PRG=`whence $0` >/dev/null 2>&1
progname=`/usr/bin/basename $0`
proc=`/usr/bin/uname -p`
BASEDIR=`/usr/bin/dirname "$PRG"`/

# Resolve symlinks. See 4152645.
while [ -h "$PRG" ]; do
	ls=`/usr/bin/ls -ld "$PRG"`
	link=`/usr/bin/expr "$ls" : '^.*-> \(.*\)$'`
	if /usr/bin/expr "$link" : '^/' > /dev/null; then
	prg="$link"
	else
	prg="`/usr/bin/dirname $PRG`/$link"
	fi
	PRG=`whence "$prg"` > /dev/null 2>&1
	BASEDIR=`/usr/bin/dirname "$PRG"`/
done


#
# merging 4 sun_moz.* scripts into one;
# use RUNMODE to indicate running mode
# moz only 0; debug 1
# 
MOZ=0
MOZDEBUG=1
RUNMODE=0


setup_basic_run_env(){
	MOZLANG="en-US";
	MOZREGION="US";

	L10MOZCRIPT="$BASEDIR/scripts/mozlocale-config.$LANG.sh"

	if [ -x "$L10MOZCRIPT" ]; then
		MOZLANG=`$L10MOZCRIPT -UILocale`;
		MOZREGION=`$L10MOZCRIPT -contentLocale`;
	fi
	for i in "$@"
	do
		if [ "$i" = "-UILocale" ]; then
			MOZLANG=""
		fi
                if [ "$i" = "-contentLocale" ]; then
                        MOZREGION=""
                fi
	done
        if [ ! -z "$MOZREGION" ]; then
                mozargs="-contentLocale $MOZREGION $mozargs"
        fi
	if [ ! -z "$MOZLANG" ]; then
		mozargs="-UILocale $MOZLANG $mozargs"
	fi

           #
       # set LANG to LC_MESSAGES
       #
       if [ "$LC_MESSAGES" = "C" ]; then
               if [ ! "$LC_MESSAGES" = "$LANG" ]; then
                       LC_MESSAGES=$LANG
                       export LC_MESSAGES
               fi;
       fi;	
	#
	# set up basic env
	#
	LIBDISTDIR=$BASEDIR/dist/lib
	ARCH=`uname -p`
	JDKHOME=$BASEDIR/java
	if [ -n "$LD_LIBRARY_PATH" ]; then
		LD_LIBRARY_PATH=$JDKHOME/lib/$ARCH/client:$JDKHOME/lib/$ARCH:/usr/lib:$LD_LIBRARY_PATH
	else	
		LD_LIBRARY_PATH=$JDKHOME/lib/$ARCH/client:$JDKHOME/lib/$ARCH:/usr/lib
	fi
	LD_LIBRARY_PATH=$JDKHOME/lib:$JDKHOME/lib/$ARCH/native_threads:$LD_LIBRARY_PATH
	LD_LIBRARY_PATH=$LIBDISTDIR:$LD_LIBRARY_PATH

	if [ "$MACH_CLASS" = "sun4u" ]; then
	    if [ -n "$LD_LIBRARY_PATH" ]; then
		LD_LIBRARY_PATH=$BASEDIR/sun4u:$LD_LIBRARY_PATH
	    else
		LD_LIBRARY_PATH=$BASEDIR/sun4u
	    fi
	fi
	PATH=$BASEDIR/java/bin:$PATH
	PATH=/bin:$PATH
	export LD_LIBRARY_PATH
	export PATH
}


print_extra_help_info(){
    echo "\t-composer \t Start with editor. Same as -edit"
    echo "\t-messenger \t Start with mail. Same as -mail"
    echo "\t-discussions \t Start with mail. Same as -news"
    echo "\t-debug \t Show detail debugging information"
    echo "\t-sunVersion \t Show Sun Mozilla Version information"
}

##support xremote see bug 4848430
check_running() {
    $BASEDIR/.sun_moz -remote 'ping()' 2>/dev/null >/dev/null
    RETURN_VAL=$?
    if [ "$RETURN_VAL" -eq "2" ]; then
      echo 0
      return 0
    else
      echo 1
      return 1
    fi
}

for arg
do
    
    case "$arg" in 
    -debug) 
	#echo "*** debug"
	RUNMODE=`expr $RUNMODE + $MOZDEBUG`
    ;;
    -help | -h)
	setup_basic_run_env ${1+"$@"}
	$BASEDIR/.sun_moz $mozargs ${1+"$@"}
	print_extra_help_info
	exit
    ;;
    -version | -v)
	setup_basic_run_env ${1+"$@"}
	$BASEDIR/.sun_moz $mozargs ${1+"$@"}
	exit
    ;;
    -composer)
	mozargs="$mozargs -edit"
    ;;
    -messenger)
	mozargs="$mozargs -mail"
    ;;
    -discussions)
	mozargs="$mozargs -news"
    ;;
    #By default on unix, 
    #the splashscreen would not appear before launching.
    #-no-about-splash)
	#mozargs="$mozargs -splash"
    #;;
    -display)
	mozargs="$mozargs --display"
    ;;
    -CreateProfile)
        setup_basic_run_env ${1+"$@"}
        $BASEDIR/.sun_moz $mozargs ${1+"$@"}
        exit
    ;;
    -sunVersion)
	echo "Sun Mozilla $MOZ_VERSION"
	echo "Build Date: $SUN_BLD_DATE"
	echo "Build: $SUN_BLD_NO"
	echo "Mozilla Tag: $SUN_BLD_MOZTAG"
        exit
    ;;
    esac
done

#echo "RUNMODE=" $RUNMODE


run_mozilla () {

	#echo "start calling run_mozilla"
	#
	# print out running mode
	#	
	case "$RUNMODE" in
	$MOZDEBUG)
	    echo "------- Running Mozilla $MOZ_VERSION debug mode-------"
	    ;;
	esac

	#
	# Print out some system information
	#
	if [ "$RUNMODE" = "$MOZDEBUG" ]; then

	    $BASEDIR/debuginfo.sh
	    $BASEDIR/checkpatches.sh
    	fi;

        case "$OS_VERSION" in
        "5.8" | "5.9" | "5.10") ;;
         *)
		echo ""
		echo "Mozilla $MOZ_VERSION requires Solaris 8 or later"
		echo ""
		exit
                ;;
        esac

	
	setup_basic_run_env ${1+"$@"}

# check to see if there's an already running instance or not
ALREADY_RUNNING=`check_running`

if [ "$ALREADY_RUNNING" -eq "1" ]; then
  if [ -z "$*" ]; then
    exec $BASEDIR/.sun_moz -remote "xfeDoCommand(openBrowser)"
  elif [ $# -eq 1 ]; then
    case "$1" in
      -mail)
        exec $BASEDIR/.sun_moz -remote "xfeDoCommand(openInbox)" \
             2>/dev/null >/dev/null
        ;;
      -compose)
        exec $BASEDIR/.sun_moz -remote "xfeDoCommand(composeMessage)" \
             2>/dev/null >/dev/null
        ;;
      -*)
        ;;
      *)
        exec $BASEDIR/.sun_moz -remote "openURL($1, new-window)" \
             2>/dev/null >/dev/null
        ;;
    esac
  fi
fi

	(if [ "$RUNMODE" = "$MOZDEBUG" ]; then \
	    $BASEDIR/.sun_moz $mozargs ${1+"$@"} ; \
	else
	    $BASEDIR/.sun_moz $mozargs ${1+"$@"} >/dev/null 2>/dev/null ;\
	fi; )
 	
}

if ( test -d $HOME/.mozilla ) then
	if ( test ! -f $HOME/.mozilla/.version ) then
	    echo ""
	    echo "WARNING: If you have used a previous version of Mozilla or "
	    echo "mozilla, you may experience problems due to incompatible preferences. "
	    echo "It is recommended that you move your existing $HOME/.mozilla "
	    echo "directory aside (e.g. % mv $HOME/.mozilla $HOME/.mozilla-old) "
	    echo "and then restart Mozilla $MOZ_VERSION."
	    echo ""
            echo "Additional information can be found in the Release Notes located at http://docs.sun.com"  
	    echo "Unknown" > $HOME/.mozilla/.version
	fi
	run_mozilla ${1+"$@"}
else


	if ( test -f $BASEDIR/license/locale/$LANG/LicensePopup ) then
		$BASEDIR/license/locale/$LANG/LicensePopup
		testnewflag=$?

	else
		$BASEDIR/license/locale/C/LicensePopup
		testnewflag=$?
	fi

	if [ "$testnewflag" = "0" ]; then

####################For interface declaration#########
if ( test -f $BASEDIR/interface/locale/$LANG/InterfacePopup ) then
             $BASEDIR/interface/locale/$LANG/InterfacePopup
else
             $BASEDIR/interface/locale/C/InterfacePopup
fi;
######################################################

 #################################################
##add for mail migration

 PREF_FILE=$HOME/.netscape/preferences.js
if ( test -f $PREF_FILE ) then

    sed '/^user_pref("mail.migration.copyMailFiles", .*);/d' "$PREF_FILE" > "$PREF_FILE.$$"
    mv -f "$PREF_FILE".$$  $PREF_FILE

    PREF_STRING=`grep 'pref("mail.migration.copyMailFilesPopupWindow", false);' "$BASEDIR/defaults/pref/mailnews.js"`

  if [ "$PREF_STRING" ];  then
   echo ""
else
   if ( test -f $BASEDIR/migration/locale/$LANG/CopymailPopup ) then
                $BASEDIR/migration/locale/$LANG/CopymailPopup
                replyflag=$?
   else
                $BASEDIR/migration/locale/C/CopymailPopup
                replyflag=$?
   fi;

   if [ "$replyflag" = "0" ]; then

   cat >>$HOME/.netscape/preferences.js<<EOF
user_pref("mail.migration.copyMailFiles", true);
EOF
  else
  cat >>$HOME/.netscape/preferences.js<<EOF
user_pref("mail.migration.copyMailFiles", false);
EOF
   fi;
 fi;

fi;
################################################

		mkdir -m 700 $HOME/.mozilla
		echo "$MOZ_VERSION_STRING" > $HOME/.mozilla/.version
		run_mozilla ${1+"$@"}
	else
		echo " You have not accepted the Binary Software Evalution Agreement. Exiting"
		echo ""
		exit 1
	fi;
fi

