c62e24d23c2b3911d3e718b8462ace7bd3c83de9
[pb.git] / pb
1 #!/bin/sh
2
3 # pinosaur's blog script
4
5 blog_index_file="blogindex.html"
6 rolling_file="rolling.html"
7 template_file="template.html"
8 rss_file="rss.xml"
9 data_dir="blog"
10
11 [ ! -z "$EDITOR" ] && EDITOR="vim"
12
13
14 init() {
15     echo "initing blog"
16     mkdir -p "$data_dir/drafts" &&\
17     mkdir -p "$data_dir/published" &&\
18     touch "$data_dir/database" 
19 }
20
21 refresh() {
22     echo 1
23     # add a confirmation of sorts here
24 }
25
26 new() {
27     [ -z "$1" ] && echo "please supply a name" && exit 1 
28
29     # sanitize input
30     sanitized=`echo -n "$1" | sed -e 's/[^A-Za-z0-9 _-]//g'| sed -e 's/ /-/g'`
31
32     # open in editor
33     $EDITOR "$data_dir/drafts/$sanitized"
34
35 }
36
37 publish() {
38     echo "Select which post to publish"
39     ls -1 "$data_dir/drafts" | nl 
40
41     read -p '> ' choice
42     to_publish=`ls -1 "$data_dir/drafts/" | sed -n "$choice p"`
43     [ -z "$to_publish" ] && echo "Invalid choice" && exit 1
44
45     cat $template_file |\
46         sed -e "s/{{TITLE}}/$to_publish/g" |\
47         sed -e "s/{{DATE}}/`date +'%a, %b %d %H:%M'`/g" |\
48         sed -e "/{{BODY}}/r $data_dir/drafts/$to_publish" |\
49         sed -e "/{{BODY}}/d" # rly ugly for now
50
51 }
52
53 delete() {
54     echo "Select which post to delete"
55     ls -1 "$data_dir/published" | nl 
56 }
57
58 # check to see if all required files are present
59 [ ! -f $blog_index_file ] && echo "missing $blog_index_file" && exit 1
60 [ ! -f $rolling_file ] && echo "missing $rolling_file" && exit 1
61 [ ! -f $template_file ] && echo "missing $template_file" && exit 1
62 [ ! -f $rss_file ] && echo "missing $rss_file" && exit 1
63
64 # possibly also check to see if index and rolling have the proper headers
65
66
67 # check if blog dir exists
68 [ ! -d $data_dir ]  && init
69
70 case $1 in
71     i|init) init;;
72     n|new) new "$2";;
73     p|publish) publish;;
74     d|delete) echo "delete";;
75     r|refresh) echo "refresh";;
76     *) echo "helper" && exit 1;;
77 esac
78