#!/bin/bash # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # # Copyright (C) 2004 Sam Hocevar # 14 rue de Plaisance, 75014 Paris, France # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # # 0. You just DO WHAT THE FUCK YOU WANT TO. #Original work by http://sov.localhostz.org || Feel free to report bugs/comments/suggestion at sov[AT]localhostz[DOT]org readonly wd=~/.clili readonly STDERR=/dev/stderr show() { local lorem=$1 if ! test -r $wd/$lorem then error 1 $lorem; else cat $wd/$lorem; fi } list() { ls $wd; } add() { local toadd=$1 local name=${2:-$1} if ! test -r $toadd then error 1 $toadd; elif test -d $toadd then error 6 $toadd; elif ! test -w $wd then error 2 $wd; elif test -e $wd/$name then error 3 $name; else cp $toadd $wd/$name; #echo "Successfully added file $toadd as $name"; # Useless verbose fi } edit() { local toedit=$1 if ! test -r $wd/$toedit then error 1 $toedit; elif ! test -w $wd/$toedit then error 3 $toedit; else ${EDITOR:-vi} $wd/$toedit; fi } remove() { local toremove=$@ if ! test -w $wd then error 2 $wd; fi if test $toremove == "all" then remove_all; else for file in $toremove do if ! test -w $wd/$file then echo "Warning : $file is not writable or does not exists. Skipping." > $STDERR; else rm $wd/$file; # && echo "$file successfully deleted."; # Useless verbose fi done fi } remove_all() { echo -n "Remove all files in your $wd directory ? [y/n]"; read yesno; case $yesno in y|Y) rm $wd/* && echo "$wd directory successfully cleaned." && exit;; n|N) echo "no file have been removed." && exit;; *) remove_all;; esac } ############## error() { local errorcode=$1 local file=$2 case $errorcode in 1) local error="$file is not readable or does not exists." ;; 2) local error="$file is not writable or does not exists." ;; 3) local error="$file already exists." ;; 4) local error="no operation specified." ;; 5) local error="too many arguments." ;; 6) local error="$file is a directory.";; *) local error="unknwon error. Please reboot your computer." ;; esac echo "ERROR: ${error}" > $STDERR && exit $errorcode } help() { echo "clili (command line lorem ipsum)"; echo "USAGE :"; echo "% clili 'text' : print the content of file 'text' in your directory on STDOUT."; echo "% clili add 'file' 'name' : add 'file' in your directory with name 'name' if given."; echo "% clili list : list your texts directory."; echo "% clili remove 'text' : remove file 'text' from your directory. You can specify as much file as you want."; echo "% clili edit 'text' : edit the file 'text' from your directory." echo "% clili help : print this help dialog." } ################# if ! test -d $wd then mkdir $wd; # echo "WARNING : Your $wd directory is empty"; # Useless verbose. fi if test $# == 0 then error 4; elif test $# == 1 then if test $1 == "list" then list; elif test $1 == "help" then help; else show $1; fi else case $1 in edit) if test $# -gt 2; then error 5; else edit $2; fi;; add) if test $# -gt 3; then error 5; else add $2 ${3:-$2}; fi;; remove) shift; remove $@;; esac fi exit 0