]> pd.if.org Git - zpackage/blob - zpm-add
support symlinks in zpm-add
[zpackage] / zpm-add
1 #!/bin/sh
2
3 # zpm add -f pkgfile pkgid [ files ]
4
5 # no package file? take from ZPM_PACKAGE_FILE
6 # no pkgid ? take from ZPM_PACKAGE_ID
7 # no id impossible
8
9 die() {
10         echo $* 1>&2
11         exit 1
12 }
13
14 # basic cleanup on a path
15 cleanpath() {
16         clean="$1"
17         if [ -z "$clean" ]; then printf ''; fi
18
19         # multiple slashes
20         clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
21         # curdir
22         clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
23         # leading curdir
24         clean=${clean#./}
25         # trailing curdir
26         clean=${clean%/.}
27         # trailing slash
28         clean=${clean%/}
29         printf "%s" "$clean"
30 }
31
32 verbose=0
33 tags=
34 isconfig=0
35 # option for "multipackage" just to let the system know that's what you meant
36 # option to take filenames from stdin
37 # parse package, version, release from file if not given
38 # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets
39 while getopts :f:vr:l:P:S:cu:g: opt; do
40         case $opt in
41                 f) pkgfile="$OPTARG" ;;
42                 P) prefix="$OPTARG" ;;
43                 S) strip=$(cleanpath "$OPTARG"); ;;
44                 t) tags="$tags $OPTARG" ;;
45                 c) isconfig=1 ;;
46                 u) username="$OPTARG" ;;
47                 g) groupname="$OPTARG" ;;
48                 v) verbose=1 ;;
49                 *) echo 'unknown option' $OPTARG; exit 1 ;;
50         esac
51 done
52 shift $((OPTIND - 1))
53
54 if [ $isconfig -eq 1 ]; then
55         tags="$tags configuration"
56 fi
57
58 if [ $verbose -gt 1 ]; then
59         set -x
60 fi
61
62 pkgid="$1"
63 shift
64 eval $(zpm parse -E $pkgid)
65
66 if [ -z "$pkgfile" ]; then
67         pkgfile=$ZPM_PACKAGE_FILE
68 fi
69
70 if [ -z "$release" ]; then
71         if [ -z "$pkgfile" ]; then
72                 die "cannot determine package file"
73         else
74                 pkgstr=$(zpm findpkg $pkgfile $pkgid)
75                 if [ -z "$pkgstr" ]; then
76                         die "unable to find package id for $pkgid in $pkgfile"
77                 fi
78                 pkgid=$pkgstr
79                 # need to reparse the new package id
80                 eval $(zpm parse -E $pkgid)
81         fi
82 fi
83
84 # look for a .zpm file here
85 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
86         pkgfile="$pkgid.zpm"
87 fi
88
89 if [ -z "$pkgfile" ]; then
90         die "cannot determine package file"
91 fi
92
93 # check for package file
94 if [ ! -f "$pkgfile" ]; then
95         echo $pkgfile does not exist
96         exit 1
97 fi
98
99 set -e
100 zpm test -v $pkgfile
101 set +e
102
103 if [ $verbose -gt 0 ]; then
104         echo adding to $pkgfile $pkgid
105 fi
106
107 package=$(zpm quote "$name")
108 pkgver=$(zpm quote "$version")
109 pkgrel=$(zpm quote "$release")
110
111 #strip=$(cleanpath "$strip")
112 for path in $*; do
113         #echo adding $path
114         mtime=$(zpm stat -f '%y' $path)
115         uid=$(zpm stat -f '%u' $path)
116         gid=$(zpm stat -f '%g' $path)
117         : ${username:=$(zpm stat -f '%U' $path)}
118         : ${groupname:=$(zpm stat -f '%G' $path)}
119         mode=$(zpm stat -f '%a' $path)
120         rpath="$path"
121
122         rpath=$(cleanpath "$path")
123
124         # strip off leading slash
125         rpath=${rpath#/}
126
127         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
128                 continue
129         fi
130
131         if [ ! -z "$strip" ]; then
132                 echo "stripping $strip"
133                 rpath=${rpath#$strip}
134                 rpath=${rpath#/}
135         fi
136
137         if [ -z "$rpath" ]; then
138                 die "$path resolves to nothing"
139         fi
140
141         prefix=$(cleanpath "$prefix")
142         if [ ! -z "$prefix" ]; then
143                 rpath="$prefix/$rpath"
144         fi
145
146         filetype=$(zpm stat -l -f '%t' "$path")
147         hash='NULL'
148         target='NULL'
149         case "$filetype" in
150                 regular)
151                         filetype=r
152                         hash=$(zpm addfile $pkgfile "$path")
153                         if [ $? -ne 0 ]; then
154                                 die "zpm addfile failed ($?): $pkgfile $path" 
155                         fi
156                         hash="'$hash'"
157                         ;;
158                 directory)
159                         filetype=d
160                         ;;
161                 symlink)
162                         filetype=l
163                         target=$(readlink $path)
164                         target="'$target'"
165                         ;;
166         esac
167
168         # TODO check that we have such a package,version,release
169         #cat <<EOS
170         zpm shell $pkgfile <<EOS
171 PRAGMA foreign_keys = ON;
172 begin;
173 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
174 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
175 commit;
176 EOS
177
178 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
179 printf "%s\n" $path
180 done