1#! @CSH@ -f
2
3# Copyright (C) 1995-2005 The Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2, or (at your option)
8# any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# Sccs2rcs is a script to convert an existing SCCS
16# history into an RCS history without losing any of
17# the information contained therein.
18# It has been tested under the following OS's:
19#     SunOS 3.5, 4.0.3, 4.1
20#     Ultrix-32 2.0, 3.1
21#
22# Things to note:
23#   + It will NOT delete or alter your ./SCCS history under any circumstances.
24#
25#   + Run in a directory where ./SCCS exists and where you can
26#       create ./RCS
27#
28#   + /usr/local/bin is put in front of the default path.
29#     (SCCS under Ultrix is set-uid sccs, bad bad bad, so
30#     /usr/local/bin/sccs here fixes that)
31#
32#   + Date, time, author, comments, branches, are all preserved.
33#
34#   + If a command fails somewhere in the middle, it bombs with
35#     a message -- remove what it's done so far and try again.
36#         "rm -rf RCS; sccs unedit `sccs tell`; sccs clean"
37#     There is no recovery and exit is far from graceful.
38#     If a particular module is hanging you up, consider
39#     doing it separately; move it from the current area so that
40#     the next run will have a better chance or working.
41#     Also (for the brave only) you might consider hacking
42#     the s-file for simpler problems:  I've successfully changed
43#     the date of a delta to be in sync, then run "sccs admin -z"
44#     on the thing.
45#
46#   + After everything finishes, ./SCCS will be moved to ./old-SCCS.
47#
48# This file may be copied, processed, hacked, mutilated, and
49# even destroyed as long as you don't tell anyone you wrote it.
50#
51# Ken Cox
52# Viewlogic Systems, Inc.
53# kenstir@viewlogic.com
54# ...!harvard!cg-atla!viewlog!kenstir
55#
56# Various hacks made by Brian Berliner before inclusion in CVS contrib area.
57#
58# Modified to detect SCCS binary files. If binary, skip the keyword
59# substitution and flag the RCS file as binary (using rcs -i -kb).
60#      -Allan G. Schrum schrum@ofsoptics.com agschrum@mindspring.com
61# Fri Sep 26 10:40:40 EDT 2003
62#
63
64
65#we'll assume the user set up the path correctly
66# for the Pmax, /usr/ucb/sccs is suid sccs, what a pain
67#   /usr/local/bin/sccs should override /usr/ucb/sccs there
68set path = (/usr/local/bin $path)
69
70
71############################################################
72# Error checking
73#
74if (! -w .) then
75    echo "Error: ./ not writeable by you."
76    exit 1
77endif
78if (! -d SCCS) then
79    echo "Error: ./SCCS directory not found."
80    exit 1
81endif
82set edits = (`sccs tell`)
83if ($#edits) then
84    echo "Error: $#edits file(s) out for edit...clean up before converting."
85    exit 1
86endif
87if (-d RCS) then
88    echo "Warning: RCS directory exists"
89    if (`ls -a RCS | wc -l` > 2) then
90        echo "Error: RCS directory not empty"
91        exit 1
92    endif
93else
94    mkdir RCS
95endif
96
97sccs clean
98
99set logfile = /tmp/sccs2rcs_$$_log
100rm -f $logfile
101set tmpfile = /tmp/sccs2rcs_$$_tmp
102rm -f $tmpfile
103set emptyfile = /tmp/sccs2rcs_$$_empty
104echo -n "" > $emptyfile
105set initialfile = /tmp/sccs2rcs_$$_init
106echo "Initial revision" > $initialfile
107set sedfile = /tmp/sccs2rcs_$$_sed
108rm -f $sedfile
109set revfile = /tmp/sccs2rcs_$$_rev
110rm -f $revfile
111
112# the quotes surround the dollar signs to fool RCS when I check in this script
113set sccs_keywords = (\
114    '%W%[ 	]*%G%'\
115    '%W%[ 	]*%E%'\
116    '%W%'\
117    '%Z%%M%[ 	]*%I%[ 	]*%G%'\
118    '%Z%%M%[ 	]*%I%[ 	]*%E%'\
119    '%M%[ 	]*%I%[ 	]*%G%'\
120    '%M%[ 	]*%I%[ 	]*%E%'\
121    '%M%'\
122    '%I%'\
123    '%G%'\
124    '%E%'\
125    '%U%')
126set rcs_keywords = (\
127    '$'Id'$'\
128    '$'Id'$'\
129    '$'Id'$'\
130    '$'SunId'$'\
131    '$'SunId'$'\
132    '$'Id'$'\
133    '$'Id'$'\
134    '$'RCSfile'$'\
135    '$'Revision'$'\
136    '$'Date'$'\
137    '$'Date'$'\
138    '')
139
140
141############################################################
142# Get some answers from user
143#
144echo ""
145echo "Do you want to be prompted for a description of each"
146echo "file as it is checked in to RCS initially?"
147echo -n "(y=prompt for description, n=null description) [y] ?"
148set ans = $<
149if ((_$ans == _) || (_$ans == _y) || (_$ans == _Y)) then
150    set nodesc = 0
151else
152    set nodesc = 1
153endif
154echo ""
155echo "The default keyword substitutions are as follows and are"
156echo "applied in the order specified:"
157set i = 1
158while ($i <= $#sccs_keywords)
159#    echo '	'\"$sccs_keywords[$i]\"'	==>	'\"$rcs_keywords[$i]\"
160    echo "	$sccs_keywords[$i]	==>	$rcs_keywords[$i]"
161    @ i = $i + 1
162end
163echo ""
164echo -n "Do you want to change them [n] ?"
165set ans = $<
166if ((_$ans != _) && (_$ans != _n) && (_$ans != _N)) then
167    echo "You can't always get what you want."
168    echo "Edit this script file and change the variables:"
169    echo '    $sccs_keywords'
170    echo '    $rcs_keywords'
171else
172    echo "good idea."
173endif
174
175# create the sed script
176set i = 1
177while ($i <= $#sccs_keywords)
178    echo "s,$sccs_keywords[$i],$rcs_keywords[$i],g" >> $sedfile
179    @ i = $i + 1
180end
181
182onintr ERROR
183
184sort -k 1,1 /dev/null >& /dev/null
185if ($status == 0) then
186    set sort_each_field = '-k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9'
187else
188    set sort_each_field = '+0 +1 +2 +3 +4 +5 +6 +7 +8'
189endif
190
191############################################################
192# Loop over every s-file in SCCS dir
193#
194foreach sfile (SCCS/s.*)
195    # get rid of the "s." at the beginning of the name
196    set file = `echo $sfile:t | sed -e "s/^..//"`
197
198    # work on each rev of that file in ascending order
199    set firsttime = 1
200
201    # Only scan the file up to the "I" keyword, then see if
202    # the "f" keyword is set to binary. The SCCS file has
203    # <ctrl>-aI denoting the start of the file (or end of header).
204    set binary = (`sed -e '/^.I/,$d' < $sfile | grep '^.f e 1$'`)
205    #if ($#binary) then
206    #    echo This is a binary file
207    #else
208    #    echo This is not a binary file
209    #endif
210
211    sccs prs $file | grep "^D " | @AWK@ '{print $2}' | sed -e 's/\./ /g' | sort -n -u $sort_each_field | sed -e 's/ /./g' > $revfile
212    foreach rev (`cat $revfile`)
213        if ($status != 0) goto ERROR
214
215        # get file into current dir and get stats
216
217	# Is the substr stuff and the +0 in the following awk script really
218	# necessary?  It seems to me that if we didn't find the date format
219	# we expected in the output we have other problems.
220	# Note: Solaris awk does not like the following line. Use gawk
221	# mawk, or nawk instead.
222	set date = `sccs prs -r$rev $file | @AWK@ '/^D / {print (substr($3,0,2)+0<70?20:19) $3, $4; exit}'`
223        set author = `sccs prs -r$rev $file | @AWK@ '/^D / {print $5; exit}'`
224        echo ""
225        echo "==> file $file, rev=$rev, date=$date, author=$author"
226        sccs edit -r$rev $file >>& $logfile
227        if ($status != 0) goto ERROR
228        echo checked out of SCCS
229
230        # add RCS keywords in place of SCCS keywords (only if not binary)
231        if ($#binary == 0) then
232            sed -f $sedfile $file > $tmpfile
233            if ($status != 0) goto ERROR
234            echo performed keyword substitutions
235            cp $tmpfile $file
236        endif
237
238        # check file into RCS
239        if ($firsttime) then
240            set firsttime = 0
241
242            if ($#binary) then
243                echo this is a binary file
244                # Mark initial, empty file as binary
245                rcs -i -kb -t$emptyfile $file
246            endif
247
248            if ($nodesc) then
249		echo about to do ci
250                echo ci -f -r$rev -d"$date" -w$author -t$emptyfile $file
251                ci -f -r$rev -d"$date" -w$author -t$emptyfile $file < $initialfile >>& $logfile
252                if ($status != 0) goto ERROR
253                echo initial rev checked into RCS without description
254            else
255                echo ""
256                echo Enter a brief description of the file $file \(end w/ Ctrl-D\):
257                cat > $tmpfile
258                ci -f -r$rev -d"$date" -w$author -t$tmpfile $file < $initialfile >>& $logfile
259                if ($status != 0) goto ERROR
260                echo initial rev checked into RCS
261            endif
262        else
263            # get RCS lock
264	    set lckrev = `echo $rev | sed -e 's/\.[0-9]*$//'`
265	    if ("$lckrev" =~ [0-9]*.*) then
266		# need to lock the brach -- it is OK if the lock fails
267		rcs -l$lckrev $file >>& $logfile
268	    else
269		# need to lock the trunk -- must succeed
270                rcs -l $file >>& $logfile
271                if ($status != 0) goto ERROR
272	    endif
273            echo got lock
274            sccs prs -r$rev $file | grep "." > $tmpfile
275            # it's OK if grep fails here and gives status == 1
276            # put the delta message in $tmpfile
277            ed $tmpfile >>& $logfile <<EOF
278/COMMENTS
2791,.d
280w
281q
282EOF
283            ci -f -r$rev -d"$date" -w$author $file < $tmpfile >>& $logfile
284            if ($status != 0) goto ERROR
285            echo checked into RCS
286        endif
287        sccs unedit $file >>& $logfile
288        if ($status != 0) goto ERROR
289    end
290    rm -f $file
291end
292
293
294############################################################
295# Clean up
296#
297echo cleaning up...
298mv SCCS old-SCCS
299rm -f $tmpfile $emptyfile $initialfile $sedfile
300echo ===================================================
301echo "       Conversion Completed Successfully"
302echo ""
303echo "         SCCS history now in old-SCCS/"
304echo ===================================================
305set exitval = 0
306goto cleanup
307
308ERROR:
309foreach f (`sccs tell`)
310    sccs unedit $f
311end
312echo ""
313echo ""
314echo Danger\!  Danger\!
315echo Some command exited with a non-zero exit status.
316echo Log file exists in $logfile.
317echo ""
318echo Incomplete history in ./RCS -- remove it
319echo Original unchanged history in ./SCCS
320set exitval = 1
321
322cleanup:
323# leave log file
324rm -f $tmpfile $emptyfile $initialfile $sedfile $revfile
325
326exit $exitval
327