1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#!/bin/bash
set -e
usage()
{
printf "\nUsage: $(basename $0) <source relative dir> <dest absolute dir> [ [ --dry-run ] [ --must-exist ] [ --test-deb-package ] ]\n"
printf "\nThis program will duplicate a source tree directory (arg 1) to a destination tree directory (arg 2).\n"
printf "\nTo perform a dry run, add --dry-run to the command line.\n"
printf "\nOptionally, the files are copied from source to destination only if
the destination file already exists. This process allows one to refresh the
contents of an existing destination tree with a new copy from a more recent
source tree. To apply this condition provide a third argument :
--must-exist.\n"
printf "\n Optionally, the files can be tested for their shipment by a Debian
package. To perform this test, provide a fourth argument:
--test-deb-package.\n"
printf "\nPlease provide a source directory to find files from\n"
printf "\nand a destination directory to store the files into.\n\n"
printf "\nNote that you must be located in the directory that is up one depth
from the source directory of interest, providing a relative path to it. The
destination directory might be absolute."
printf "A typical invocation might be:\n"
printf "$0 <relative path source dir> <full path dest dir>\n\n"
exit 1
}
dry_run=0
no_create_new_file=0
test_deb_package=0
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--dry-run)
dry_run=1
printf "Performing a dry run.\n"
shift # past argument
;;
--must-exist)
no_create_new_file=1
printf "Only refreshing existing files.\n"
shift # past argument
;;
--test-deb-package)
test_deb_package=1
printf "Test if Debian package ships the file.\n"
shift # past argument
;;
--default)
DEFAULT=YES
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
printf "One positional argument: $1.\n"
shift # past argument
;;
esac
done
# Restore the position arguments and check that there are 2 !
set -- "${POSITIONAL[@]}" # restore positional parameters
#printf "Argument count: $#\n"
if test $# -gt 2; then
printf "\nError in the command line. Check it for typos, please.\n"
usage
fi
if test $# -lt 2; then
printf "\nThere must be at least two compulsory arguments. Please check your command line.\n"
usage
fi
if test ! -d $1; then
printf "\nSource directory does not exist\n"
exit 1
fi
if test ! -d $2; then
printf "\nDestination directory does not exist\n"
exit 1
fi
src_tree_dir=$1
dest_tree_dir=$2
for file in $(find ${src_tree_dir} -type f)
do
printf "Working on ${file}... \n"
dir_name=$(dirname ${file})
base_name=$(basename ${file})
# If we only refresh existing files
if test ${no_create_new_file} -eq 1; then
# We need to test if the file exists already
if test -f ${dest_tree_dir}/${file}; then
# If the file exists, then perform the refresh by copying the file
if test ${dry_run} -ne 1; then
# Only if this is not a dry run.
printf "mkdir -p ${dest_tree_dir}/${dir_name}\n"
mkdir -p ${dest_tree_dir}/${dir_name}
printf "cp ${file} ${dest_tree_dir}/${file}\n"
cp ${file} ${dest_tree_dir}/${file}
else
<<<<<<< HEAD
printf "Dry run, should be copying ${file} to ${dest_tree_dir}/${file}.\n"
=======
printf "Dry run, not copying ${file} to ${dest_tree_dir}/${file}.\n"
>>>>>>> 4b1f6a2 (Reinstate the script.)
fi
else
# The file does not exist: do nothing.
printf "${file} does not exist in destination tree, skipping.\n"
fi
else
# We are NOT only refreshing files, copy anything we find
if test ${dry_run} -ne 1; then
# Only if this is not a dry run.
printf "mkdir -p ${dest_tree_dir}/${dir_name}\n"
mkdir -p ${dest_tree_dir}/${dir_name}
printf "cp ${file} ${dest_tree_dir}/${file}\n"
cp ${file} ${dest_tree_dir}/${file}
fi
fi
if test ${test_deb_package} -eq 1; then
result=$(apt-file search ${file})
if test $? -ne 0; then
printf "==> not provided by any package.\n\n"
else
printf "==> provided by the following package(s):\n ${result}\n\n"
fi
fi
done
|