61515083333b679bd6d5c6abf705a4358031f916
[infrastructure.git] / scripts / import-module.sh
1 #!/bin/bash
2 #
3 # $Id$
4 #
5
6 date=$(date +%Y-%m-%d)
7
8 #### REVIEW this carefully
9 module=new_plc_api
10 cvstag=planetlab-4_0-rc3
11 previous=002-rc3-2007-05-02
12 next=003-${cvstag}-${date}
13
14 ####
15 svn="svn+ssh://thierry@svn.one-lab.org/svn"
16 cvs=":pserver:anon@cvs.planet-lab.org:/cvs"
17 import=$HOME/import-$module-$date
18
19
20 function message () {
21   echo -n '-------------------- '
22   echo $@
23   echo -n 'Enter return to proceed .. '
24   read x
25 }
26
27 function run () {
28   echo -n '-------------------- '
29   message="$1" ; shift
30   echo $message
31   
32   echo -n 'PWD=' ; pwd
33   echo RUNNING $@
34   $@
35 }
36
37 function run_prompt () {
38   default="$1" ; shift
39   message="$1" ; shift
40   case "$default" in
41     y|Y) label="[y]/n" ;;
42     n|N) label="y/[n]" ;;
43     *) label="y/n (no default)" ;;
44   esac
45
46   echo -n '-------------------- '
47   echo $message
48   
49   echo -n 'PWD= ' ; pwd 
50   echo -n "want to run $@ $label ? "
51   read answer
52   [ -z "$answer" ] && answer=$default
53   while true; do
54     case $answer in
55       y|Y)
56         $@
57         break
58         ;;
59       n|N)
60         echo 'Skipped'
61         break
62         ;;
63       *)
64         echo 'Please enter y or n'
65         ;;
66     esac
67   done
68 }
69
70 function run_loop () {
71   message="$1" ; shift
72   while true; do
73     echo -n '-------------------- '
74     echo $message
75     echo -n PWD= ; pwd
76     echo Running $@
77     $@
78     echo -n 'OK ? y/[n] '
79     read answer
80     [ -z "$answer" ] && answer=n
81     case $answer in
82       y|Y)
83         break ;;
84       *) ;;
85   esac
86 done
87 }
88
89 ### echoes OK on stdout if url exists
90 function svn_url_check () {
91    url=$1; shift
92    svn list $url &> /dev/null && echo OK
93 }
94
95 ### return 'y' or 'n' for being used as prompt
96 function svn_url_needs_creation () {
97    url=$1; shift
98    if [ -n "$(svn_url_check $url)" ] ; then echo n ; else echo y ; fi
99 }
100
101 function dir_needs_creation () {
102    dir=$1; shift
103    if [ -d "$dir" ] ; then echo n ; else echo y ; fi
104 }
105
106 ##############################
107 ##############################
108 ##############################
109
110 cat <<EOF
111 ####### WARNING
112
113  this script is quite rough and should be used with care
114
115  I am slowly trying to automate this painful process,
116
117  so again : USE WITH CARE
118
119 module=$module
120 previous=$previous
121 next=$next
122 cvstag=$cvstag
123  
124 svn=$svn
125 cvs=$cvs
126 date=$date
127 import=$import
128 ####### WARNING
129
130 EOF
131
132 function run_test () {
133   message Hi there
134   run_prompt n "going to /etc" cd /etc
135   run_prompt y "going to /etc" cd /etc
136   run_prompt y "Cleaning up /foobar" rmdir /foobar
137   run_prompt $(dir_needs_creation /foobar) "create unexisting dir /foobar" mkdir /foobar
138   run_prompt $(dir_needs_creation /var) "create existing dir /var" mkdir /var
139   run_prompt y "Cleaning up /foobar" rmdir /foobar
140   
141   run_loop "Check this and that" ls -ls passwd
142   run "Listing passwd" ls -ls passwd
143   exit 0
144 }
145
146 # checks current status
147 echo "------------------------------ Checking $module/imports/import-$previous"
148 [ -z "$(svn_url_check $svn/$module/imports/import-$previous)" ] && echo WARNING : import-$previous not found
149
150 echo ''
151
152 # snapshot current status
153 prompt=$(svn_url_needs_creation $svn/$module/imports/before-$next)
154 run_prompt $prompt "Creating snapshot of current status" \
155    svn copy $svn/$module/trunk $svn/$module/imports/before-$next
156
157 # init import dir from previous import
158 prompt=$(svn_url_needs_creation $svn/$module/imports/import-$next)
159 run_prompt $prompt "Preparing import-$next from import-$previous" \
160    svn copy $svn/$module/imports/import-$previous $svn/$module/imports/import-$next
161
162 ####
163 run_prompt $(dir_needs_creation $import/$module/imports) "Creating working dir " \
164     mkdir -p $import/$module/imports
165
166 ###
167 run_prompt $(dir_needs_creation $import/$module/imports/import-$next) "Checking out import-$next for applying changes" \
168     svn co $svn/$module/imports/import-$next $import/$module/imports/import-$next
169
170 run "Going there" \
171     cd $import/$module/imports
172
173 message 'NOTE: the way we merge changes right below might need to be tested/improved'
174
175 run_prompt n "Incorporating changes since import-$previous in import-$next" \
176     cvs -d $cvs export -r $cvstag -d import-$next $module
177
178 run "Going to import-$next" \
179     cd $import/$module/imports/import-$next
180
181 run_loop "Check the changes in import-$next -- no conflict expected" svn status $import/$module/imports/import-$next
182
183 run_prompt n "CHECKING IN changes in import-$next" \
184     svn ci -m "Changes since $previous"
185
186 ###
187 run "Going to $import/$module" \
188     cd $import/$module
189
190 run_prompt $(dir_needs_creation $svn/$module/trunk) "Checking out trunk for applying changes" \
191     svn co $svn/$module/trunk 
192
193 run_prompt n "Merging changes from import-$previous to import-$next into trunk" \
194     svn merge $svn/$module/imports/import-$previous $svn/$module/imports/import-$next trunk
195
196 run_loop "Check the changes in trunk -- conflicts should get resolved" svn status trunk
197
198 run_prompt n "CHECKING IN changes in trunk" \
199     svn ci -m "Merged changes from import-$previous to import-$next"
200
201 prompt=$(svn_url_needs_creation $svn/$module/imports/after-$next)
202 run_prompt $prompt "Backing up into after-$next" \
203     svn copy $svn/$module/$trunk $svn/$module/imports/after-$next
204
205 run_prompt n "Want to rename $import as $import-to-trash" \
206     mv $import $import-totrash
207
208 echo 'Done'
209 exit 0