]> pd.if.org Git - zpackage/blob - zpm-add
add -m option to add to set file mode
[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 addcontent=1
36 complete=0
37 mode=
38 # option for "multipackage" just to let the system know that's what you meant
39 # option to take filenames from stdin
40 # parse package, version, release from file if not given
41 # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets
42 while getopts :f:vr:l:P:S:cu:g:NC opt; do
43         case $opt in
44                 N) addcontent=0 ;;
45                 f) pkgfile="$OPTARG" ;;
46                 P) prefix="$OPTARG" ;;
47                 S) strip=$(cleanpath "$OPTARG"); ;;
48                 t) tags="$tags $OPTARG" ;;
49                 c) isconfig=1 ;;
50                 u) username="$OPTARG" ;;
51                 g) groupname="$OPTARG" ;;
52                 m) mode="$OPTARG" ;;
53                 v) verbose=$((verbose + 1)) ;;
54                 C) complete=1 ;;
55                 *) echo 'unknown option' $OPTARG; exit 1 ;;
56         esac
57 done
58 shift $((OPTIND - 1))
59
60 if [ $isconfig -eq 1 ]; then
61         tags="$tags configuration"
62 fi
63
64 if [ $verbose -gt 2 ]; then
65         set -x
66 fi
67
68 pkgid="$1"
69 shift
70 eval $(zpm parse -E $pkgid)
71
72 if [ -z "$pkgfile" ]; then
73         pkgfile=$ZPM_PACKAGE_FILE
74 fi
75
76 if [ -z "$release" ]; then
77         if [ -z "$pkgfile" ]; then
78                 die "cannot determine package file"
79         else
80                 pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
81                 if [ -z "$pkgstr" ]; then
82                         die "unable to find package id for $pkgid in $pkgfile"
83                 fi
84                 pkgid=$pkgstr
85                 # need to reparse the new package id
86                 eval $(zpm parse -E $pkgid)
87         fi
88 fi
89
90 # look for a .zpm file here
91 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
92         pkgfile="$pkgid.zpm"
93 fi
94
95 if [ -z "$pkgfile" ]; then
96         die "cannot determine package file"
97 fi
98
99 # check for package file
100 if [ ! -f "$pkgfile" ]; then
101         echo $pkgfile does not exist
102         exit 1
103 fi
104
105 set -e
106 zpm test -v $pkgfile
107 set +e
108
109 if [ $verbose -gt 0 ]; then
110         echo adding to $pkgfile $pkgid
111 fi
112
113 package=$(zpm quote "$name")
114 pkgver=$(zpm quote "$version")
115 pkgrel=$(zpm quote "$release")
116
117 #strip=$(cleanpath "$strip")
118 for path in $*; do
119         #echo adding $path
120         mtime=$(zpm stat -f '%y' $path)
121         uid=$(zpm stat -f '%u' $path)
122         gid=$(zpm stat -f '%g' $path)
123
124         if [ -z "$mode" ]; then
125                 mode=$(zpm stat -f '%a' $path)
126         fi
127
128         # only stat the file for the user and group name if not set on the
129         # command line
130         : ${username:=$(zpm stat -f '%U' $path)}
131         : ${groupname:=$(zpm stat -f '%G' $path)}
132
133         rpath="$path"
134
135         rpath=$(cleanpath "$path")
136
137         # strip off leading slash
138         rpath=${rpath#/}
139
140         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
141                 continue
142         fi
143
144         if [ ! -z "$strip" ]; then
145                 rpath=${rpath#$strip}
146                 rpath=${rpath#/}
147         fi
148
149         if [ -z "$rpath" ]; then
150                 die "$path resolves to nothing"
151         fi
152
153         prefix=$(cleanpath "$prefix")
154         if [ ! -z "$prefix" ]; then
155                 rpath="$prefix/$rpath"
156         fi
157
158         # ensure all paths are absolute
159         rpath=/${rpath#/}
160
161         filetype=$(zpm stat -l -f '%t' "$path")
162         hash='NULL'
163         target='NULL'
164         case "$filetype" in
165                 regular)
166                         filetype=r
167                         if [ $addcontent -eq 1 ]; then
168                                 hash=$(zpm addfile $pkgfile "$path")
169                                 if [ $? -ne 0 ]; then
170                                         die "zpm addfile failed ($?): $pkgfile $path" 
171                                 fi
172                         else
173                                 hash=$(zpm hash "$path")
174                         fi
175                         hash="'$hash'"
176                         ;;
177                 directory)
178                         filetype=d
179                         ;;
180                 symlink)
181                         filetype=l
182                         target=$(readlink $path)
183                         target="'$target'"
184                         ;;
185         esac
186
187         # TODO check that we have such a package,version,release
188         #cat <<EOS
189         zpm shell $pkgfile <<EOS
190 begin;
191 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
192 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
193 commit;
194 EOS
195
196 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
197 if [ $verbose -gt 1 ]; then
198         printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname $path
199 elif [ $verbose -gt 0 ]; then
200         printf "%s\n" $path
201 fi
202
203 done
204
205 if [ $complete -eq 1 ]; then
206         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
207         zpm packagehash -f $pkgfile -s -q $pkgid
208 else
209         zpm pkg -f $pkgfile $pkgid hash=
210 fi