Initial import from FreeBSD RELENG_4:
[games.git] / tools / tools / upgrade / move_aout_libs.sh
1 #!/bin/sh
2 #
3 # $FreeBSD: src/tools/tools/upgrade/move_aout_libs.sh,v 1.6 1999/08/28 00:54:34 peter Exp $
4
5 # Search for a.out libraries and move them to an aout subdirectory of
6 # the elf library directory.
7 #
8 # The arguments are the directories to search.
9 #
10 libdirs="$*"
11
12 # Create a temporary tool to get the timestamp of libraries. No, I don't
13 # want to use perl or whatever.
14 create_get_time_stamp ( )
15 {
16         echo "#include <stdio.h>" > /tmp/get_time_stamp.c
17         echo "#include <sys/stat.h>" >> /tmp/get_time_stamp.c
18         echo "int main(int argc, char *argv[]) {" >> /tmp/get_time_stamp.c
19         echo "int ierr; struct stat fs;" >> /tmp/get_time_stamp.c
20         echo "if ((ierr = stat(argv[1],&fs)) == 0)" >> /tmp/get_time_stamp.c
21         echo "printf(\"%ld\n\",(long) fs.st_mtime);" >> /tmp/get_time_stamp.c
22         echo "return (ierr); }" >> /tmp/get_time_stamp.c
23         gcc -o /tmp/get_time_stamp /tmp/get_time_stamp.c
24         rm /tmp/get_time_stamp.c
25         return
26 }
27
28 # Move an a.out library to the aout subdirectory of the elf directory.
29 move_file ( ) 
30 {
31         if test -d $dir/aout; then
32         else
33                 echo "Creating directory $dir/aout"
34                 mkdir $dir/aout
35                 ldconfig -m $dir/aout
36         fi
37         fname=${file#$dir/}
38         if test -f $dir/aout/$fname; then
39                 if test -x /tmp/get_time_stamp; then
40                 else
41                         create_get_time_stamp
42                 fi
43                 t1=`/tmp/get_time_stamp $dir/aout/$fname`
44                 t2=`/tmp/get_time_stamp $file`
45                 if test $t1 -gt $t2; then
46                         echo $file is older than $dir/aout/$fname
47                         answer=""
48                         while test "$answer" != "y" -a "$answer" != "n"; do
49                                 read -p "OK to delete the older file? (y/n) " answer
50                         done
51                         if test $answer = "y"; then
52                                 echo Deleting $file
53                                 chflags noschg $file
54                                 rm $file
55                         else
56                                 echo "You need to move $file out of $dir because that's an elf directory"
57                         fi
58                 else
59                         echo $dir/aout/$fname is older than $file
60                         answer=""
61                         while test "$answer" != "y" -a "$answer" != "n"; do
62                                 read -p "OK to overwrite the older file? (y/n) " answer
63                         done
64                         if test $answer = "y"; then
65                                 echo Overwriting $dir/aout/$fname with $file
66                                 chflags noschg $file
67                                 mv $file $dir/aout/$fname
68                                 ldconfig -R
69                         else
70                                 echo "You need to move $file out of $dir because that's an elf directory"
71                         fi
72                 fi
73         else
74                 echo Move $fname from $dir to $dir/aout
75                 chflags noschg $file
76                 mv $file $dir/aout/$fname
77                 ldconfig -R
78         fi
79         return
80 }
81
82 # Given a list of files in a directory, find those that are a.out
83 # libraries and move them.
84 move_if_aout ( ) 
85 {
86         # Check each library
87         for file in $files
88         do
89                 # Don't touch symbolic links yet. It's not clear how
90                 # they should be handled.
91                 if test -h $file; then
92                 else
93                         # Check that this is a normal file.
94                         if test -f $file; then
95                                 # Identify the file by magic
96                                 filemagic=`file $file`
97
98                                 # Check if the file is an a.out library
99                                 if expr "$filemagic" : ".*$aoutmagic"; then
100                                         # Move the a.out library
101                                         move_file
102                                 fi
103                         fi
104                 fi
105         done
106         return
107 }
108
109 # Only search the directories specified.
110 for dir in $libdirs
111 do
112         # Make sure the directory exists, or ldconfig will choke later.
113         mkdir -p $dir $dir/aout
114
115         echo "Searching library directory $dir for a.out libraries..."
116
117         # Get a list of archive libraries.
118         files=`ls $dir/*.a 2> /dev/null`
119
120         # a.out archive libraries look like this:
121         aoutmagic="current ar archive random library"
122
123         # Move each a.out archive library:
124         move_if_aout
125
126         # Get a list of shared libraries
127         files=`ls $dir/*.so.*.* 2> /dev/null`
128
129         # a.out shared libraries look like this:
130         aoutmagic="FreeBSD/i386 compact demand paged shared library"
131
132         # Move each a.out shared library:
133         move_if_aout
134 done
135
136 # If we created the time stamp program, delete it:
137 if test -x /tmp/get_time_stamp; then
138         rm /tmp/get_time_stamp
139 fi