1#!/bin/bash
2#
3#  Copyright (C) 2020 The Android Open Source Project
4#
5#  Licensed under the Apache License, Version 2.0 (the "License");
6#  you may not use this file except in compliance with the License.
7#  You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#  Unless required by applicable law or agreed to in writing, software
12#  distributed under the License is distributed on an "AS IS" BASIS,
13#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#  See the License for the specific language governing permissions and
15#  limitations under the License.
16#
17
18set -e
19
20function usage() {
21  echo "usage: $0 <source dir> <exploded dir>"
22  echo "This script splits files in <source dir> by line and puts the results in <exploded dir>"
23  exit 1
24}
25
26scriptDir="$(cd $(dirname $0) && pwd)"
27removeLeavesArg=""
28if [ "$1" == "--remove-leaves" ]; then
29  removeLeavesArg="$1"
30  shift
31fi
32if [ "$1" == "--consolidate-leaves" ]; then
33  removeLeavesArg="$1"
34  shift
35fi
36sourcePath="$1"
37explodedDir="$2"
38
39if [ "$sourcePath" == "" ]; then
40  usage
41fi
42sourcePath="$(readlink -f $sourcePath)"
43
44if [ "$explodedDir" == "" ]; then
45  usage
46fi
47mkdir -p "$explodedDir"
48explodedDir="$(cd $explodedDir && pwd)"
49
50function explodePath() {
51  sourceFile="$1"
52  explodedPath="$2"
53
54  mkdir -p "$explodedPath"
55
56  # split $sourceFile into lines, and put each line into a file named 00001, 00002, 00003, ...
57  $scriptDir/explode.py $removeLeavesArg "$sourceFile" "$explodedPath"
58  touch "$explodedPath/file"
59}
60
61
62function main() {
63  rm "$explodedDir" -rf
64  mkdir -p "$explodedDir"
65
66  if [ -f $sourcePath ]; then
67    echo exploding single file $sourcePath into $explodedDir
68    explodePath "$sourcePath" "$explodedDir"
69  else
70    echo splitting everything in $sourcePath into $explodedDir
71    cd $sourcePath
72    for filePath in $(find -type f); do
73      explodePath "$sourcePath/$filePath" "$explodedDir/$filePath"
74    done
75    for filePath in $(find -type l); do
76      cp -P "$sourcePath/$filePath" "$explodedDir/$filePath"
77    done
78  fi
79  echo done splitting everything in $sourcePath into $explodedDir
80}
81
82
83main
84