1#!/bin/bash
2#====================================================================================================
3#
4# Script Name: convertMDF
5#
6# General Description: This script calls the necessary tools to convert between the old (text-based)
7#					  meta-data format and the new (binary) format.
8#
9#====================================================================================================
10# Copyright (C) 2014 The Android Open Source Project
11#
12# Licensed under the Apache License, Version 2.0 (the "License");
13# you may not use this file except in compliance with the License.
14# You may obtain a copy of the License at
15#
16#      http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the License is distributed on an "AS IS" BASIS,
20# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21# See the License for the specific language governing permissions and
22# limitations under the License.
23#====================================================================================================
24
25# Do some checking on our environment - we need Java present
26
27if [ -z "$JAVA_HOME" ]
28then
29  echo "Environment variable JAVA_HOME needs to be set!"
30  exit 1
31fi
32
33# Define our root dir and make a dummy parm.txt
34
35BASEFILE=`basename $1 .mdf`
36ROOTDIR=./Dmt
37mkdir $ROOTDIR
38CURRPARM=$ROOTDIR/parm.txt
39cp -f /dev/null $CURRPARM
40echo "type:node" >> $CURRPARM
41echo "access:Get,Add,Replace,Delete" >> $CURRPARM
42 
43# Make sure we have a Unix mdf file, not a DOS one!
44
45dos2unix $1 2> /dev/null
46
47# Begin building the Dmt file structure
48
49echo "Building tree from $1..."
50
51exec < $1
52
53while read line
54do
55  # Check for comment lines
56
57  echo $line | grep -E '^[[:space:]]*#' > /dev/null
58  COMMENTLINE=$?
59
60  # Check for parens
61
62  if echo $line | grep '^\[' > /dev/null
63  then
64    # We've encountered a new node - begin node creation
65
66    CURRDIR=`echo $line | sed "s/\[//g" | sed "s/\]//g" | sed "s/*/[]/g"`
67    if [ "$CURRDIR" = "." ]
68    then
69      CURRDIR=$ROOTDIR
70    else
71      CURRDIR=$ROOTDIR$CURRDIR 
72      #echo "Making dir: $CURRDIR"
73      mkdir -p "$CURRDIR"
74    fi
75
76    CURRPARM=$CURRDIR/parm.txt
77    cp -f /dev/null "$CURRPARM"
78  elif [ "$line" != "" -a "$COMMENTLINE" -ne 0 ]
79  then
80    # Populate the parm.txt file with data
81   
82    echo "$line" >> "$CURRPARM"
83  fi
84
85done
86
87# Call the DMTValidator to make sure we have good data
88
89echo "Validating DMT structure..."
90
91$JAVA_HOME/bin/java -classpath lib/GenTool.jar:lib/joda-time-1.1.jar:lib/jakarta-regexp-1.4.jar DMTValidator -d Dmt
92
93if [ $? -eq 1 ]
94then
95  echo "Please fix the errors above before continuing..."
96  echo "Cleaning up on abort..."
97  rm -rf $ROOTDIR
98  exit 1
99fi
100
101# Now call the Gen tool to build the binary MDF
102
103echo "Calling Gen tool to convert to binary MDF..."
104
105$JAVA_HOME/bin/java -classpath lib/GenTool.jar:lib/joda-time-1.1.jar:lib/jakarta-regexp-1.4.jar Gen -d Dmt -mdfconvert 2> /tmp/java.run.out.$$
106
107# Cleanup our Dmt directory
108
109echo "Cleaning up..."
110
111rm -rf $ROOTDIR
112mv -f root.bmdf $BASEFILE.bmdf
113