#!/bin/bash

#
# Install Builder Script for Flock
#

# ------------------------------------------------------------------------
# Defines
# ------------------------------------------------------------------------

  if [ $(expr substr `uname` 1 6) = "CYGWIN" ]; 
   then
    CREATEDIR="mkdir -p"
    MOZILLABUILD="/cygdrive/c/mozilla-build"
   else
    CREATEDIR="nsinstall -D"
    MOZILLABUILD="/c/mozilla-build"
  fi
  RELEASEDIR="$o/release-installer"
  INSTALLERBASE="$m/flock/installer/windows"
  EDITION_def="Flock"
  LION_def="en-US"
  AUTOMATIC="no"
  BRANDING="$m/other-licenses/branding"
  EXENAME="flock.installer.exe"
  SOURCE_DIR="$o/dist/bin"
  INSTALLTEMP="$RELEASEDIR/temp"
  INSTALLNSIS="$RELEASEDIR/installer"
  MY7Z="$MOZILLABUILD/7zip/7z.exe"
  NSIS="$MOZILLABUILD/nsis-2.22/makensis.exe"
  ERROR=0


# ------------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------------

  function sanitycheck ()
  {
    printf ":: checking for $1...";
    if [ ! -e $2 ]
      then 
       printf "not found\n"
       ERROR=1
       exit 0
      else
       printf "found [$2]\n"
      fi
  }

  function cleanfiles ()
  {
    printf ":: cleaning up temporary files..."
    rm -fr $INSTALLTEMP/*
    rm -rf $RELEASEDIR/app.7z
    rm -rf $INSTALLNSIS/
    printf "done!\n"

  }

  function callhelp
  {
      printf "\nUsage: $0 [option] 

  --edition EDITION, uses the folder under branding/ (ie Flock)
    *must be set
  --LION LANG, uses the folder under EDITION, ie (en-US)
  --sourcedir SOURCE, where to find the compiled flock.exe
  --outfile OUTFILE, destination filename, minus suffix
    will end up in the RELEASEDIR folder
  --releasedir RELEASEDIR, destination folder for compiled
    installer, will also be used for temp files.
  --help, this page.   
    "
    exit 0
  }

# ------------------------------------------------------------------------
# Script body
# ------------------------------------------------------------------------


  if [ "$#" = "0" ]
   then
     callhelp
   fi
  
  while [ ! -z "$*" ] ; do
   case $1 in
  
    "--clean") 
    cleanfiles
    exit 0
    ;;
    
    "" | "help" | "-h" | "--help" | "/?")
    callhelp
    ;;
    
    "--sourcedir")
      SOURCE_DIR="$2"
      shift 2
    ;;
    
    "--edition")
      TEMP1=`echo $2 | sed -e 's/\//\-/g' | tr [a-z] [A-Z]`
      TEMP2=`echo $2 | sed -e 's/\//\-/g' | tr [A-Z] [a-z]`
      EDITION="$BRANDING/${TEMP1:0:1}${TEMP2:1}"
      shift 2
    ;;
    
    "--LION")
      LION="$2"
      shift 2
    ;;
    
    "--outfile")
      EXENAME=`echo $2.exe | sed -e 's/\//\-/g' | tr [A-Z] [a-z]`
      shift 2
    ;;

    "--releasedir")
      RELEASEDIR="$2"
      shift 2
    ;;
    
    "--automatic")
      # search for edition and LIONuage packs.
      AUTOMATIC="yes"
      shift
    ;;  
    
    *) # required, or an invalid command will stop this process and appear to hang.
     shift
    ;;
   esac
  done;  

  LOGFILE="build"
  if [ ! $LION ]; then
   LION="$BRANDING/$EDITION_def/$LION_def"
  fi
  if [ "$AUTOMATIC" == "yes" ]; then
  
    TEMP1=""
    TEMP1=`cat $m/.mozconfig | grep -- --with-fbranding-dir= | sed 's/#.*$//;s/^.*=//;s/ .*$//'`
    if [ "$TEMP1"  != "" ]; then
      sanitycheck "automatic edition [$BRANDING/$TEMP1]" "$BRANDING/$TEMP1"
      EDITION="$TEMP1"
    else
      EDITION="$BRANDING/$EDITION_def"
    fi 
    
    TEMP1=""   
    TEMP1=`cat $m/.mozconfig | grep -- --enable-ui-locale= | sed 's/#.*$//;s/^.*=//;s/ .*$//'`
#    if [ `echo ${#TEMP1}` -eq 5 ]; then
#      if [ -d "$EDITION/$TEMP1" ]; then
#        LION="$EDITION/$TEMP1"
#      elif [ -d "$m/../l10n/$TEMP1" ]; then
#        LION="$m/../l10n/$TEMP1"
#      else 
      LION="$BRANDING/$EDITION_def/$LION_def"
#      fi
#    fi  
  fi
  
  sanitycheck "edition branding folder" "$EDITION"  
  sanitycheck "l10n files [$LION]" "$LION"

  printf ":: logging to \n::    $RELEASEDIR/$LOGFILE.log\n"
  
  sanitycheck "compiled source files" $SOURCE_DIR/flock.exe
  printf ":: using source dir \n::    $SOURCE_DIR\n"   
  sanitycheck "installer scripts" $INSTALLERBASE/installer.nsi
  sanitycheck "7zip" $MY7Z
  sanitycheck "NSIS installer" $NSIS

  cleanfiles
  
  $CREATEDIR $RELEASEDIR
  $CREATEDIR $INSTALLNSIS  
  $CREATEDIR $INSTALLTEMP
  
  printf "::\n:: starting build process.\n::\n"

  cp $INSTALLERBASE/*.* $INSTALLNSIS/.
  cp $LION/* $INSTALLNSIS/.

  
  cd $o/browser/installer
  make removed-files
  perl $m/toolkit/mozapps/installer/windows/nsis/make-installremoves.pl  removed-files > removed-files.log
  cp removed-files.log $INSTALLTEMP/removed-files.log

  cd $RELEASEDIR
  printf ":: copying dist files to \n::    $INSTALLTEMP..."
  $CREATEDIR $INSTALLTEMP/nonlocalized
  cp -rp  $SOURCE_DIR/* $INSTALLTEMP/nonlocalized/.
  printf "done\n\n"

  printf ":: copying yahootoolbar.xpi to  \n::    $INSTALLTEMP/nonlocalized/..."
  cp $m/other-licenses/bundled/yahootoolbar.xpi $INSTALLTEMP/nonlocalized/yahootoolbar.xpi

  printf ":: looking for branding modification script branding.sh in \n::    $LION..."
   if [ ! -e "$LION/branding.sh" ]
   then 
    printf "not found\n"
   else
    printf "found, calling script to apply patches.\n"
    call "$LION/branding.sh"
   fi
  
  if [ -f "$FBUILD_HOME/lib/build-Win32.sh" ] ; then
     . "$FBUILD_HOME/lib/build-Win32.sh"
  fi

  cd $INSTALLNSIS
  CURRENTVER=`cat $m/flock/config/version.txt`
  mv defines.nsi defines.nsi.in
  sed  -e "s/%VERSION/$CURRENTVER/g" defines.nsi.in > defines.nsi
  printf ":: building license..."
  $NSIS license.nsi  >> $RELEASEDIR/$LOGFILE.log
  printf "done, saved as license.exe in \n::    $INSTALLNSIS\n\n"

  printf ":: building installer..."
  $NSIS installer.nsi  >> $RELEASEDIR/$LOGFILE.log
  sign_code $INSTALLNSIS/setup.exe
  cp -rpv $INSTALLNSIS/setup.exe $INSTALLTEMP/ >> $RELEASEDIR/$LOGFILE.log
  printf "done, saved as setup.exe in \n::    $INSTALLTEMP\n\n"

  printf ":: building uninstaller...\n"
  $NSIS uninstaller.nsi >> $RELEASEDIR/$LOGFILE.log
  $CREATEDIR $INSTALLTEMP/nonlocalized/uninstall/
  sign_code $INSTALLNSIS/helper.exe
  cp -rpv $INSTALLNSIS/helper.exe $INSTALLTEMP/nonlocalized/uninstall/. >> $RELEASEDIR/$LOGFILE.log
  printf "done, saved as helper.exe in \n::    $INSTALLTEMP/nonlocalized/uninstall\n\n"
  
  cp -rpv $m/other-licenses/bundled/install_flash_player.exe $INSTALLTEMP/nonlocalized/ >> $RELEASEDIR/$LOGFILE.log
  printf ":: copied flash player into \n::    $INSTALLTEMP/nonlocalized\n\n"
  
  cd $INSTALLTEMP
  printf ":: compressing files..."
  rm -f $RELEASEDIR/app.7z
  if [ $(expr substr `uname` 1 6) = "CYGWIN" ]; 
  then
    $MY7Z a -r -t7z `cygpath -w $RELEASEDIR/app.7z` >> $RELEASEDIR/$LOGFILE.log
  else
    $MY7Z a -r -t7z $RELEASEDIR/app.7z >> $RELEASEDIR/$LOGFILE.log
  fi
  printf "done, saved as app.7z in \n::    $RELEASEDIR\n\n"

  printf ":: compiling final executable..."
  cat $INSTALLNSIS/7zSD.sfx $INSTALLNSIS/app.tag $RELEASEDIR/app.7z > $RELEASEDIR/$EXENAME
  chmod 0755 $RELEASEDIR/$EXENAME
  printf "done. \n$EXENAME is at \n::    $RELEASEDIR/$EXENAME\n\n"
  sign_code $RELEASEDIR/$EXENAME
  
