nice new tool to show version of feisty meow
[feisty_meow.git] / scripts / files / squish_directories.sh
1 #!/bin/bash
2
3 # archives the files / directories passed on the command line into an archive
4 # file tagged with a datestamp, and removes the original files / directories.
5 # if no names are passed to the script, then it operates on *all* directories
6 # under the current location.
7
8 #hmmm: may want to revisit default behavior.
9
10 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
11
12 dirs=()
13
14 # snag the command line arguments into an array of names.
15 for i in "$@"; do dirs+=("$i"); done
16
17 # if there were no arguments, use the current directories list of directories.
18 if [ ${#dirs[@]} -eq 0 ]; then
19   # using a temp file is clumsy but after a lot of different methods being tried, this one
20   # worked and wasn't super ugly.
21   tempfile="$(mktemp /tmp/dirlist.XXXXXX)"
22   find $dir -mindepth 1 -maxdepth 1 -type d -exec echo "dirs+=(\"{}\");" ';' >$tempfile
23   source "$tempfile"
24 #echo dirs default to: ${dirs[@]}
25   \rm -f $tempfile
26 fi
27
28 # takes a directory name as an argument and sucks the directory
29 # into a timestamped archive file.
30 function flattenizer()
31 {
32   for dirname in "$@"; do
33     while [[ $dirname =~ .*/$ ]]; do
34       dirname="${dirname:0:((${#dirname}-1))}"
35     done
36     if [ ! -z "$dirname" -a -d "$dirname" ]; then
37       echo "flattening '$dirname'..."
38       zip --symlinks -rm "${dirname}_squished_on_$(hostname)_at_$(date_stringer).zip" "$dirname" &>/dev/null
39     fi
40   done
41 }
42
43 for ((i=0; i < ${#dirs[@]}; i++)); do
44   dir="${dirs[i]}"
45   flattenizer "$dir"
46 done
47