dotfiles

Dotfiles for my OpenBSD environment.
git clone https://git.mtkn.jp/dotfiles
Log | Files | Refs

chogo (2364B)


      1 #!/bin/sh
      2 # This is my personal accounting script
      3 # The format of a Kakeibo file is as follows:
      4 # 	<id> <account>:<???>:<Kamoku>[:<tekiyou>[ ... ]] <ammount> <comment>
      5 # id     : the identifier of the transaction (sor far this does nothing)
      6 # account: account name
      7 # ???    : assets|liabilities|equity|expenses|income
      8 # Kamoku : any name to identify the useage
      9 # comment: any string without a newline
     10 
     11 usage(){
     12 	echo 'usage: '$(basename "$0")' [-d <depth>] <command> [<file>]'
     13 	echo '       <command>: st|bs|pl|bf|ck'
     14 	echo '       if no <file> is supplied, '$(basename "$0")' reads from stdin.'
     15 }
     16 
     17 st(){
     18 	if [ "$depth" -lt 0 ]; then
     19 		awk '{sum[$3]+=$4}END{for(key in sum){print key, sum[key]}}' | sort
     20 	else
     21 		awk -v depth=$depth '{
     22 			split($3, a, ":")
     23 			for(k = 1; k <= depth; k++){
     24 				if(k == depth || k == length(a)){
     25 					printf "%s", a[k]
     26 					break;
     27 				}else{
     28 					printf "%s:", a[k]
     29 				}
     30 			}
     31 			printf " %s\n", $4
     32 		}' |
     33 		awk '{sum[$1]+=$2}END{for(key in sum){print key, sum[key]}}' |
     34 		sort
     35 	fi
     36 }
     37 
     38 bs(){
     39 	st |
     40 	grep -e 'assets' -e 'liabilities' -e 'equity'
     41 }
     42 
     43 pl(){
     44 	st |
     45 	grep -e 'expenses' -e 'income'
     46 }
     47 
     48 bf(){
     49 	d=$(date "+%Y-%m-00")
     50 	bs |
     51 	grep -v brought_forward |
     52 	awk -v d="$d" '{printf "0 %s %s %d\n", d, $1, $2}' |
     53 	awk -v d="$d" '
     54 		NR==1{print; split($3, a, ":"); s=$4}
     55 		NR>1{
     56 			split($3, b, ":")
     57 			if(a[1] == b[1]){
     58 				s += $4
     59 			}
     60 			else{
     61 				printf "0 %s %s:equity:brought_forward %d\n", d, a[1], -s
     62 				s = $4
     63 				a[1] = b[1]
     64 			}
     65 			print
     66 		}
     67 		END{printf "0 %s %s:equity:brought_forward %d\n", d, b[1], -s}
     68 	'
     69 }
     70 
     71 ck(){
     72 	awk '{a+=$4}END{if(a!=0){print "error sum = "a; exit 1}}'
     73 }
     74 
     75 #main
     76 depth=3
     77 while getopts d: opt; do
     78 	case $opt in
     79 		d)	depth="$OPTARG" ;; ##TODO: check wether depth is number
     80 		*)	usage >&2; exit 1 ;;
     81 	esac
     82 done
     83 shift $(expr "$OPTIND" - 1)
     84 
     85 if [ ! -n "$1" ]; then usage >&2; exit 1; fi
     86 if [ -n "$3" ]; then usage >&2; exit 1; fi
     87 if [ -n "$2" ]; then
     88 	if [ ! -f "$2" ]; then echo "file $2 not found" >&2; exit 1; fi
     89 fi
     90 case "$1" in
     91 	st) eval 'cat '"$2" | sed '/^[[:space:]]*$/d' | st ;;
     92 	bs) eval 'cat '"$2" | sed '/^[[:space:]]*$/d' | bs ;;
     93 	pl) eval 'cat '"$2" | sed '/^[[:space:]]*$/d' | pl ;;
     94 	bf) eval 'cat '"$2" | sed '/^[[:space:]]*$/d' | bf ;;
     95 	ck) eval 'cat '"$2" | sed '/^[[:space:]]*$/d' | ck ;;
     96 	*) usage >&2; exit 1 ;;
     97 	#BUG: script not terminated if $2 is empty and command doesn't exist
     98 esac