first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / text / smoove_lists.sh
1 #!/bin/bash
2 #list schmoover?
3 #smoove_lists source target
4
5 #takes the contents one level deep in the directories in source
6 #and stuffs them into similarly named directory in target.
7 #the directoy under target is named after the last directory
8 #component in each source directory name.
9 #here's an example:
10 #   smoovelists  ~/notes_pending  ~/list_archive
11 #would move all the files in the directories under notes_pending into
12 #similar directories under list_archive.
13 #so, a file ~/notes_pending/foolio/moopter.txt would get moved over to
14 #a directory called ~/list_archive/moopter.txt
15 #
16 #this is useful for coalescing a subset of some to-do items that are under
17 #directories in a temporary folder into the main archive of to-dos.
18 #otherwise one must do some manual labor to collapse the directories into
19 #the right places.
20 #it only supports one level deep of directories currently because that's
21 #the most common usage.
22
23 if [ $# -lt 2 ]; then
24   echo smoove_lists: this requires a source directory where a bunch of list
25   echo directories are located and a target where similarly named directories
26   echo exist.  the files in the source will be moved over to the target.
27   exit 1
28 fi
29
30 source="$(pushd "$1" &>/dev/null; pwd; popd &>/dev/null)"
31 target="$(pushd "$2" &>/dev/null; pwd; popd &>/dev/null)"
32
33 #echo source is $source
34 #echo target is $target
35
36 # create the same directory names under the target if they aren't already
37 # present under it.
38 pushd "$source" &>/dev/null
39
40 tempfile="$(mktemp "$TMP/zz_smoove.XXXXXX")"
41
42 find . -maxdepth 1 -mindepth 1 -type d  >"$tempfile"
43 while read found; do
44   # make a corresponding directory if there isn't one yet.
45   if [ ! -d "$target/$found" ]; then
46     mkdir "$target/$found"
47   fi
48   # move all the files out of the source and into the target.
49   mv "$found"/* "$target/$found/"
50 done <"$tempfile"
51
52 rm "$tempfile"
53
54 # clean out any directories that we managed to move everything out of.
55 perl "$SHELLDIR/files/zapdirs.pl" $source
56
57 popd &>/dev/null
58