File: sh.mkshadow

package info (click to toggle)
shtool 2.0.8-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 808 kB
  • sloc: perl: 399; makefile: 110; sh: 42
file content (210 lines) | stat: -rw-r--r-- 5,648 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
##
##  mkshadow -- Make a shadow tree through symbolic links
##  Copyright (c) 1998-2008 Ralf S. Engelschall <rse@engelschall.com>
##
##  This file is part of shtool and free software; you can redistribute
##  it and/or modify it under the terms of the GNU General Public
##  License as published by the Free Software Foundation; either version
##  2 of the License, or (at your option) any later version.
##
##  This file is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
##  General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
##  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
##

str_tool="mkshadow"
str_usage="[-v|--verbose] [-t|--trace] [-a|--all] <src-dir> <dst-dir>"
arg_spec="2="
opt_spec="v.t.a."
opt_alias="v:verbose,t:trace,a:all"
opt_v=no
opt_t=no
opt_a=no

. ./sh.common

#   source and destination directory
src=`echo "$1" | sed -e 's:/$::' -e 's:^\./\(.\):\1:'`
dst=`echo "$2" | sed -e 's:/$::' -e 's:^\./\(.\):\1:'`

#   check whether source exists
if [ ! -d $src ]; then
    echo "$msgprefix:Error: source directory not found: \`$src'" 1>&2
    shtool_exit 1
fi

#   determine if one of the paths is an absolute path,
#   because then we have to use an absolute symlink
oneisabs=0
case $src in
    /* ) oneisabs=1 ;;
esac
case $dst in
    /* ) oneisabs=1 ;;
esac

#   determine reverse directory for destination directory
dstrevdir=''
if [ $oneisabs = 0 ]; then
    #   derive reverse path from forward path
    pwd=`pwd`
    OIFS="$IFS"; IFS='/'
    for pe in $dst; do
        if [ "x$pe" = "x.." ]; then
            OIFS2="$IFS"; IFS="$DIFS"
            eval `echo "$pwd" |\
                  sed -e 's:\([^/]*\)$:; dir="\1":' \
                      -e 's:^\(.*\)/[^/]*;:pwd="\1";:'\
                      -e 's:^;:pwd="";:'`
            dstrevdir="$dir/$dstrevdir"
            IFS="$OIFS2"
        else
            dstrevdir="../$dstrevdir"
        fi
    done
    IFS="$OIFS"
else
    src="`cd $src; pwd`";
fi

#   create directory tree at destination
if [ ! -d $dst ]; then
    if [ ".$opt_t" = .yes ]; then
        echo "mkdir $dst" 1>&2
    fi
    mkdir $dst
fi
if [ ".$opt_a" = .yes ]; then
    DIRS=`cd $src; find . -type d -print |\
          sed -e '/^\.$/d' -e 's:^\./::'`
else
    DIRS=`cd $src; find . -type d -print |\
          sed -e '/\/CVS/d' -e '/^\.$/d' -e 's:^\./::'`
fi
for dir in $DIRS; do
    if [ ".$opt_t" = .yes ]; then
        echo "mkdir $dst/$dir" 1>&2
    fi
    mkdir $dst/$dir
done

#   fill directory tree with symlinks to files
if [ ".$opt_a" = .yes ]; then
    FILES="`cd $src; find . -depth -print |\
            sed -e 's/^\.\///'`"
else
    FILES="`cd $src; find . -depth -print |\
            sed -e '/\.o$/d' -e '/\.a$/d' -e '/\.so$/d' \
                -e '/\.cvsignore$/d' -e '/\/CVS/d' \
                -e '/\/\.#/d' -e '/\.orig$/d' \
                -e 's/^\.\///'`"
fi
for file in $FILES; do
     #  don't use `-type f' above for find because of symlinks
     if [ -d "$src/$file" ]; then
         continue
     fi
     basename=`echo $file | sed -e 's:^.*/::'`
     dir=`echo $file | sed -e 's:[^/]*$::' -e 's:/$::' -e 's:$:/:' -e 's:^/$::'`
     from=`echo "$src/$file" | sed -e 's/^\.\///'`
     to="$dst/$dir$basename"
     if [ $oneisabs = 0 ]; then
         if [ ".$dir" != . ]; then
             subdir=`echo $dir | sed -e 's:/$::'`
             #   derive reverse path from forward path
             revdir=''
             OIFS="$IFS"; IFS='/'
             for pe in $subdir; do
                 revdir="../$revdir"
             done
             IFS="$OIFS"
             #   finalize from
             from="$revdir$from"
         fi
         from="$dstrevdir$from"
     fi
     if [ ".$opt_v" = .yes ]; then
         echo "    $to" 1>&2
     fi
     if [ ".$opt_t" = .yes ]; then
         echo "ln -s $from $to" 1>&2
     fi
     ln -s $from $to
done

shtool_exit 0

##
##  manual page
##

=pod

=head1 NAME

B<shtool-mkshadow> - B<GNU shtool> create shadow tree using symlinks

=head1 SYNOPSIS

B<shtool mkshadow>
[B<-v>|B<--verbose>]
[B<-t>|B<--trace>]
[B<-a>|B<--all>]
I<src-dir>
I<dst-dir>

=head1 DESCRIPTION

This command creates a shadow tree of I<src-dir> under I<dst-dir> by
recreating the directory hierarchy of I<src-dir> under I<dst-dir> and by
creating the files of I<src-dir> by linking them into the corresponding
directories under I<dst-dir> via symbolic links. When I<src-dir> can
be reached via relative paths from I<dst-dir>, relative symbolic links
are used, too. This high-level functionality is originally designed for
developers to create copies of source trees.

=head1 OPTIONS

The following command line options are available.

=over 4

=item B<-v>, B<--verbose>

Display some processing information.

=item B<-t>, B<--trace>

Enable the output of the essential shell commands which are executed.

=item B<-a>, B<--all>

Really shadow all files and directories in I<src-dir>. Default is to
skip CVS related files and directories, backup files, object files, etc.


=back

=head1 EXAMPLE

 #   shell script
 shtool mkshadow -v -a . /tmp/shadow

=head1 HISTORY

The B<GNU shtool> B<mkshadow> command was originally written by Ralf S.
Engelschall E<lt>rse@engelschall.comE<gt> in 1998 for B<Apache>. It was
later revised and taken over into B<GNU shtool>.

=head1 SEE ALSO

shtool(1), ln(1).

=cut