1#! /bin/sh
2# @(#)rcs2sccs.sh	1.20 20/08/25 Copyright 2011-2018 J. Schilling
3#
4#
5# Id: rcs2sccs,v 1.12 90/10/04 20:52:23 kenc Exp Locker: kenc
6
7############################################################
8PATH=INS_BASE/SCCS_BIN_PREbin:$PATH:/usr/ccs/bin
9############################################################
10ex_code=0
11do_v6=""
12do_rm=FALSE
13is_schily_sccs=FALSE
14
15usage() {
16	echo "Usage: rcs2sccs2 [-help] [-rm] [-V] [-V6] [file...]"
17	echo "Options:"
18	echo "	-help	print this help"
19	echo "	-rm	Remove RCS files after completion"
20	echo "	-V	Print SCCS version and exit"
21	echo "	-V6	Create SCCS v6 history files"
22}
23
24while [ $# -ge 1 ]; do
25	case "$1" in
26	-rm | -remove)
27		do_rm=TRUE
28		shift
29		continue;;
30	-V | -version | --version)
31		echo "$0 PROVIDER-SCCS version VERSION VDATE (HOST_SUB)"
32		exit 0
33		;;
34	-V4)
35		do_v6="-V4"
36		shift
37		continue;;
38	-V6)
39		do_v6=-V6
40		shift
41		continue;;
42	'-?' | -help | --help)
43		usage
44		exit
45		;;
46	-*)
47		echo "Illegal option $1"
48		usage
49		exit 1
50		;;
51	*)
52		break;;
53	esac
54done
55
56if [ "$do_v6" = "-V6" ]; then
57	TZ=GMT
58	export TZ
59fi
60############################################################
61# Error checking
62#
63if [ ! -d SCCS ] ; then
64    mkdir SCCS
65fi
66
67vers=`sccs -V | grep 'schily.*version'`
68if [ -n "$vers" ]; then
69	is_schily_sccs=TRUE
70fi
71if [ "$is_schily_sccs" = FALSE ]; then
72	echo "No recent SCCS found"
73	exit 1
74fi
75
76logfile=/tmp/rcs2sccs_$$_log
77rm -f $logfile
78tmpfile=/tmp/rcs2sccs_$$_tmp
79rm -f $tmpfile
80emptyfile=/tmp/rcs2sccs_$$_empty
81echo -n "" > $emptyfile
82initialfile=/tmp/rcs2sccs_$$_init
83echo "Initial revision" > $initialfile
84sedfile=/tmp/rcs2sccs_$$_sed
85rm -f $sedfile
86revfile=/tmp/rcs2sccs_$$_rev
87rm -f $revfile
88commentfile=/tmp/rcs2sccs_$$_comment
89rm -f $commentfile
90
91#
92# NOTE: Signal numbers other then 1, 2, 3, 6, 9, 14, and 15 are not portable.
93#
94trap "rm -f $logfile $tmpfile $emptyfile $initialfile $sedfile $revfile $commentfile; exit" 0 1 2 3 15
95
96# create the sed script
97cat > $sedfile << EOF
98s,;Id;,%Z%%M% %I% %E%,g
99s,;SunId;,%Z%%M% %I% %E%,g
100s,;RCSfile;,%M%,g
101s,;Revision;,%I%,g
102s,;Date;,%E%,g
103s,;Id:.*;,%Z%%M% %I% %E%,g
104s,;SunId:.*;,%Z%%M% %I% %E%,g
105s,;RCSfile:.*;,%M%,g
106s,;Revision:.*;,%I%,g
107s,;Date:.*;,%E%,g
108EOF
109sed -e 's/;/\\$/g' $sedfile > $tmpfile
110cp $tmpfile $sedfile
111
112if sort -k 1,1 /dev/null 2>/dev/null; then
113	sort_args='-k 1 -k 2 -k 3 -k 4 -k 5 -k 6 -k 7 -k 8 -k 9'
114else
115	sort_args='+0 +1 +2 +3 +4 +5 +6 +7 +8'
116fi
117
118errlog()
119{
120	tail $logfile
121	echo "See complete log in $logfile"
122	logfile=""
123}
124
125############################################################
126# Convert one RCS history file
127#
128rcs_to_sccs() {
129    vfile="$1"
130
131    # get rid of the ",v" at the end of the name
132    file=`echo "$vfile" | sed -e 's/,v$//'`
133
134    # work on each rev of that file in ascending order
135    firsttime=1
136    if sccs istext $do_v6 -s "$vfile"; then
137	encode=""
138    else
139	encode=-b
140    fi
141    if test -x "$vfile"; then
142	exec=TRUE
143    else
144	exec=FALSE
145    fi
146    rlog $file | grep "^revision [0-9][0-9]*\." | awk '{print $2}' | sed -e 's/\./ /g' | sort -n -u $sort_args | sed -e 's/ /./g' > $revfile
147    for rev in `cat $revfile`; do
148        if [ $? != 0 ]; then
149		echo ERROR - revision
150		exit 1
151	fi
152        # get file into current dir and get stats
153	if [ "$do_v6" = "-V6" ]; then
154	        date=`rlog -r$rev $file | grep "^date: " | awk '{print $2; exit}'`
155	        time=`rlog -r$rev $file | grep "^date: " | awk '{print $3; exit}' | sed -e 's/;/+0000/'`
156	else
157	        date=`rlog -r$rev $file | grep "^date: " | awk '{print $2; exit}' | \
158				sed -e 's,^19\([0-9][0-9]/\),\1,' -e 's,^20\([0-9][0-9]/\),\1,'`
159	        time=`rlog -r$rev $file | grep "^date: " | awk '{print $3; exit}' | sed -e 's/;//'`
160	fi
161        author=`rlog -r$rev $file | grep "^date: " | awk '{print $5; exit}' | sed -e 's/;//'`
162	date="$date $time"
163        echo ""
164	rlog -r$rev $file | sed -e '/^branches: /d' -e '1,/^date: /d' -e '/^===========/d' -e 's/$/\\/' > $commentfile
165        echo "==> file $file, rev=$rev, date=$date, author=$author"
166	rm -f $file
167        co -r$rev $file >> $logfile  2>&1
168        if [ $? != 0 ]; then
169		echo ERROR - co
170		errlog
171		exit 1
172	fi
173        echo checked out of RCS
174
175	if [ "$encode" = "" ]; then
176		# add SCCS keywords in place of RCS keywords
177		sed -f $sedfile $file > $tmpfile
178		if [ $? != 0 ]; then
179			echo ERROR - sed
180			exit 1
181		fi
182		echo performed keyword substitutions
183		rm -f $file
184		cp $tmpfile $file
185	fi
186
187        # check file into SCCS
188        if [ "$firsttime" = "1" ]; then
189            firsttime=0
190	    echo about to do sccs admin
191            echo sccs admin $do_v6 $encode -n -i$file $file < $commentfile
192            sccs admin $do_v6 $encode -n -i$file $file < $commentfile >> $logfile 2>&1
193            if [ $? != 0 ]; then
194		    echo ERROR - sccs admin
195		    errlog
196		    exit 1
197	    fi
198	    if [ "$do_v6" = "-V6" ]; then
199		sed -e "s;^c date and time created ..../../.. ..:..:..+0000 by [^ ]*;c date and time created $date by $author;" SCCS/s.$file > $tmpfile
200	    else
201		sed -e "s;^c date and time created ../../.. ..:..:.. by [^ ]*;c date and time created $date by $author;" SCCS/s.$file > $tmpfile
202	    fi
203	    rm -f SCCS/s.$file
204	    cp $tmpfile SCCS/s.$file
205	    chmod 444 SCCS/s.$file
206	    sccs admin -z $file
207	    if [ $? != 0 ]; then
208		    echo ERROR - sccs admin -z
209		    exit 1
210	    fi
211            echo initial rev checked into SCCS
212        else
213	    #
214	    # Unfortunately, rlog(1) does not print the "next" information that
215	    # points to the predecessor revision, so we tell sccs to check out
216	    # the highest revision of the same branch.
217	    #
218	    case $rev in
219	    *.*.*.*)
220		brev=`echo $rev | sed -e 's/\.[0-9]*$//'`
221		sccs admin -fb $file 2>>$logfile
222		echo sccs get -e -p -r$brev $file
223		sccs get -e -p -r$brev $file >/dev/null 2>>$logfile
224		;;
225	    *)
226		brev=`echo $rev | sed -e 's/\.[0-9]*$//'`
227		echo sccs get -e -p -r$brev $file
228		sccs get -e -p -r$brev $file >/dev/null 2>> $logfile
229		;;
230	    esac
231	    if [ $? != 0 ]; then
232		    echo ERROR - sccs get
233		    errlog
234		    exit 1
235	    fi
236	    sccs delta $file < $commentfile >> $logfile 2>&1
237            if [ $? != 0 ]; then
238		    echo ERROR - sccs delta -r$rev $file
239		    errlog
240		    exit 1
241	    fi
242            echo checked into SCCS
243	fi
244	if [ "$do_v6" = "-V6" ]; then
245		sed -e "s;^d D $rev ..../../.. ..:..:..+0000 [^ ][^ ]*;d D $rev $date $author;" SCCS/s.$file > $tmpfile
246	else
247		sed -e "s;^d D $rev ../../.. ..:..:.. [^ ][^ ]*;d D $rev $date $author;" SCCS/s.$file > $tmpfile
248	fi
249	rm -f SCCS/s.$file
250	cp $tmpfile SCCS/s.$file
251	chmod 444 SCCS/s.$file
252	if [ "$exec" = TRUE ]; then
253		chmod +x SCCS/s.$file
254	fi
255	sccs admin -z $file
256        if [ $? != 0 ]; then
257		echo ERROR - sccs admin -z
258		exit 1
259	fi
260    done
261    if [ -s "$revfile" ]; then
262	rm -f $file
263    else
264	echo No Revisions in "$vfile"
265	ex_code=1
266	do_rm=FALSE
267    fi
268}
269
270############################################################
271# Loop over every RCS file in RCS dir
272#
273if [ $# -gt 0 ]; then
274	for vfile in "$@"; do
275		rcs_to_sccs "$vfile"
276	done
277else
278	for vfile in *,v; do
279		rcs_to_sccs "$vfile"
280	done
281fi
282
283############################################################
284# Clean up
285#
286echo cleaning up...
287rm -f $logfile $tmpfile $emptyfile $initialfile $sedfile $revfile $commentfile
288echo ===================================================
289echo "       Conversion Completed Successfully"
290echo ===================================================
291
292if [ "$do_rm" = TRUE ]; then
293	if [ $# -gt 0 ]; then
294		rm -f "$@"
295	else
296		rm -f *,v
297	fi
298fi
299
300exit $ex_code
301