Read script parameters from config file. (from ToT to factory-980B)

make_factory_package.sh needs many parameters and to support multi-board
installation it has to be run twice (without and with --subfolder). This
change adds the functionality to read sections of parameters from a
config file and executes them in order.

Cherry-Picked-From: http://gerrit.chromium.org/gerrit/7473
Cherry-Picked-From: http://gerrit.chromium.org/gerrit/7698
Cherry-Picked-From: http://gerrit.chromium.org/gerrit/7764

BUG=chrome-os-partner:5850
TEST=1. Create a config file (mp_factory.conf) with the following
        content:
       [x86-zgb]
        --factory ~/trunk/src/build/images/${BOARD}/latest/chromiumos_factory_image.bin
        --release ~/trunk/src/build/images/${BOARD}/latest/chromiumos_image.bin
        --hwid_updater ~/trunk/src/platform/chromeos-hwid/hwid_bundle_zgb.sh
        --firmware_updater none
        --detect_release_image

       [x86-zgb-he]
        --subfolder x86-zgb-he
        --factory ~/trunk/src/build/images/${BOARD}/latest/chromiumos_factory_image.bin
        --release ~/trunk/src/build/images/${BOARD}/latest/chromiumos_image.bin
        --hwid_updater ~/trunk/src/platform/chromeos-hwid/hwid_bundle_zgb.sh
        --firmware_updater none
        --detect_release_image

     2. BOARD=x86-zgb ./make_factory_package.sh --config mp_factory.conf

Change-Id: Icf3a4c37fdd1b24b81c69b5f1ec9c85289c5bb2c
Reviewed-on: http://gerrit.chromium.org/gerrit/7852
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Tested-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/make_factory_package.sh b/make_factory_package.sh
index 99b0812..9183da9 100755
--- a/make_factory_package.sh
+++ b/make_factory_package.sh
@@ -74,6 +74,8 @@
 DEFINE_integer sectors 31277232  "Size of image in sectors"
 DEFINE_boolean detect_release_image ${FLAGS_TRUE} \
   "If set, try to auto-detect the type of release image and convert if required"
+DEFINE_string config "" \
+  "Config file where parameters are read from"
 
 # Parse command line
 FLAGS "$@" || exit 1
@@ -617,10 +619,79 @@
   python2.6 devserver.py --factory_config miniomaha.conf"
 }
 
+parse_and_run_config() {
+  # This function parses parameters from config file. Parameters can be put
+  # in sections and sections of parameters will be run in turn.
+  #
+  # Config file format:
+  # [section1]
+  #  --param value
+  #  --another_param  # comment
+  #
+  # # some more comment
+  # [section2]
+  #  --yet_another_param
+  #
+  # Note that a section header must start at the beginning of a line.
+  # And it's not allowed to read from config file recursively.
+
+  local config_file="$1"
+  local -a cmds
+  local cmd=""
+
+  echo "Read parameters from: $config_file"
+  local config="$(<$config_file)"
+  local IFS=$'\n'
+  for line in $config
+  do
+    if [[ "$line" =~ ^\[.*] ]]; then
+      if [ -n "$cmd" ]; then
+        cmds+=("$cmd")
+        cmd=""
+      fi
+      continue
+    fi
+    line="${line%%#*}"
+    cmd="$cmd $line"
+  done
+  if [ -n "$cmd" ]; then
+    cmds+=("$cmd")
+  fi
+
+  for cmd in "${cmds[@]}"
+  do
+    info "Executing: $0 $cmd"
+    eval "MFP_SUBPROCESS=1 $0 $cmd"
+  done
+}
+
 main() {
   set -e
   trap on_exit EXIT
 
+  if [ -n "$FLAGS_config" ]; then
+    [ -z "$MFP_SUBPROCESS" ] ||
+      die "Recursively reading from config file is not allowed"
+
+    check_file_param FLAGS_config ""
+    check_empty_param FLAGS_release "when using config file"
+    check_empty_param FLAGS_factory "when using config file"
+    check_empty_param FLAGS_firmware_updater "when using config file"
+    check_empty_param FLAGS_hwid_updater "when using config file"
+    check_empty_param FLAGS_install_shim "when using config file"
+    check_empty_param FLAGS_complete_script "when using config file"
+    check_empty_param FLAGS_usbimg "when using config file"
+    check_empty_param FLAGS_diskimg "when using config file"
+    check_empty_param FLAGS_subfolder "when using config file"
+
+    # Make the path and folder of config file available when parsing config.
+    MFP_CONFIG_PATH="$(readlink -f "$FLAGS_config")"
+    MFP_CONFIG_DIR="$(dirname "$MFP_CONFIG_PATH")"
+
+    parse_and_run_config "$FLAGS_config"
+    exit
+  fi
+
   check_parameters
   setup_environment
   if [ "$FLAGS_detect_release_image" = "$FLAGS_TRUE" ]; then