added move functionality

This commit is contained in:
Mehmet Tekman 2017-08-29 03:06:13 +01:00
parent 63d06824a1
commit c4317eef4c

View File

@ -1,5 +1,23 @@
#!/bin/bash #!/bin/bash
__cleanup_org(){
local file=$1
local dir=$2
[ "$file" = "" ] || [ "$dir" = "" ] && echo "nothing to clean up." && return 0
rm `dirname ${file}`/\#`basename ${file}`\# ${file}\~ 2>/dev/null # remove temp files
# cleanup
# - Remove unused directories
local stack_count=6 # can't expect more than 6 nested dirs, surely...
while [ $stack_count -gt 0 ]; do
stack_count=$(( $stack_count - 1 ))
find ${dir} -type d -exec rmdir {} \; 2>/dev/null
done
}
org(){ org(){
local org_loc=~/.config/org_notes.location local org_loc=~/.config/org_notes.location
@ -13,7 +31,7 @@ or run the install script."
local org_dir=$(cat $org_loc) local org_dir=$(cat $org_loc)
local file=$1 local file=$1
local delete=$2 local command=$2
case $file in case $file in
"") "")
@ -26,7 +44,7 @@ or run the install script."
-h);& -h);&
--help) --help)
echo " echo "
`basename $0` <filename> [--delete] `basename $0` <filename> [--delete] [--move=<dest>]
Generates an org-notes file in a folder location set in the $org_loc file Generates an org-notes file in a folder location set in the $org_loc file
"; ";
@ -34,39 +52,57 @@ Generates an org-notes file in a folder location set in the $org_loc file
;; ;;
esac esac
if [[ "$delete" =~ "--" ]]; then if [ "$command" = "--delete" ] || [[ "$command" =~ "--move=" ]]; then
[ "$delete" != "--delete" ] && echo "Unable to parse: $delete" && return -1 else
echo "Unable to parse: $command"
return -1
fi fi
local ext=$( echo $file | awk -F'.' '{print $NF}' ) local ext=$( echo $file | awk -F'.' '{print $NF}' )
[ "$ext" != ".org" ] && file=${file}.org [ "$ext" != ".org" ] && file=${file}.org
if [ "$delete" = "--delete" ]; then local eloc=$org_dir/$file
local eloc=$org_dir/$file
if [ "$command" != "" ]; then
if [ -e $eloc ]; then if [ -e $eloc ]; then
echo -n "Remove $file? [y/n] "; read ans;
if [ "$ans" = "y" ]; then
rm `dirname ${eloc}`/\#`basename ${eloc}`\# ${eloc}\~ 2>/dev/null # remove temp files
rm ${eloc} && echo "Deleted."
# cleanup case $command in
# - Remove unused directories "--delete")
local stack_count=6 # can't expect more than 6 nested dirs, surely... echo -n "Remove $file? [y/n] "; read ans;
while [ $stack_count -gt 0 ]; do if [ "$ans" = "y" ]; then
stack_count=$(( $stack_count - 1 )) rm ${eloc} && echo "Deleted."
find ${org_dir} -type d -exec rmdir {} \; 2>/dev/null __cleanup_org $eloc $org_dir
done return 0
fi fi
;;
"--move="*)
local dest=`echo $command | sed 's|--move=||'`
echo -n "Move $file to $dest ? [y/n] "; read ans;
if [ "$ans" = "y" ]; then
local ext1=$( echo $dest | awk -F'.' '{print $NF}' )
[ "$ext1" != ".org" ] && dest=${dest}.org
local edest=$org_dir/$dest
mkdir -p $org_dir/`dirname $dest`
mv $eloc $edest && echo "Moved: $file --> $dest"
__cleanup_org $eloc $org_dir
return 0
fi
;;
esac
else else
echo "Cannot find $file. Aborting." echo "Cannot find $file. Aborting."
return -1
fi fi
else else
# no command, just create
local dirn=`dirname $file` local dirn=`dirname $file`
mkdir -p $org_dir/$dirn mkdir -p $org_dir/$dirn
emacs $org_dir/$file emacs $org_dir/$file
return 0
fi fi
} }