#!/bin/bash
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

## 
## Usage:
##
## $ mozilla [args]
##
## This script is meant to run the application binary from mozilla/dist/bin.
##
## The script will setup all the environment voodoo needed to make
## the application binary to work.
##

#uncomment for debugging
#set -x

moz_libdir=/usr/lib64/moblin-web-browser-3.6

# Use run-mozilla.sh in the current dir if it exists
# If not, then start resolving symlinks until we find run-mozilla.sh
found=0
progname="$0"
curdir=`dirname "$progname"`
progbase=`basename "$progname"`
run_moz="$curdir/run-mozilla.sh"
profiles_root="$HOME/.mozilla/moblin-web-browser"
profiles_ini="${profiles_root}/profiles.ini"
profile_moblin_default="moblin.default"
profile_moblin="$profiles_root/$profile_moblin_default"
profile_moblin_2_0="$HOME/.moblin-web-browser"
browser_profile=""
browser_lc="en-US"

# Readahead before the browser startup
if [ "x$1" = "x-readahead" ]
then
  [ -x /sbin/sreadahead ] && \
  [ -f $profiles_root/breadahead.packed ] && \
    /sbin/sreadahead $profiles_root/breadahead.packed >& /dev/null
  exit 0
fi

if test -x "$run_moz"; then
  dist_bin="$curdir"
  found=1
else
  here=`/bin/pwd`
  while [ -h "$progname" ]; do
    bn=`basename "$progname"`
    cd `dirname "$progname"`
    # Resolve symlink of dirname
    cd `/bin/pwd`
    progname=`/bin/ls -l "$bn" | sed -e 's/^.* -> //' `
    progbase=`basename "$progname"`
    if [ ! -x "$progname" ]; then
      break
    fi
    curdir=`dirname "$progname"`
    run_moz="$curdir/run-mozilla.sh"
    if [ -x "$run_moz" ]; then
      cd "$curdir"
      dist_bin=`/bin/pwd`
      run_moz="$dist_bin/run-mozilla.sh"
      found=1
      break
    fi
  done
  cd "$here"
fi
if [ $found = 0 ]; then
  # Check default compile-time libdir
  if [ -x "$moz_libdir/run-mozilla.sh" ]; then
    dist_bin="$moz_libdir"
    run_moz="$moz_libdir/run-mozilla.sh"
    curdir=$moz_libdir
  else
    echo "Cannot find moblin-web-browser runtime directory. Exiting."
    exit 1
  fi
fi

langpack_path=$(cd $curdir && echo $PWD)/lang

# set var browser_lc
set_browser_lc()
{
  if [ "x$1" = "x" ]; then return 0; fi

  local lc=${1%%.*}; lc=${lc%%@*}

  if [ -e "$langpack_path/langpack-${lc/_/-}@firefox.mozilla.org" ]
  then
    browser_lc="${lc/_/-}"
  elif [ -e "$langpack_path/langpack-${lc%%_*}@firefox.mozilla.org" ]
  then
    browser_lc="${lc%%_*}"
  fi

  return 0;
}

# create profiles.ini file
create_profiles_ini()
{
  mkdir -p $profile_moblin
  cat <<EOF > $profiles_ini
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=$profile_moblin_default
Default=1
EOF
}

# set var browser_profile
set_profile_path()
{
  if [ -e "$profiles_ini" ]
  then
    local path="$profile_moblin_default"
    local default="no"
    local relative="yes"

    while read line;
    do
      case "$line" in
        [Profile*)
          if [ "x$default" = "xyes" ]
          then 
            break; 
          fi
          ;;
        Path=*)
          path="${line#Path=}"
          ;;
        Default=1)
          default="yes"
          ;;
        IsRelative=1)
          relative="yes"
          ;;
        *)
          ;;
      esac
    done < $profiles_ini

    if [ x"$relative" = "xyes" ]
    then
      browser_profile="$profiles_root/${path}"
    elif [ expr match ${path} "/" ]
    then
      browser_profile=${path}
    fi
  else
    create_profiles_ini
    browser_profile="$profile_moblin"
    mkdir -p ${browser_profile}/extensions
  fi
}

script_args=""
debugging=0
MOZILLA_BIN="${progbase}-bin"

if [ "$OSTYPE" = "beos" ]; then
  mimeset -F "$MOZILLA_BIN"
fi

pass_arg_count=0
while [ $# -gt $pass_arg_count ]
do
  case "$1" in
    -p | --pure | -pure)
      MOZILLA_BIN="${MOZILLA_BIN}.pure"
      shift
      ;;
    -g | --debug)
      script_args="$script_args -g"
      debugging=1
      shift
      ;;
    -d | --debugger)
      script_args="$script_args -d $2"
      shift 2
      ;;
    *)
      # Move the unrecognized argument to the end of the list.
      arg="$1"
      shift
      set -- "$@" "$arg"
      pass_arg_count=`expr $pass_arg_count + 1`
      ;;
  esac
done

# Migrate data from the clutter based Moblin web browser
if [ -d "$profile_moblin_2_0" ] && [ ! -d "$profiles_root" ]
then
  create_profiles_ini
  cp -a $profile_moblin_2_0/* $profile_moblin/
  rm -fR $profile_moblin/Cache
  rm -f $profile_moblin/*lock
fi

set_browser_lc $LANG
set_profile_path

if [ -e "$langpack_path" ]
then
  langpack="langpack-${browser_lc}@firefox.mozilla.org"

  if [ "x$browser_profile" != "x" ]
  then
    langext="$browser_profile/extensions/$langpack"
    for oldlangext in `ls -d $browser_profile/extensions/langpack-* 2>/dev/null`;
    do
      if [ "x$oldlangext" != "x$langext" ] && [ -L "$oldlangext" ]
      then
        # Dont load unused langpark
        unlink $oldlangext
      fi
    done
    if [ ! -e "$langext" ] && [ "x$browser_lc" != "xen-US" ]
    then
      ln -s $langpack_path/$langpack $langext
    fi
  fi
fi

[ -x $curdir/breadahead.sh ] && \
  $curdir/breadahead.sh $(cd $curdir && echo $PWD)  \
                        $browser_profile \
                        /usr/lib/mozilla/plugins \
                        $HOME/.mozilla/plugins >& /dev/null

locale=$browser_lc
if [ $debugging = 1 ]
then
  echo $dist_bin/run-mozilla.sh $script_args $dist_bin/$MOZILLA_BIN  -UILocale $locale "$@"
fi
"$dist_bin/run-mozilla.sh" $script_args "$dist_bin/$MOZILLA_BIN" -UILocale $locale "$@"
exitcode=$?

exit $exitcode
# EOF.
