OSDN Git Service

Keep the file timesamp of commit time
[pukiwiki/pukiwiki_devel.git] / release.sh
1 #!/bin/sh
2 # $Id: release.sh,v 1.35 2011/06/05 14:22:51 henoheno Exp $
3 # $CVSKNIT_Id: release.sh,v 1.11 2004/05/28 14:26:24 henoheno Exp $
4 #  Release automation script for PukiWiki
5 #  ==========================================================
6    Copyright='(C) 2002-2004,2011 minix-up project, All Rights Reserved'
7    Homepage='http://cvsknit.sourceforge.net/'
8    License='(also revised)BSD Licnese, NO WARRANTY'
9 #
10
11 # Name and Usage --------------------------------------------
12 _name="` basename $0 `"
13
14 usage(){
15   trace 'usage()' || return  # (DEBUG)
16   warn  "Usage: $_name [options] VERSION_TAG (1.4.3_rc1 like)"
17   warn  "  Options:"
18   warn  "    --nopkg     Suppress creating archive (Extract and chmod only)"
19   warn  "    --norm      --nopkg, and remove nothing (.cvsignore etc)"
20   warn  "    --co        --norm, and use 'checkout' command instead of 'export'"
21   warn  "    --utf8      Create UTF-8 converted archive (EXPERIMENTAL)"
22   warn  "    -z|--zip    Create *.zip archive"
23   warn  "    --move-dist Move *.ini.php => *.ini-dist.php"
24   warn  "    --copy-dist Move, and Copy *.ini.php <= *.ini-dist.php"
25   warn  "    --git       Use git repository"
26   warn  "    --repo <repository> Git repository_url"
27   warn  "    --name <distname> package_name"
28   return 1
29 }
30
31 # Common functions ------------------------------------------
32 warn(){  echo "$*" 1>&2 ; }
33 err() {  warn "Error: $*" ; exit 1 ; }
34
35 quote(){
36   test    $# -gt 0  && {  echo -n  "\"$1\"" ; shift ; }
37   while [ $# -gt 0 ] ; do echo -n " \"$1\"" ; shift ; done ; echo
38 }
39
40 trace(){
41   test "$__debug" || return 0  # (DEBUG)
42   _msg="$1" ; test $# -gt 0 && shift ; warn "  $_msg    : ` quote "$@" `"
43 }
44
45 check_versiontag(){
46   case "$1" in
47     [1-9].[0-9]              | [1-9].[0-9]                   ) tag="r$1" ;;
48     [1-9].[0-9]_rc[1-9]      | [1-9].[0-9]_rc[1-9]           ) tag="r$1" ;;
49     [1-9].[0-9].[0-9]        | [1-9].[0-9].[0-9][0-9]        ) tag="r$1" ;;
50     [1-9].[0-9].[0-9]_[a-z]* | [1-9].[0-9].[0-9][0-9]_[a-z]* ) tag="r$1" ;;
51     [1-9].[0-9].[0-9]_[1-9]  | [1-9].[0-9].[0-9][0-9]_[1-9]  ) tag="r$1" ;;
52     [1-9].[0-9].[0-9]_[1-9]_[a-z]*  | [1-9].[0-9].[0-9][0-9]_[1-9]_[a-z]*  ) tag="r$1" ;;
53     HEAD | r1_3_3_branch | branch_r* ) tag="$rel" ;;
54     '' ) usage ; return 1 ;;
55      * ) warn "Error: Invalid string: $1" ; usage ; return 1 ;;
56   esac
57   echo "$tag" | tr '.' '_'
58 }
59
60 chmod_pkg(){
61   ( cd "$1"
62     # ALL: Read only
63     find . -type d | while read line; do chmod 755 "$line"; done
64     find . -type f | while read line; do chmod 644 "$line"; done
65     # Add write permission for PukiWiki
66     chmod 777 attach backup cache counter diff trackback wiki* 2>/dev/null
67     chmod 666 wiki*/*.txt cache/*.dat cache/*.ref cache/*.rel  2>/dev/null
68   )
69 }
70
71 # Default variables -----------------------------------------
72
73 mod=pukiwiki
74
75 CVSROOT=":pserver:anonymous@cvs.sourceforge.jp:/cvsroot/$mod"
76
77 # Function verifying arguments ------------------------------
78
79 getopt(){ _arg=noarg
80   trace 'getopt()' "$@"  # (DEBUG)
81
82   case "$1" in
83   ''  )  echo 1 ;;
84   -[hH]|--help ) echo _help _exit ;;
85   --debug      ) echo _debug 1    ;;
86   --nopkg      ) echo _nopkg 1    ;;
87   --norm|--noremove ) echo _nopkg _noremove 1 ;;
88   --co|--checkout   ) echo _nopkg _noremove _checkout 1 ;;
89   -z|--zip     ) echo _zip 1      ;;
90   --ut|--utf|--utf8|--utf-8 ) echo _utf8 1  ;;
91   --copy-dist  ) echo _copy_dist 1 ;;
92   --move-dist  ) echo _move_dist 1 ;;
93   -d  ) echo _CVSROOT 2 ; _arg="$2" ;;
94   --git ) echo _git 1 ;;
95   --repo ) echo _gitrepo 2 ; _arg="$2" ;;
96   --name ) echo _pkg_name 2 ; _arg="$2" ;;
97   -*  ) warn "Error: Unknown option \"$1\"" ; return 1 ;;
98    *  ) echo OTHER ;;
99   esac
100
101   test 'x' != "x$_arg"
102 }
103
104 # Working start ---------------------------------------------
105
106 # Show arguments in one line (DEBUG)
107 case '--debug' in "$1"|"$3") false ;; * ) true ;; esac || {
108   test 'x--debug' = "x$1" && shift ; __debug=on ; trace 'Args  ' "$@"
109 }
110
111 # Parsing
112 while [ $# -gt 0 ] ; do
113   chs="` getopt "$@" `" || err "Syntax error with '$1'"
114   trace '$chs  ' "$chs"  # (DEBUG)
115
116   for ch in $chs ; do
117     case "$ch" in
118      [1-3]   ) shift $ch ;;
119      _exit   ) exit      ;;
120      _help   ) usage     ;;
121
122      _CVSROOT) CVSROOT="$2" ;;
123      _gitrepo) gitrepo="$2" ;;
124      _pkg_name) pkg_name="$2" ;;
125
126      _*      ) eval "_$ch"=on ;;
127       *      ) break 2   ;;
128     esac
129   done
130 done
131
132 # No argument
133 if [ $# -eq 0 ] ; then usage ; exit ; fi
134
135 # Utility check ---------------------------------------------
136
137 if [ "$__utf8" ] ; then
138
139   # nkf
140   which nkf || err "nkf version 2.0 or later (UTF-8 enabled) not found"
141   nkf_version="` nkf -v 2>&1 | sed -e '/^Network Kanji Filter/!d' -e 's/.* Version \([1-9]\).*/\1/' `"
142   if [ "$nkf_version" = '1' -o "$nkf_version" = '0' ] ; then
143     err "nkf found but not seems 2.x (UTF-8 enabled) or later"
144   fi
145
146   # encls.php
147   encls="./encls.php"
148   if [ ! -f "$encls" ]
149   then err "encls not found"
150   else
151     php="` which php `"        || err "php-cli not found"
152     encls="$php `pwd`/$encls"
153   fi
154
155   convert(){
156     for list in "$@" ; do
157       # NOTE: Specify '-E'(From EUC-JP) otherwise skin file will be collapsed
158       nkf -Ew "$list" > "$list.$$.tmp" && mv "$list.$$.tmp" "$list" && echo "  $list"
159     done
160   }
161   convert_EUCJP2UTF8(){
162     for list in "$@" ; do
163       # Very rough conversion!
164       #sed 's/EUC-JP/UTF-8/g' "$list" > "$list.$$.tmp" && mv "$list.$$.tmp" "$list"
165       sed 's#^//UTF-8:##' "$list" > "$list.$$.tmp" && mv "$list.$$.tmp" "$list"
166     done
167   }
168 fi > /dev/null
169
170 if [ -z "$__zip" ]
171 then
172   which tar  || err "tar not found"
173   which gzip || err "gzip not found"
174 else
175   which zip  || err "zip not found"
176 fi > /dev/null
177
178 # Argument check --------------------------------------------
179
180 rel="$1"
181
182 if [ "$pkg_name" ] ; then
183   pkg_dir="$pkg_name"
184   tag="$rel"
185 else
186   tag="` check_versiontag "$rel" `" || exit 1
187   pkg_dir="${mod}-${rel}"
188   if [ "$__utf8" ] ; then
189     pkg_dir="${pkg_dir}_utf8"
190   fi
191 fi
192
193 # Export the module -----------------------------------------
194 test ! -d "$pkg_dir" || err "There's already a directory: $pkg_dir"
195
196 if [ -z "$__checkout" ]
197 then cmd="export"
198 else cmd="checkout"
199 fi
200
201 if [ "$__git" ] ; then
202   echo git clone "$gitrepo" "$pkg_dir"
203        git clone "$gitrepo" "$pkg_dir"
204   echo cd $pkg_dir 
205   cd $pkg_dir 
206   echo git reset --hard "$tag"
207        git reset --hard "$tag"
208
209   # Set file timestamp
210   for FILE in $(git ls-files); do
211     TIME=$(git log --pretty=format:%ci -n1 $FILE)
212     echo $TIME'\t'$FILE
213     STAMP=$(date -d "$TIME" +"%y%m%d%H%M.%S")
214     touch -t $STAMP $FILE
215   done
216
217   cd ..
218 else
219   exit
220   echo cvs -z3 -d "$CVSROOT" -q "$cmd" -r "$tag" -d "$pkg_dir" "$mod"
221        cvs -z3 -d "$CVSROOT" -q "$cmd" -r "$tag" -d "$pkg_dir" "$mod"
222 fi
223
224 test   -d "$pkg_dir" || err "There isn't a directory: $pkg_dir"
225
226 # Remove '.cvsignore' if exists -----------------------------
227 test -z "$__noremove" && {
228   if [ "$__git" ] ; then
229     echo rm -rf "$pkg_dir/.git"
230          rm -rf "$pkg_dir/.git"
231   fi
232   echo find "$pkg_dir" -type f -name '.cvsignore' "| xargs rm -f"
233        find "$pkg_dir" -type f -name '.cvsignore' | xargs rm -f
234 }
235
236
237 # Conversion ------------------------------------------------
238
239 if [ "$__utf8" ] ; then
240   echo "Converting EUC-JP => UTF-8 ..."
241   find "$pkg_dir" -type f \( -name "*.txt" -or -name "*.php" -or -name "*.lng"  -or -name "*.dat" -or -name "*.ref" \) |
242   while read line; do
243     case "$line" in
244       "$pkg_dir"/zh-CN.lng.php ) ;; # UTF-8 already, Do nothing
245       * ) convert "$line" ;;
246     esac
247   done
248
249   # Replace 'EUC-JP' => 'UTF-8'
250   ( cd "$pkg_dir" &&
251     convert_EUCJP2UTF8 lib/init.php skin/pukiwiki.skin*.php
252   )
253
254   # Filename encoded 'encoded-EUC-JP' to 'encoded-UTF-8'
255   echo "Renaming encoded-EUC-JP => encoded-UTF-8 ..."
256   ( cd "$pkg_dir" &&
257     for dir in wiki wiki.en cache; do
258       ( cd "$dir" &&
259         ls *.txt *.ref *.rel 2>/dev/null | while read line; do
260           target="`$encls "$line" 2>/dev/null`" || exit 1
261           if [ "x$line" != "x$target" ] ; then
262             echo " " mv "$dir/$line" "$dir/$target"
263                      mv "$line" "$target" || exit 1
264           fi
265         done
266       ) || exit 1
267     done
268   ) || err "stop."
269 fi
270
271 # chmod -----------------------------------------------------
272
273 chmod_pkg "$pkg_dir"
274
275 # Create a package ------------------------------------------
276
277 test ! -z "$__nopkg" && exit 0
278
279 ( cd "$pkg_dir"
280
281   # wiki.en/
282   target="wiki.en"
283   if [ -z "$__zip" ]
284   then tar cf - "$target" | gzip -9 > "$target".tgz
285   else zip -r9 "$target.zip" "$target"
286   fi
287   rm -Rf "$target"
288
289   # en documents
290   if [ -z "$__zip" ]
291   then gzip -9 *.en.txt
292   else
293     for list in *.en.txt ; do
294       zip  -9 "$list".zip "$list"
295       rm -f "$list"
296     done
297   fi
298 )
299
300 # Move / Copy *.ini.php files
301 if [ 'x' != "x$__copy_dist$__move_dist" ] ; then
302 ( cd "$pkg_dir"
303
304   find . -type f -name "*.ini.php" | while read file; do
305     dist_file="` echo "$file" | sed 's/ini\.php$/ini-dist.php/' `"
306     mv -f "$file" "$dist_file"
307     test "$__copy_dist" && cp -f "$dist_file" "$file"
308   done
309 )
310 fi
311
312 if [ -z "$__zip" ]
313 then
314   # Tar + gzip
315   echo tar cf - "$pkg_dir" \| gzip -9 \> "$pkg_dir.tar.gz"
316        tar cf - "$pkg_dir"  | gzip -9  > "$pkg_dir.tar.gz"
317 else
318   # Zip
319   echo zip -r9 "$pkg_dir.zip" "$pkg_dir"
320        zip -r9 "$pkg_dir.zip" "$pkg_dir"
321 fi
322