]> pd.if.org Git - zpackage/blob - zpm-addtopackage
cleanup package tests
[zpackage] / zpm-addtopackage
1 #!/bin/sh
2
3 pkgver=${ZPMPKGVER:-1.0}
4 pkgrel=${ZPMPKGREL:-1}
5
6 die() {
7         echo $* 1>&2
8         exit 1
9 }
10
11 # basic cleanup on a path
12 cleanpath() {
13         clean="$1"
14         if [ -z "$clean" ]; then printf ''; fi
15
16         # multiple slashes
17         clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
18         # curdir
19         clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
20         # leading curdir
21         clean=${clean#./}
22         # trailing curdir
23         clean=${clean%/.}
24         # trailing slash
25         clean=${clean%/}
26         printf "%s" "$clean"
27 }
28
29 tags=
30 create=0
31 # option for "multipackage" just to let the system know that's what you meant
32 # option to take filenames from stdin
33 # parse package, version, release from file if not given
34 while getopts :f:v:r:l:P:S:Cc opt; do
35         case $opt in
36                 f) pkgfile="$OPTARG" ;;
37                 v) pkgver="$OPTARG" ;;
38                 r) pkgrel="$OPTARG" ;;
39                 l) licenses="$OPTARG" ;;
40                 P) prefix="$OPTARG" ;;
41                 S) strip=$(cleanpath "$OPTARG"); ;;
42                 t) tags="$tags $OPTARG" ;;
43                 c) tags="$tags configuration" ;;
44                 C) create=1 ;;
45                 *) echo 'unknown option'; exit 1 ;;
46         esac
47 done
48 shift $((OPTIND - 1))
49
50 package="$1"
51 shift
52 if [ -z "$package" ]; then
53         die "must specify package"
54 fi
55
56 set -e
57 if [ -z "$pkgfile" ]; then
58         pkgfile="$package-$pkgver-$pkgrel.zpm"
59 fi
60
61 # check for package file
62 if [ ! -f "$pkgfile" ]; then
63         if [ $create -eq 1 ]; then
64                 echo creating $pkgfile
65                 zpm newpackage -I -f $pkgfile -v $pkgver -r $pkgrel $package || exit 1
66         else
67                 echo $pkgfile does not exist
68                 exit 1
69         fi
70 fi
71
72 zpm test -v $pkgfile
73
74 #strip=$(cleanpath "$strip")
75 for path in $*; do
76         echo adding $path
77         mtime=$(zpm stat -f '%y' $path)
78         uid=$(zpm stat -f '%u' $path)
79         gid=$(zpm stat -f '%g' $path)
80         username=$(zpm stat -f '%U' $path)
81         groupname=$(zpm stat -f '%G' $path)
82         mode=$(zpm stat -f '%a' $path)
83         rpath="$path"
84
85         rpath=$(cleanpath "$path")
86
87         # strip off leading slash
88         rpath=${rpath#/}
89
90         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
91                 continue
92         fi
93
94         if [ ! -z "$strip" ]; then
95                 echo "stripping $strip"
96                 rpath=${rpath#$strip}
97                 rpath=${rpath#/}
98         fi
99
100         if [ -z "$rpath" ]; then
101                 die "$path resolves to nothing"
102         fi
103
104         prefix=$(cleanpath "$prefix")
105         if [ ! -z "$prefix" ]; then
106                 rpath="$prefix/$rpath"
107         fi
108
109         filetype=$(zpm stat -f '%t' "$path")
110         hash='NULL'
111         target='NULL'
112         case "$filetype" in
113                 regular)
114                         filetype=r
115                         hash=$(zpm addfile $pkgfile "$path")
116                         if [ $? -ne 0 ]; then
117                                 die "zpm addfile failed ($?): $pkgfile $path" 
118                         fi
119                         hash="'$hash'"
120                         ;;
121                 directory)
122                         filetype=d
123                         ;;
124                 symlink)
125                         filetype=l
126                         target=$(readlink $path)
127                         target="'$target'"
128                         ;;
129         esac
130
131         zpm shell $pkgfile <<EOS
132 PRAGMA foreign_keys = ON;
133 begin;
134 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
135 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
136 commit;
137 EOS
138
139 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
140 printf "%s\n" $path
141 done