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