File: sh.mkshadow

package info (click to toggle)
shtool 1.6.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 400 kB
  • ctags: 5
  • sloc: perl: 379; makefile: 156; sh: 52
file content (141 lines) | stat: -rw-r--r-- 4,205 bytes parent folder | download
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
##
##  mkshadow -- Make a shadow tree through symbolic links
##  Copyright (c) 1998-2002 Ralf S. Engelschall <rse@engelschall.com>
##  Originally written for Apache
##
##  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
    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