Files
scratchpkg/functions/functions
2018-05-25 19:07:44 +08:00

255 lines
5.2 KiB
Bash

#!/bin/bash
FUNCTIONS=${FUNCTIONS:-'/usr/share/scratchpkg'}
source "$FUNCTIONS/options"
source "$FUNCTIONS/color"
source "$FUNCTIONS/message"
# defaults value
CONF_FILE="/etc/scratchpkg.conf"
BUILD_SCRIPT="spkgbuild"
ROOT_DIR="/"
INDEX_DIR="/var/lib/scratchpkg/index"
PACKAGE_DIR="/var/cache/scratchpkg/packages"
SOURCE_DIR="/var/cache/scratchpkg/sources"
WORK_DIR="/tmp"
LOG_DIR="/var/cache/scratchpkg/log"
HOOK_DIR="/etc/hooks"
PORT_DIR="/etc/ports"
OPTIONS=(!libtool emptydirs strip docs purge zipman buildflags makeflags)
PURGE_FILES=(usr/{,share/}info/dir)
DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})
MAN_DIRS=({usr{,/local}{,/share},opt/*}/man)
if [ -f $CONF_FILE ]; then
. $CONF_FILE
else
msgerr "Configuration file not found."
exit 1
fi
# check for extract tool
if [ "$(type -p bsdtar)" ]; then
EXTPROG=$(type -p bsdtar)
elif [ "$(type -p tar)" ]; then
EXTPROG=$(type -p tar)
else
msgerr "libarchive or tar is needed. Aborted"
exit 1
fi
# functions load by scratchpkg
pushd() {
command pushd "$@" >/dev/null
}
popd() {
command popd >/dev/null
}
rmdir_silent() {
command rmdir "$@" 2> /dev/null
}
rm_silent() {
command rm "$@" 2> /dev/null
}
testinput() {
if [ ! "$1" ]; then
msg "This option require an argument."
return 1
else
return 0
fi
}
needroot() {
if [ $UID != 0 ]; then
if [ "$#" -eq 0 ]; then
needroot "This operation"
else
msgerr "$@ need root access!"
fi
exit 1
fi
}
checktool() {
if ! type -p $1 &>/dev/null; then
msgerr "$1 not exist in your system!"
exit 1
fi
}
# function to print all installed packages
allinstalled() {
ls ${INDEX_DIR}/*/.pkginfo | rev | cut -d '/' -f2 | rev 2>/dev/null
}
getpkginfo() {
description=$(grep "^# description[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# description[[:blank:]]*:[[:blank:]]*//')
backup=$(grep "^# backup[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# backup[[:blank:]]*:[[:blank:]]*//')
conflict=$(grep "^# conflict[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# conflict[[:blank:]]*:[[:blank:]]*//')
depends=$(grep "^# depends[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//')
makedepends=$(grep "^# makedepends[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# makedepends[[:blank:]]*:[[:blank:]]*//')
noextract=$(grep "^# noextract[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# noextract[[:blank:]]*:[[:blank:]]*//')
options=$(grep "^# options[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# options[[:blank:]]*:[[:blank:]]*//')
. $BUILD_SCRIPT
}
getpkginfofrompkg() {
name=$(tar xf $1 .pkginfo -O | grep ^name | cut -d " " -f3)
version=$(tar xf $1 .pkginfo -O | grep ^version | cut -d " " -f3)
release=$(tar xf $1 .pkginfo -O | grep ^release | cut -d " " -f3)
description=$(tar xf $1 .pkginfo -O | grep ^description | cut -d " " -f3-)
backup=$(tar xf $1 .pkginfo -O | grep ^backup | cut -d " " -f3-)
conflict=$(tar xf $1 .pkginfo -O | grep ^conflict | cut -d " " -f3-)
depends=$(tar xf $1 .pkginfo -O | grep ^depends | cut -d " " -f3-)
makedepends=$(tar xf $1 .pkginfo -O | grep ^makedepends | cut -d " " -f3-)
}
getinstalledname() {
iname=$(cat $INDEX_DIR/$1/.pkginfo | grep ^name | cut -d " " -f3)
iversion=$(cat $INDEX_DIR/$1/.pkginfo | grep ^version | cut -d " " -f3)
irelease=$(cat $INDEX_DIR/$1/.pkginfo | grep ^release | cut -d " " -f3)
echo "$iname-$iversion-$irelease"
}
################################################################
# function to get information from installed package database
#
# Usage: installed_pkg_info <arg> <pkg>
#
# Note: 'arg' could be name, version, release, depends,
# makedepends and descriptions.
################################################################
installed_pkg_info() {
if [ -f $INDEX_DIR/$2/.pkginfo ]; then
echo $(cat $INDEX_DIR/$2/.pkginfo | grep ^$1 | cut -d " " -f3-)
fi
}
getinfopkg() {
cat /tmp/spkg.$2.pkginfo | grep ^$1 | cut -d " " -f3
}
vercomp() {
if [ "$1" = "$2" ]; then
return 0 # same version
elif [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]; then
return 1 # $1 lower than $2
else
return 2 # $1 higher than $2
fi
}
createtemplate() {
if [ -d $1 ]; then
msg "Error directory ${RED}$1${CRESET} already exist"
exit 1
else
mkdir $1
echo "# description :
# backup :
# conflict :
# depends :
# makedepends :
# noextract :
name=$1
version=
release=1
options=()
source=()
md5sum=()
build() {
cd \$name-\$version
./configure --prefix=/usr
make
make DESTDIR=\$PKG install
}" > $1/$BUILD_SCRIPT
msg "Template port have created for ${GREEN}$1${CRESET}."
fi
}
checkdirexist() {
for dir in "$@"; do
if [ ! -d $dir ]; then
msgwarn "Directory ${YELLOW}$dir${CRESET} not exist."
DIR_ERROR=yes
fi
done
[ "$DIR_ERROR" ] && exit 1
}
checkdirwrite() {
for dir in "$@"; do
if [ ! -w $dir ]; then
msgwarn "Directory ${YELLOW}$dir${CRESET} not writable."
DIR_ERROR=yes
fi
done
[ "$DIR_ERROR" ] && exit 1
}
checkdirread() {
for dir in "$@"; do
if [ ! -x $dir ] || [ ! -r $1 ]; then
msgwarn "Directory ${YELLOW}$dir${CRESET} not readable."
DIR_ERROR=yes
fi
done
[ "$DIR_ERROR" ] && exit 1
}
msginst() {
echo -e "[${GREEN}*${CRESET}] $@"
}
msgnoinst() {
echo -e "[ ] $@"
}
msgmiss() {
echo -e "[${RED}m${CRESET}] $@"
}