]> pd.if.org Git - zpackage/blob - zpm-addtopackage
integrate previous work
[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 # option for "multipackage" just to let the system know that's what you meant
30 # option to take filenames from stdin
31 # parse package, version, release from file if not given
32 while getopts :f:v:r:d:a:u:l:p:b:P:S: opt; do
33         case $opt in
34                 f) pkgfile="$OPTARG" ;;
35                 v) pkgver="$OPTARG" ;;
36                 r) pkgrel="$OPTARG" ;;
37                 d) description="$OPTARG" ;;
38                 a) arch="$OPTARG" ;;
39                 u) url="$OPTARG" ;;
40                 l) licenses="$OPTARG" ;;
41                 p) packager="$OPTARG" ;;
42                 b) builddate="$OPTARG" ;;
43                 P) prefix="$OPTARG" ;;
44                 S) strip=$(cleanpath "$OPTARG"); ;;
45                 t) tags="$OPTARG" ;;
46                 c) tags="$tags +configuration" ;;
47         esac
48 done
49 shift $((OPTIND - 1))
50
51 package="$1"
52 shift
53 if [ -z "$package" ]; then
54         die "must specify package"
55 fi
56
57 set -e
58 if [ -z "$pkgfile" ]; then
59         pkgfile="$package-$pkgver-$pkgrel.zpm"
60 fi
61
62 # make sure package exists
63 zpm newpackage -I -f $pkgfile -v $pkgver -r $pkgrel $package || exit 1
64
65 zpm test -v $pkgfile
66
67 #strip=$(cleanpath "$strip")
68 for path in $*; do
69         mtime=$(zpm stat -f '%y' $path)
70         uid=$(zpm stat -f '%u' $path)
71         gid=$(zpm stat -f '%g' $path)
72         username=$(zpm stat -f '%U' $path)
73         groupname=$(zpm stat -f '%G' $path)
74         mode=$(zpm stat -f '%a' $path)
75         rpath="$path"
76
77         rpath=$(cleanpath "$path")
78
79         # strip off leading slash
80         rpath=${rpath#/}
81
82         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
83                 continue
84         fi
85
86         if [ ! -z "$strip" ]; then
87                 echo "stripping $strip"
88                 rpath=${rpath#$strip}
89                 rpath=${rpath#/}
90         fi
91
92         if [ -z "$rpath" ]; then
93                 die "$path resolves to nothing"
94         fi
95
96         prefix=$(cleanpath "$prefix")
97         if [ ! -z "$prefix" ]; then
98                 rpath="$prefix/$rpath"
99         fi
100
101         filetype=$(zpm stat -f '%t' "$path")
102         hash='NULL'
103         target='NULL'
104         case "$filetype" in
105                 regular)
106                         filetype=r
107                         hash=$(zpm addfile $pkgfile "$path")
108                         if [ $? -ne 0 ]; then
109                                 die "zpm addfile failed ($?): $pkgfile $path" 
110                         fi
111                         hash="'$hash'"
112                         ;;
113                 directory)
114                         filetype=d
115                         ;;
116                 symlink)
117                         filetype=l
118                         target=$(readlink $path)
119                         target="'$target'"
120                         ;;
121         esac
122
123         sqlite3 $pkgfile <<EOS
124 PRAGMA foreign_keys = ON;
125 begin;
126 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
127 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
128 commit;
129 EOS
130
131 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
132 printf "%s\n" $path
133 done