Optimus black and stuff
[wiki.git] / send.sh
1 #!/bin/sh
2
3 # Replicant redmine wiki templates
4 #
5 # Copyright (C) 2014-2015 Paul Kocialkowski <contact@paulk.fr>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 . ./credentials.sh
21
22 COOKIES=".cookies"
23 REDMINE="https://redmine.replicant.us"
24 PROJECT="replicant"
25
26 extract() {
27         echo "$1" | sed "s/.*<.*name=\"$2\"[^>]*value=\"\([^\"]*\)\".*/\1/g"
28 }
29
30 login() {
31         if [ "$username" = "" ]
32         then
33                 echo "Username:"
34                 read username
35         fi
36
37         if [ "$password" = "" ]
38         then
39                 echo "Password:"
40                 read password
41         fi
42
43         curl -d "username=$username&password=$password&submit=Login" -c $COOKIES "$REDMINE/login"
44 }
45
46 push() {
47         page=$1
48         comment=$2
49
50         edit=$( curl -b $COOKIES -c $COOKIES -O /dev/stdout "$REDMINE/projects/$PROJECT/wiki/$page/edit" | grep name | tr -d '\n' )
51
52         method=$( extract "$edit" "_method" )
53         authenticity_token=$( extract "$edit" "authenticity_token" )
54         content_version=$( extract "$edit" "content\[version\]" )
55         commit=$( extract "$edit" "commit" )
56
57         curl -b $COOKIES -c $COOKIES -F "_method=$method" -F "authenticity_token=$authenticity_token" -F "content[version]=$content_version" -F "content[comments]=$comment" -F "content[text]=$( cat $page )" -F "commit=$commit" "$REDMINE/projects/$PROJECT/wiki/$page"
58 }
59
60 cleanup() {
61         rm $COOKIES
62 }
63
64 case $1 in
65         "login")
66                 login
67                 ;;
68         "push")
69                 push "$2" "$3"
70                 ;;
71         "cleanup")
72                 cleanup
73                 ;;
74 esac