############################################################################
#
# Copyright (c) 2013 - dsixda (dislam@rocketmail.com)
#
# Android Kitchen is 100% free.  This script file is intended for personal
# and/or educational use only.  It may not be duplicated for monetary
# benefit or any other purpose without the permission of the developer.
#
############################################################################

#
# This script has one mandatory argument:
# $1 = updater-script/update-script
#


if [ "$1" != "" ]
then

  if [ "$1" != "updater-script" ] && [ "$1" != "update-script" ]
  then
    echo "Error: Specified '$1' instead of updater-script or update-script"
    exit 1
  fi


  cd WORKING_*
  working_folder=`pwd`
  cd ..


  #
  # Find device name for lookup in edify_defs folder
  #
  device_name=`scripts/get_samsung_variant`

  if [ "$device_name" == "" ]
  then
    device_name=`scripts/get_device_name`

    if [ -e $working_folder/boot.img ]
    then
      scripts/check_mt65xx_bootimg
      is_mt65xx_bootimg=$?
    fi      

  else

    is_mt65xx_bootimg=0

    #
    # Check Galaxy platform type
    #

    scripts/check_galaxy_s
    is_gs=$?

    if [ "$is_gs" != "1" ]
    then
      scripts/check_galaxy_s2
      is_gs2=$? 

      if [ "$is_gs2" != "1" ]
      then
        scripts/check_galaxy_s2x
        is_gs2x=$?

        if [ "$is_gs2x" != "1" ]
        then
          scripts/check_galaxy_s3_exynos
          is_gs3_exynos=$?

          if [ "$is_gs3_exynos" == "1" ] 
          then
            is_gs3_common=1

          else
            scripts/check_galaxy_s3_common
            is_gs3_common=$?

            if [ "$is_gs3_common" != "1" ]
            then
              scripts/check_gnote2_common
              is_gnote2_common=$?

              if [ "$is_gnote2_common" != "1" ]
              then
                scripts/check_galaxy_s4_common
                is_gs4_common=$?
              fi
            fi
          fi
        fi
      fi
    fi    
  fi

  echo
  echo "Device name in kitchen, or variant of: $device_name"


  change_mnt=`scripts/get_edify_def_val $device_name change_mnt`
  change_mnt_dell=`scripts/get_edify_def_val $device_name change_mnt_dell`

  if [ "$change_mnt" == "yes" ] || [ "$change_mnt_dell" == "yes" ]
  then
    param1=`scripts/get_edify_def_val $device_name param1`
    param2=`scripts/get_edify_def_val $device_name param2`
    param1_sdcard=`scripts/get_edify_def_val $device_name param1_sdcard`
    param2_sdcard=`scripts/get_edify_def_val $device_name param2_sdcard`
    param1_cache=`scripts/get_edify_def_val $device_name param1_cache`
    param2_cache=`scripts/get_edify_def_val $device_name param2_cache`
    fix_boot=`scripts/get_edify_def_val $device_name fix_boot`
    fix_boot2=`scripts/get_edify_def_val $device_name fix_boot2`
    fix_boot3=`scripts/get_edify_def_val $device_name fix_boot3`
    use_cache_not_tmp=`scripts/get_edify_def_val $device_name use_cache_not_tmp`
    sys_mnt=`scripts/get_edify_def_val $device_name sys_mnt`
    cache_mnt=`scripts/get_edify_def_val $device_name cache_mnt`
    boot_mnt=`scripts/get_edify_def_val $device_name boot_mnt`
    data_mnt=`scripts/get_edify_def_val $device_name data_mnt`
    sdcard_mnt=`scripts/get_edify_def_val $device_name sdcard_mnt`
    webtop_mnt=`scripts/get_edify_def_val $device_name webtop_mnt`
    modem_mnt=`scripts/get_edify_def_val $device_name modem_mnt`
    preload_mnt=`scripts/get_edify_def_val $device_name preload_mnt`
  fi

  cd WORKING_*
  cd META-INF/com/google/android


  if [ "$change_mnt" == "yes" ]
  then

    if [ "$param1_cache" == "UNKNOWN" ]
    then
      param1_cache=$param1
    fi
    if [ "$param2_cache" == "UNKNOWN" ]
    then
      param2_cache=$param2
    fi
    if [ "$param1_sdcard" == "UNKNOWN" ]
    then
      param1_sdcard=$param1
    fi
    if [ "$param2_sdcard" == "UNKNOWN" ]
    then
      param2_sdcard=$param2
    fi


    #################################
    # change_mnt==yes; updater-script
    #################################

    if [ "$1" == "updater-script" ]
    then
      sed -i \
        -e 's/format("MTD", "system")/format("'"$param1"'", "'"$param2"'", "'"$sys_mnt"'")/g' \
        -e 's/format("MTD", "preload")/format("'"$param1"'", "'"$param2"'", "'"$preload_mnt"'")/g' \
        -e 's/format("MTD", "cache")/format("'"$param1_cache"'", "'"$param2_cache"'", "'"$cache_mnt"'")/g' \
        -e 's/format("MTD", "boot")/format("'"$param1"'", "'"$param2"'", "'"$boot_mnt"'")/g' \
        -e 's/format("MTD", "userdata")/format("'"$param1"'", "'"$param2"'", "'"$data_mnt"'")/g' \
        \
        -e 's/format("'"$param1"'", "MTD", "system")/format("'"$param1"'", "'"$sys_mnt"'")/g' \
        -e 's/format("'"$param1"'", "MTD", "preload")/format("'"$param1"'", "'"$preload_mnt"'")/g' \
        -e 's/format("'"$param1_cache"'", "MTD", "cache")/format("'"$param1_cache"'", "'"$cache_mnt"'")/g' \
        -e 's/format("'"$param1"'", "MTD", "boot")/format("'"$param1"'", "'"$boot_mnt"'")/g' \
        -e 's/format("'"$param1"'", "MTD", "userdata")/format("'"$param1"'", "'"$data_mnt"'")/g' \
        \
        -e 's/mount("MTD", "system"/mount("'"$param1"'", "'"$param2"'", "'"$sys_mnt"'"/g' \
        -e 's/mount("MTD", "preload"/mount("'"$param1"'", "'"$param2"'", "'"$preload_mnt"'"/g' \
        -e 's/mount("MTD", "cache"/mount("'"$param1_cache"'", "'"$param2_cache"'", "'"$cache_mnt"'"/g' \
        -e 's/mount("MTD", "boot"/mount("'"$param1"'", "'"$param2"'", "'"$boot_mnt"'"/g' \
        -e 's/mount("MTD", "userdata"/mount("'"$param1"'", "'"$param2"'", "'"$data_mnt"'"/g' \
        -e 's/mount("MTD", "sdcard"/mount("'"$param1_sdcard"'", "'"$param2_sdcard"'", "'"$sdcard_mnt"'"/g' \
        \
        -e 's/mount("'"$param1"'", "MTD", "system"/mount("'"$param1"'", "'"$sys_mnt"'"/g' \
        -e 's/mount("'"$param1"'", "MTD", "preload"/mount("'"$param1"'", "'"$preload_mnt"'"/g' \
        -e 's/mount("'"$param1_cache"'", "MTD", "cache"/mount("'"$param1_cache"'", "'"$cache_mnt"'"/g' \
        -e 's/mount("'"$param1"'", "MTD", "boot"/mount("'"$param1"'", "'"$boot_mnt"'"/g' \
        -e 's/mount("'"$param1"'", "MTD", "userdata"/mount("'"$param1"'", "'"$data_mnt"'"/g' \
        -e 's/mount("'"$param1_sdcard"'", "MTD", "sdcard"/mount("'"$param1_sdcard"'", "'"$sdcard_mnt"'"/g' \
        \
        -e 's/\('"$webtop_mnt"'", "\)???"/\1\/webtop"/g' \
        updater-script


      if [ `grep -c "^mount.*webtop" updater-script` -gt 0 ]
      then
        echo "unmount(\"/webtop\");" >> updater-script
      fi

      #
      # Certain devices use/require a special way of flashing the boot.img
      #
      # NOTE: :a;N;$!ba; = reads multi-line pattern as one-line pattern
      #

      if [ -e updater-script.orig ] && [ "$fix_boot" != "yes" ]
      then
        grep_boot=`grep -c "^package_extract_file(\"boot.img\", \"$boot_mnt\");" updater-script.orig`
        if [ $grep_boot != 0 ]
        then
          fix_boot=yes
        fi
      fi

      if [ "$fix_boot" == "yes" ]
      then
        sed -i -e ':a;N;$!ba;s/assert(package_extract_file("boot.img", "\/tmp\/boot.img"),\n[ ]*write_raw_image("\/tmp\/boot.img", "boot"),\n[ ]*delete("\/tmp\/boot.img"));/package_extract_file("boot.img", "'"$boot_mnt"'");/g' updater-script
      
      elif [ "$fix_boot2" == "yes" ]
      then
        sed -i -e 's/write_raw_image("\/tmp\/boot.img", "boot")/write_raw_image("\/tmp\/boot.img", "'"$boot_mnt"'")/g' updater-script

      elif [ "$fix_boot3" == "yes" ]
      then
        sed -i -e 's/write_raw_image("\/tmp\/boot.img", "boot")/run_program("\/sbin\/busybox", "dd", "if=\/tmp\/boot.img", "of='"$boot_mnt"'")/g' updater-script
      fi



      #
      # Fix cases where some formats/mounts have different numbers of args in the same file
      #
      # e.g.  format ext3 SYSTEM:
      #       format DATA:
      #

      mount_four_args=`grep "^mount" updater-script | grep -c $param2`
      if [ "$param2_cache" != "$param2" ]
      then
        mount_four_args2=`grep "^mount" updater-script | grep -c $param2_cache`
      else
        mount_four_args2=0
      fi
      if [ "$param2_sdcard" != "$param2" ]
      then
        mount_four_args3=`grep "^mount" updater-script | grep -c $param2_sdcard`
      else
        mount_four_args3=0
      fi
      mount_four_args_total=$(($mount_four_args+$mount_four_args2+$mount_four_args3))
      mount_all=`grep -c "^mount" updater-script`


      if [ $mount_all -gt $mount_four_args_total ] && [ $mount_four_args_total -gt 0 ]
      then
        sed -i \
          -e 's/mount("'"$param1"'", "'"$param2"'",/mount("'"$param1"'",/g' \
          -e 's/mount("'"$param1_cache"'", "'"$param2_cache"'",/mount("'"$param1_cache"'",/g' \
          -e 's/mount("'"$param1_sdcard"'", "'"$param2_sdcard"'",/mount("'"$param1_sdcard"'",/g' \
        updater-script
      fi


      fmt_three_args=`grep "^format" updater-script | grep -c $param2`
      if [ "$param2_cache" != "$param2" ]
      then
        fmt_three_args2=`grep "^format" updater-script | grep -c $param2_cache`
      else
        fmt_three_args2=0
      fi
      
      fmt_three_args_total=$(($fmt_three_args+$fmt_three_args2))
      fmt_all=`grep -c "^format" updater-script`

      if [ $fmt_all -gt $fmt_three_args ] && [ $fmt_three_args -gt 0 ]
      then
        sed -i -e 's/format("'"$param1"'", "'"$param2"'",/format("'"$param1"'",/g' updater-script
        sed -i -e 's/format("'"$param1_cache"'", "'"$param2_cache"'",/format("'"$param1_cache"'",/g' updater-script
      fi


      #
      # Cover the case where the ROM uses a different filesystem from what was defined
      # above (for both 'format' and 'mount')
      #
      # e.g. updater-script uses ext4 in CyanogenMod instead of the expected ext3
      #

      if [ -e updater-script.orig ]
      then
        param1_old=`grep -m 1 "mount(.*$sys_mnt" updater-script.orig | \
                    sed -e 's/mount("\([^"]*\).*/\1/g'`
        param2_old=`grep -m 1 "mount(.*$sys_mnt" updater-script.orig | \
                    sed -e 's/mount("[^"]*"[ ]*,[ ]*"\([^"]*\).*/\1/g'`
        
        if [ "$param1_old" != "" ] && [ "$param2_old" != "" ]
        then
          if [ "$param1_old" != "$param1" ]
          then
            sed -i -e 's/'"$param1"'/'"$param1_old"'/g' updater-script 
          fi
          if [ "$param2_old" != "$param2" ]
          then
            sed -i -e 's/'"$param2"'/'"$param2_old"'/g' updater-script
          fi
        fi


        #
        # Check if the updater-script uses /cache instead of /tmp for run_program
        # or writing images.
        #
        
        if [ `grep -c "/cache/boot.img" updater-script.orig` -gt 0 ] && \
            [ `grep -c "\"/tmp/" updater-script` -gt 0 ]
        then
          use_cache_not_tmp=yes
        elif [ `grep -c "\"/tmp/" updater-script` -gt 0 ]
        then
          use_cache_not_tmp=no
        fi
      fi

      #
      # use_cache_not_tmp can be set to 'yes' in either edify_defs file or from updater-script.orig
      #
      if [ "$use_cache_not_tmp" == "yes" ]
      then
        sed -i \
          -e 's/\/tmp\//\/cache\//g' \
          -e 's/\("\/cache\/boot.img", \)"boot"/\1"'"$boot_mnt"'"/g' \
          updater-script
      fi


      #
      # Fix case where a shell script was used for mounting instead of the 'mount' command
      #
      if [ `grep -c "mount(\".*$sys_mnt\", \".*\"" updater-script` -gt 0 ]
      then
        if [ `grep -c "auto \\$PARTITION \\$ANDROID_ROOT" updater-script` -gt 0 ]
        then
          sed -i -e '/mount(".*'"$sys_mnt"'", ".*"/d' updater-script
        fi
      fi

      if [ `grep -c "mount(\".*$data_mnt\", \".*\"" updater-script` -gt 0 ]
      then
        if [ `grep -c "auto \\$PARTITION \\$ANDROID_DATA" updater-script` -gt 0 ]
        then
          sed -i -e '/mount(".*'"$data_mnt"'", ".*"/d' updater-script
        fi
      fi

      mnt_list=( $data_mnt $sys_mnt $cache_mnt $boot_mnt $sdcard_mnt )
      
      for mnt in ${mnt_list[@]}
      do
        if [ `grep -c "mount(\".*$mnt\", \".*\""  updater-script` -gt 0 ]
        then
          if [ `grep -c "mount $mnt" updater-script` -gt 0 ]
          then
            sed -i -e '/mount(".*'"$mnt"'", ".*"/d' updater-script
          fi
        fi
      done


      #
      # Alternative mount/unmount commands
      #

      use_sbin_mnt=0
      use_sbin_bb=0

      if [ -e updater-script.orig ]
      then
        grep_sbin_mnt=`grep -c "run_program(\"/sbin/mount\"" updater-script.orig`
        if [ $grep_sbin_mnt -gt 0 ]
        then
          use_sbin_mnt=1
        fi

        grep_sbin_bb=`grep -c "run_program(\"/sbin/busybox\", \"mount\"" updater-script.orig`
        if [ $grep_sbin_bb -gt 0 ]
        then
          use_sbin_bb=1
        fi

      else
    
        #
        # For Samsung Galaxy S2 and equivalent
        #
        if [ "$is_gs" == "1" ] || [ "$is_gs2" == "1" ] || [ "$is_gs2x" == "1" ]
        then
          use_sbin_mnt=1
        fi
      fi


      if [ $use_sbin_mnt == 1 ]
      then
        sed -i \
          -e 's/^mount("[^"]*", "[^"]*", \("[^"]*"\), \("[^"]*"\))/run_program("\/sbin\/mount", \1, \2)/g' \
          -e 's/^unmount(\("[^"]*"\))/run_program("\/sbin\/umount", \1)/g' \
        updater-script
      fi

      if [ $use_sbin_bb == 1 ]
      then
        sed -i \
          -e 's/^mount("[^"]*", "[^"]*", "[^"]*", \("[^"]*"\))/run_program("\/sbin\/busybox", "mount", \1)/g' \
          -e 's/^unmount(\("[^"]*"\))/run_program("\/sbin\/busybox", "umount", \1)/g' \
        updater-script
      fi


      #
      # MT65XX EMCC-based devices use "bootimg" rather than boot
      #        

      if [ "$is_mt65xx_bootimg" == "1" ]
      then
        sed -i \
          -e 's/boot.img\", \"boot\"),/boot.img\", \"bootimg\"),/g' \
        updater-script
      fi


      ###############################
      # Samsung zImage and modem.bin   
      ###############################

      if [ "$is_gs2" == "1" ] 
      then

        ##############################
        # Samsung Galaxy S2 (original)
        ##############################

        if [ `grep -c flash_zimage updater-script` -gt 0 ] && [ `grep -c flash_modem updater-script` -gt 0 ]
        then

          sed -i \
            -e '/^flash_zimage[ ]*$/d' \
            -e 's/^flash_modem[ ]*$/package_extract_file("flash_image", "\/tmp\/flash_image");\nset_perm(0, 0, 0777, "\/tmp\/flash_image");\nassert(package_extract_file("zImage", "\/tmp\/zImage"),\n       run_program("\/tmp\/flash_image", "'"$boot_mnt"'", "\/tmp\/zImage"),\n       delete("\/tmp\/zImage"));\nassert(package_extract_file("modem.bin", "\/tmp\/modem.bin"),\n       run_program("\/tmp\/flash_image", "'"$modem_mnt"'", "\/tmp\/modem.bin"),\n       delete("\/tmp\/modem.bin"));\ndelete("\/tmp\/flash_image");\n/g' \
            updater-script

        elif [ `grep -c flash_zimage updater-script` -gt 0 ] && [ `grep -c flash_modem updater-script` == 0 ]
        then

          sed -i -e 's/^flash_zimage[ ]*$/package_extract_file("flash_image", "\/tmp\/flash_image");\nset_perm(0, 0, 0777, "\/tmp\/flash_image");\nassert(package_extract_file("zImage", "\/tmp\/zImage"),\n       run_program("\/tmp\/flash_image", "'"$boot_mnt"'", "\/tmp\/zImage"),\n       delete("\/tmp\/zImage"));\ndelete("\/tmp\/flash_image");\n/g' updater-script

        elif [ `grep -c flash_zimage updater-script` == 0 ] && [ `grep -c flash_modem updater-script` -gt 0 ]
        then

          sed -i -e 's/^flash_modem[ ]*$/package_extract_file("flash_image", "\/tmp\/flash_image");\nset_perm(0, 0, 0777, "\/tmp\/flash_image");\nassert(package_extract_file("modem.bin", "\/tmp\/modem.bin"),\n       run_program("\/tmp\/flash_image", "'"$modem_mnt"'", "\/tmp\/modem.bin"),\n       delete("\/tmp\/modem.bin"));\ndelete("\/tmp\/flash_image");\n/g' updater-script
     
        fi

      elif [ "$is_gs" == "1" ]
      then

        ###################
        # Samsung Galaxy S
        ###################

        if [ `grep -c flash_zimage updater-script` -gt 0 ] && [ `grep -c flash_modem updater-script` -gt 0 ]
        then

          sed -i \
            -e '/^flash_zimage[ ]*$/d' \
            -e 's/^flash_modem[ ]*$/package_extract_file("redbend_ua", "\/tmp\/redbend_ua");\nset_perm(0, 0, 0777, "\/tmp\/redbend_ua");\nassert(package_extract_file("zImage", "\/tmp\/zImage"),\n       run_program("\/tmp\/redbend_ua", "restore", "\/tmp\/zImage", "'"$boot_mnt"'"),\n       delete("\/tmp\/zImage"));\nassert(package_extract_file("modem.bin", "\/tmp\/modem.bin"),\n       run_program("\/tmp\/redbend_ua", "restore", "\/tmp\/modem.bin", "'"$modem_mnt"'"),\n       delete("\/tmp\/modem.bin"));\ndelete("\/tmp\/redbend_ua");\n/g' \
            updater-script

        elif [ `grep -c flash_zimage updater-script` -gt 0 ] && [ `grep -c flash_modem updater-script` == 0 ]
        then

          sed -i -e 's/^flash_zimage[ ]*$/package_extract_file("redbend_ua", "\/tmp\/redbend_ua");\nset_perm(0, 0, 0777, "\/tmp\/redbend_ua");\nassert(package_extract_file("zImage", "\/tmp\/zImage"),\n       run_program("\/tmp\/redbend_ua", "restore", "\/tmp\/zImage", "'"$boot_mnt"'"),\n       delete("\/tmp\/zImage"));\ndelete("\/tmp\/redbend_ua");\n/g' updater-script

        elif [ `grep -c flash_zimage updater-script` == 0 ] && [ `grep -c flash_modem updater-script` -gt 0 ]
        then

          sed -i -e 's/^flash_modem[ ]*$/package_extract_file("redbend_ua", "\/tmp\/redbend_ua");\nset_perm(0, 0, 0777, "\/tmp\/redbend_ua");\nassert(package_extract_file("modem.bin", "\/tmp\/modem.bin"),\n       run_program("\/tmp\/redbend_ua", "restore", "\/tmp\/modem.bin", "'"$modem_mnt"'"),\n       delete("\/tmp\/modem.bin"));\ndelete("\/tmp\/redbend_ua");\n/g' updater-script
     
        fi

      elif [ "$is_gs3_exynos" == "1" ] || [ "$is_gnote2_common" == "1" ] || [ "$is_gs4_common" == "1" ]
      then

        ###############################################
        # Samsung Galaxy S3 International / Note 2 / S4
        ###############################################

        if [ `grep -c flash_modem updater-script` -gt 0 ]
        then

          sed -i -e 's/^flash_modem[ ]*$/package_extract_file("flash_image", "\/tmp\/flash_image");\nset_perm(0, 0, 0777, "\/tmp\/flash_image");\nassert(package_extract_file("modem.bin", "\/tmp\/modem.bin"),\n       run_program("\/tmp\/flash_image", "'"$modem_mnt"'", "\/tmp\/modem.bin"),\n       delete("\/tmp\/modem.bin"));\ndelete("\/tmp\/flash_image");\n/g' updater-script
     
        fi
      fi


    ################################
    # change_mnt==yes; update-script
    ################################

    elif [ "$1" == "update-script" ]
    then
      sed -i \
        -e 's/format '"$param1"' '"$param2"' '"$sys_mnt"' 0/format SYSTEM:/g' \
        -e 's/format '"$param1"' '"$param2"' '"$sys_mnt"'/format SYSTEM:/g' \
        -e 's/format '"$param1"' '"$param2"' '"$preload_mnt"'/format PRELOAD:/g' \
        -e 's/format '"$param1_cache"' '"$param2_cache"' '"$cache_mnt"'/format CACHE:/g' \
        -e 's/format '"$param1"' '"$param2"' '"$boot_mnt"'/format BOOT:/g' \
        -e 's/format '"$param1"' '"$param2"' '"$data_mnt"'/format DATA:/g' \
        \
        -e 's/format '"$param1"' '"$sys_mnt"'/format '"$param1"' SYSTEM:/g' \
        -e 's/format '"$param1"' '"$preload_mnt"'/format '"$param1"' PRELOAD:/g' \
        -e 's/format '"$param1_cache"' '"$cache_mnt"'/format '"$param1_cache"' CACHE:/g' \
        -e 's/format '"$param1"' '"$boot_mnt"'/format '"$param1"' BOOT:/g' \
        -e 's/format '"$param1"' '"$data_mnt"'/format '"$param1"' DATA:/g' \
        \
        -e 's/package_extract_file PACKAGE:boot.img '"$boot_mnt"'/write_raw_image PACKAGE:boot.img BOOT:/g' \
        -e 's/run_program PACKAGE:sbin\/busybox dd if=TMP:boot.img of='"$boot_mnt"'/write_raw_image PACKAGE:boot.img BOOT:/g' \
        update-script

      sed -i \
        -e ':a;N;$!ba;s/package_extract_file PACKAGE:boot.img TMP:boot.img\nwrite_raw_image PACKAGE:boot.img BOOT:\ndelete TMP:boot.img/write_raw_image PACKAGE:boot.img BOOT:/g' \
        update-script

      #
      # Cover the case where the ROM uses a different filesystem, such that the update-script
      # wasn't adjusted in the above code.
      #
      # e.g. update-script uses ext4 in CyanogenMod instead of the expected ext3
      #

      grep_other=`grep "format .* $sys_mnt[ ]*$" update-script`  

      if [ "$grep_other" != "" ]
      then
        sed -i \
          -e 's/format .* '"$sys_mnt"'/format SYSTEM:/g' \
          -e 's/format .* '"$preload_mnt"'/format PRELOAD:/g' \
          -e 's/format .* '"$cache_mnt"'/format CACHE:/g' \
          -e 's/format .* '"$boot_mnt"'/format BOOT:/g' \
          -e 's/format .* '"$data_mnt"'/format DATA:/g' \
          \
          update-script
      fi
    fi


  ##########################
  # For Dell
  ##########################

  elif [ "$change_mnt_dell" == "yes" ]
  then
    if [ "$1" == "updater-script" ]
    then

      sed -i \
        -e 's/mount("MTD", "system"/mount("'"$param_system"'", "system"/g' \
        -e 's/mount("MTD", "cache"/mount("'"$param_cache"'", "cache"/g' \
        -e 's/mount("MTD", "data"/mount("'"$param_data"'", "data"/g' \
        -e 's/write_raw_image("\/tmp\/boot.img/write_boot_raw_image("\/tmp\/boot.img/g' \
        updater-script
    fi
  fi

 
  cd ../../../../..

  ################################
  # All cases; update-script
  ################################

  if [ "$1" == "update-script" ]
  then

    #
    # Convert 'delete_recursive' of a partition to 'format', for compatibility 
    # with all of the scripts.  This will be changed back when you build ROM.
    #

    if [ "$is_gs" == "1" ] || [ "$is_gs2" == "1" ] || [ "$is_gs2x" == "1" ] \
      || [ "$is_gs3_common" == "1" ] || [ "$is_gs4_common" == "1" ]
    then
      cd WORKING_*
      cd META-INF/com/google/android
      sed -i -e 's/delete_recursive \([^:]*:\)[ ]*$/format \1/g' update-script
      cd ../../../../..
    fi
  fi


  ################################
  # All cases; updater-script
  ################################

  if [ "$1" == "updater-script" ]
  then

    cd WORKING_*
    cd META-INF/com/google/android

    #
    # Fix cases where a mount hasn't been fixed yet
    #
    # e.g.  If updater-script contains: 
    #         mount("MTD", "userdata", "/data");
    #       But the other mount entries in file look like:  
    #         mount("yaffs2", "MTD", ...);
    #  
    grep_list=( yaffs2 ext3 )

    for str in ${grep_list[@]}
    do
      grep_result=`grep -c "^mount(\"$str\"" updater-script`

      if [ $grep_result -gt 0 ]
      then
        sed -i \
          -e 's/^mount("MTD"/mount("'"$str"'", "MTD"/g' \
          updater-script
        break
      fi
    done

    #
    # Galaxy S3 system partition fix
    #
    if [ "$is_gs3_common" == "1" ] || [ "$is_gs4_common" == "1" ] 
    then
      sed -i \
        -e 's/mount("'"$param1"'", "'"$param2"'", "'"$sys_mnt"'", "\/system");//g' \
        -e 's/delete_recursive("\/system");/unmount("\/system");\nformat("'"$param1"'", "'"$param2"'", "'"$sys_mnt"'", "0");\nmount("'"$param1"'", "'"$param2"'", "'"$sys_mnt"'", "\/system");/g' \
        updater-script
    fi

    cd ../../../../..
  fi


  # Remove blank lines
  cd WORKING_*
  cd META-INF/com/google/android
  sed -i -e '/^$/d' $1
  cd ../../../../..

fi
