1#!/system/bin/sh
2
3# Copyright 2014 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Android shell script to make the destination directory identical with the
8# source directory, without doing unnecessary copies. This assumes that the
9# the destination directory was originally a copy of the source directory, and
10# has since been modified.
11
12source=$1
13dest=$2
14echo copying $source to $dest
15
16delete_extra() {
17  # Don't delete symbolic links, since doing so deletes the vital lib link.
18  if [ ! -L "$1" ]
19  then
20    if [ ! -e "$source/$1" ]
21    then
22      echo rm -rf "$dest/$1"
23      rm -rf "$dest/$1"
24    elif [ -d "$1" ]
25    then
26      for f in "$1"/*
27      do
28       delete_extra "$f"
29      done
30    fi
31  fi
32}
33
34copy_if_older() {
35  if [ -d "$1" ] && [ -e "$dest/$1" ]
36  then
37    if [ ! -e "$dest/$1" ]
38    then
39      echo cp -a "$1" "$dest/$1"
40      cp -a "$1" "$dest/$1"
41    else
42      for f in "$1"/*
43      do
44        copy_if_older "$f"
45      done
46    fi
47  elif [ ! -e "$dest/$1" ] || [ "$1" -ot "$dest/$1" ] || [ "$1" -nt "$dest/$1" ]
48  then
49    # dates are different, so either the destination of the source has changed.
50    echo cp -a "$1" "$dest/$1"
51    cp -a "$1" "$dest/$1"
52  fi
53}
54
55if [ -e "$dest" ]
56then
57  echo cd "$dest"
58  cd "$dest"
59  for f in ./*
60  do
61    if [ -e "$f" ]
62    then
63      delete_extra "$f"
64    fi
65  done
66else
67  echo mkdir "$dest"
68  mkdir "$dest"
69fi
70echo cd "$source"
71cd "$source"
72for f in ./*
73do
74  if [ -e "$f" ]
75  then
76    copy_if_older "$f"
77  fi
78done
79