5a603b4dd6dcdc147975e2e518f3df3e93c7e037
[feisty_meow.git] / scripts / files / zip_directories.sh
1 #!/bin/bash
2
3 # goes through the current directory (currently) and zips up any directories
4 # into an archive with the same name as the directory plus a time stamp.
5
6 source $FEISTY_MEOW_SCRIPTS/core/functions.sh
7
8 dirs=()
9
10 # snag the command line arguments into an array of names.
11 for i in "$@"; do dirs+=("$i"); done
12
13 # if there were no arguments, use the current directories list of directories.
14 if [ ${#dirs[@]} -eq 0 ]; then
15   # using a temp file is clumsy but after a lot of different methods being tried, this one
16   # worked and wasn't super ugly.
17   tempfile="$(mktemp /tmp/dirlist.XXXXXX)"
18   find $dir -mindepth 1 -maxdepth 1 -type d -exec echo "dirs+=(\"{}\");" ';' >$tempfile
19   source "$tempfile"
20 #echo dirs default to: ${dirs[@]}
21   \rm -f $tempfile
22 fi
23
24 # takes a directory name as an argument and sucks the directory
25 # into a timestamped zip file.
26 function flattenizer()
27 {
28   for dirname in "$@"; do
29     while [[ $dirname =~ .*/$ ]]; do
30       dirname="${dirname:0:((${#dirname}-1))}"
31     done
32     if [ ! -z "$dirname" -a -d "$dirname" ]; then
33       echo "flattening '$dirname'..."
34       zip -rm "${dirname}_$(date_stringer).zip" "$dirname" &>/dev/null
35     fi
36   done
37 }
38
39 for ((i=0; i < ${#dirs[@]}; i++)); do
40   dir="${dirs[i]}"
41   flattenizer "$dir"
42 done
43