#!/bin/sh # zpm add -f pkgfile pkgid [ files ] # no package file? take from ZPM_PACKAGE_FILE # no pkgid ? take from ZPM_PACKAGE_ID # no id impossible die() { echo $* 1>&2 exit 1 } # basic cleanup on a path cleanpath() { clean="$1" if [ -z "$clean" ]; then printf ''; fi # multiple slashes clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g') # curdir clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g') # leading curdir clean=${clean#./} # trailing curdir clean=${clean%/.} # trailing slash clean=${clean%/} printf "%s" "$clean" } verbose=0 tags= isconfig=0 addcontent=1 complete=0 # option for "multipackage" just to let the system know that's what you meant # option to take filenames from stdin # parse package, version, release from file if not given # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets # -T set link target # -M set mtime # -m set mode # -H set hash, will be overridden for regular files unless -N is set # -F set filetype, regular, symlink, directory while getopts :f:vr:l:P:S:cu:g:NCm:M:T:H:F: opt; do case $opt in N) addcontent=0 ;; f) pkgfile="$OPTARG" ;; P) prefix="$OPTARG" ;; S) strip=$(cleanpath "$OPTARG"); ;; t) tags="$tags $OPTARG" ;; c) isconfig=1 ;; u) cluser="$OPTARG" ;; g) clgroup="$OPTARG" ;; m) clmode="$OPTARG" ;; M) clmtime="$OPTARG" ;; T) cltarget="$OPTARG" ;; F) cltype="$OPTARG" ;; H) clhash="$OPTARG" ;; v) verbose=$((verbose + 1)) ;; C) complete=1 ;; *) echo 'unknown option' $OPTARG; exit 1 ;; esac done shift $((OPTIND - 1)) if [ $isconfig -eq 1 ]; then tags="$tags configuration" fi if [ $verbose -gt 2 ]; then set -x fi pkgid="$1" shift eval $(zpm parse -E $pkgid) if [ -z "$pkgfile" ]; then pkgfile=$ZPM_PACKAGE_FILE fi if [ -z "$release" ]; then if [ -z "$pkgfile" ]; then die "cannot determine package file" else pkgstr=$(zpm findpkg -f $pkgfile $pkgid) if [ -z "$pkgstr" ]; then die "unable to find package id for $pkgid in $pkgfile" fi pkgid=$pkgstr # need to reparse the new package id eval $(zpm parse -E $pkgid) fi fi # look for a .zpm file here if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then pkgfile="$pkgid.zpm" fi if [ -z "$pkgfile" ]; then die "cannot determine package file" fi # check for package file if [ ! -f "$pkgfile" ]; then echo $pkgfile does not exist exit 1 fi set -e zpm test -v $pkgfile set +e if [ $verbose -gt 0 ]; then echo adding to $pkgfile $pkgid fi package=$(zpm quote "$name") pkgver=$(zpm quote "$version") pkgrel=$(zpm quote "$release") #strip=$(cleanpath "$strip") for path in $*; do #echo adding $path if [ $verbose -gt 1 ]; then printf "adding %s\n" $path fi # only stat the file for items not set on the command line mode=${clmode:-$(zpm stat -l -f '%a' $path)} username=${cluser:-$(zpm stat -l -f '%U' $path)} groupname=${clgroup:-$(zpm stat -l -f '%G' $path)} mtime=${clmtime:-$(zpm stat -l -f '%y' $path)} filetype=${cltype:-$(zpm stat -l -f '%t' "$path")} rpath="$path" rpath=$(cleanpath "$path") # strip off leading slash rpath=${rpath#/} if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then continue fi if [ ! -z "$strip" ]; then rpath=${rpath#$strip} rpath=${rpath#/} fi if [ -z "$rpath" ]; then die "$path resolves to nothing" fi prefix=$(cleanpath "$prefix") if [ ! -z "$prefix" ]; then rpath="$prefix/$rpath" fi # ensure all paths are absolute rpath=/${rpath#/} hash='NULL' target='NULL' case "$filetype" in regular) filetype=r if [ $addcontent -eq 1 ]; then hash=$(zpm addfile $pkgfile "$path") if [ $? -ne 0 ]; then die "zpm addfile failed ($?): $pkgfile $path" fi else if [ -z "$clhash" ]; then hash=$(zpm hash "$path") else hash=$clhash fi fi hash="'$hash'" ;; directory) filetype=d ;; symlink) filetype=l if [ -n "$cltarget" ]; then target=$cltarget else target=$(readlink $path) target=$(zpm quote -q "$target") fi ;; *) die "filetype $filetype not supported" ;; esac # TODO check that we have such a package,version,release #cat < $target"} if [ $verbose -gt 1 ]; then printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname $path elif [ $verbose -gt 0 ]; then printf "%s\n" $path fi done if [ $complete -eq 1 ]; then zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s') zpm packagehash -f $pkgfile -s -q $pkgid else zpm pkg -f $pkgfile $pkgid hash= fi