Files
scratchpkg/scratch
2018-03-05 07:03:02 +08:00

734 lines
21 KiB
Bash
Executable File

#!/bin/bash
export LC_ALL=C
. /usr/share/scratchpkg/functions || exit 1
showinfo() {
echo
echo " SCRATCHPKG CONFIG"
echo " -------------------------"
echo
echo "CFLAGS=$CFLAGS"
echo "CXXFLAGS=$CXXFLAGS"
echo "MAKEFLAGS=$MAKEFLAGS"
echo
echo "CONF_FILE=$CONF_FILE"
echo "BUILD_SCRIPT=$BUILD_SCRIPT"
echo
echo "ROOT_DIR=$ROOT_DIR"
echo "INDEX_DIR=$INDEX_DIR"
echo
echo "PACKAGE_DIR=$PACKAGE_DIR"
echo "SOURCE_DIR=$SOURCE_DIR"
echo "WORK_DIR=$WORK_DIR"
echo "LOG_DIR=$LOG_DIR"
echo "HOOK_DIR=$HOOK_DIR"
echo
echo "NO_EXTRACT=$NO_EXTRACT"
echo
echo "OPTIONS=${OPTIONS[@]}"
echo "PURGE_FILES=${PURGE_FILES[@]}"
echo "DOC_DIRS=${DOC_DIRS[@]}"
echo "MAN_DIRS=${MAN_DIRS[@]}"
}
listinstalled() {
for installed in $(ls $INDEX_DIR); do
getinstalledname $installed
installedpkg+=($installedname)
done
echo ${installedpkg[@]} | tr ' ' '\n'
msg "Total installed package(s): ${#installedpkg[@]}"
}
listorphan() {
for all_installed in $(ls ${INDEX_DIR}); do
dep=$(cat $INDEX_DIR/$all_installed/.pkginfo | grep ^depends | cut -d " " -f3-)
for deps in ${dep[@]}; do
ALL_DEP+=($deps)
done
done
for all in $(ls $INDEX_DIR); do
ORPHAN=yes
getinstalledname $all
for depended in ${ALL_DEP[@]}; do
if [ $depended = $all ]; then
ORPHAN=no
break
fi
done
if [ "$ORPHAN" = yes ]; then
ORPHAN_PKG+=($installedname)
fi
done
echo ${ORPHAN_PKG[@]} | tr ' ' '\n'
msg "Total orphan package(s): ${#ORPHAN_PKG[@]}"
}
searchpkg() {
for port in ${PORT_REPO[@]}; do
if [ -d $port ]; then
OUTPUT=()
pushd $port
OUTPUT=$(grep -R "# description" | grep -i "$1" | grep "$BUILD_SCRIPT" | cut -d '/' -f1)
popd
if [ "$OUTPUT" ]; then
found=yes
for out in ${OUTPUT[@]}; do
if [ -f $port/$out/$BUILD_SCRIPT ]; then
pushd $port/$out
getpkginfo
popd
if [ ! -z "$name" ] && [ ! -z "$version" ] && [ ! -z "$release" ]; then
portname=$(basename $port)
search_result="${color_blue}($portname)${color_reset} ${color_purple}$name${color_reset} ${color_cyan}$version-$release${color_reset}"
if [ -d $INDEX_DIR/$name ]; then
getinstalledname $name
[ -f $INDEX_DIR/$name/.lock ] && ITSLOCK="[locked]"
if [ "$name-$version-$release" = "$installedname" ]; then
msg3 "$search_result ${color_green}[installed]${color_reset} ${color_blue}$ITSLOCK${color_reset}"
msg4 "$description"
else
msg3 "$search_result ${color_yellow}[installed $iversion-$irelease]${color_reset} ${color_blue}$ITSLOCK${color_reset}"
msg4 "$description"
fi
else
msg3 "$search_result"
msg4 "$description"
fi
unset description
fi
fi
done
fi
fi
done
if [ "$found" != "yes" ]; then
msg "No matching package found."
fi
}
checkowner() {
for installed in $(ls $INDEX_DIR); do
for output in $(cat $INDEX_DIR/$installed/.files | grep $1); do
echo -e "${color_cyan}$installed${color_reset} => ${color_purple}$output${color_reset}"
done
done
}
showtree() {
if [ ! -d $INDEX_DIR/$1 ]; then
msg "Package ${color_red}$1${color_reset} not installed."
else
while IFS=' ' read -r line; do
echo "$line"
done < <(cat $INDEX_DIR/$1/.files)
fi
}
checkintegrity() {
pushd $ROOT_DIR
for installed in $(ls $INDEX_DIR); do
while IFS=' ' read -r line; do
if [ ! -e "$line" ]; then
msg "${color_yellow}$installed${color_reset} is missing: ${color_purple}/$line${color_reset}"
MISSING_FILE=yes
fi
done < <(cat $INDEX_DIR/$installed/.files)
done
popd
[ "$UID" != "0" ] && msg "${color_yellow}(check integrity is recommended run as root user or using sudo)${color_reset}"
[ ! "$MISSING_FILE" ] && msg "Your system file is consistent with package tree."
}
showpackageinfo() {
if [ ! -f $1 ]; then
msg "${color_red}$1${color_reset} not exist."
exit 1
else
if [ $(tar -tf "$1" | grep -x ".pkginfo") ]; then
getpkginfofrompkg $1
echo -e "${color_green}info:${color_reset}"
[ "$name" ] && echo "name = $name"
[ "$version" ] && echo "version = $version"
[ "$release" ] && echo "release = $release"
[ "$description" ] && echo "description = $description"
[ "$backup" ] && for b in ${backup[@]}; do echo "backup = $b"; done
[ "$conflict" ] && for c in ${conflict[@]}; do echo "conflict = $c"; done
[ "$depends" ] && for d in ${depends[@]}; do echo "depends = $d"; done
[ "$makedepends" ] && for md in ${makedepends[@]}; do echo "makedepends = $md"; done
echo ""
if [ $(tar -tf "$1" | grep -x ".pkgreadme") ]; then
echo -e "${color_green}readme:${color_reset}"
tar -xf $1 .pkgreadme -O
echo ""
echo ""
fi
if [ $(tar -tf "$1" | grep -x ".pkginstall") ]; then
echo -e "${color_green}pkginstall files:${color_reset}"
tar -xf $1 .pkginstall -O
echo ""
fi
else
msg "${color_yellow}$1${color_reset} is not package created by scratchpkg."
exit 1
fi
fi
}
showdepends() {
for portdepends in ${PORT_REPO[@]}; do
if [ -f $portdepends/$1/$BUILD_SCRIPT ]; then
pushd $portdepends/$1
getpkginfo
popd
break
fi
done
if [ -z $name ]; then
msgerr "Port ${color_red}$1${color_reset} not exist."
exit 1
fi
for dep in ${depends[@]}; do
if [ -d $INDEX_DIR/$dep ]; then
msg2 "$dep ${color_green}[installed]${color_reset}"
else
PORT_EXIST=no
for port in ${PORT_REPO[@]}; do
if [ -f $port/$dep/$BUILD_SCRIPT ]; then
PORT_EXIST=yes
break
fi
done
case $PORT_EXIST in
yes) msg2 "$dep" ;;
no) msg2 "$dep ${color_red}[port not exist]${color_reset}" ;;
esac
fi
done
for mdep in ${makedepends[@]}; do
if [ -d $INDEX_DIR/$mdep ]; then
msg2 "$mdep ${color_cyan}(make)${color_reset} ${color_green}[installed]${color_reset}"
else
PORT_EXIST=no
for port in ${PORT_REPO[@]}; do
if [ -f $port/$mdep/$BUILD_SCRIPT ]; then
PORT_EXIST=yes
break
fi
done
case $PORT_EXIST in
yes) msg2 "$mdep ${color_cyan}(make)${color_reset}" ;;
no) msg2 "$mdep ${color_cyan}(make)${color_reset} ${color_red}[port not exist]${color_reset}" ;;
esac
fi
done
}
showupdate() {
for package_name in $(ls $INDEX_DIR); do
for port in ${PORT_REPO[@]}; do
if [ -f $port/$package_name/$BUILD_SCRIPT ]; then
. $port/$package_name/$BUILD_SCRIPT
getinstalledname $name
if [ "$name-$version-$release" != "$installedname" ]; then
if [ -f "$INDEX_DIR/$package_name/.lock" ]; then
ITSLOCK="[locked]"
fi
msg "$name ${color_red}$iversion-$irelease${color_reset} => ${color_green}$version-$release${color_reset} ${color_blue}$ITSLOCK${color_reset}"
OUTDATE=yes
unset ITSLOCK
fi
break
fi
done
done
[ ! "$OUTDATE" ] && msg "All package is up to date."
}
showdependent() {
if [ ! -d $INDEX_DIR/$1 ]; then
msg "Package ${color_red}$1${color_reset} not installed."
exit 1
else
for all_installed in $(ls $INDEX_DIR); do
depend=$(cat $INDEX_DIR/$all_installed/.pkginfo | grep ^depends | cut -d " " -f3-)
for dep in ${depend[@]}; do
if [ $dep = $1 ]; then
msg2 "$all_installed"
fi
done
makedepend=$(cat $INDEX_DIR/$all_installed/.pkginfo | grep ^makedepends | cut -d " " -f3-)
for mdep in ${makedepend[@]}; do
if [ $mdep = $1 ]; then
msg2 "$all_installed ${color_cyan}(make)${color_reset}"
fi
done
done
fi
}
showglobaldependent() {
for port in ${PORT_REPO[@]}; do
if [ -d $port ]; then
for all in $(ls $port); do
if [ -f $port/$all/$BUILD_SCRIPT ]; then
depend=$(cat $port/$all/$BUILD_SCRIPT | grep ^'# depends' | tr -d ':' | cut -d " " -f3-)
for dep in ${depend[@]}; do
if [ $dep = $1 ]; then
if [ -d $INDEX_DIR/$all ]; then
msg2 "$all ${color_green}[installed]${color_reset}"
else
msg2 "$all"
fi
fi
done
makedepend=$(cat $port/$all/$BUILD_SCRIPT | grep ^'# makedepends' | tr -d ':' | cut -d " " -f3-)
for mdep in ${makedepend[@]}; do
if [ $mdep = $1 ]; then
if [ -d $INDEX_DIR/$all ]; then
msg2 "$all ${color_cyan}(make)${color_reset} ${color_green}[installed]${color_reset}"
else
msg2 "$all ${color_cyan}(make)${color_reset}"
fi
fi
done
fi
done
fi
done
}
getportpath() {
for repo in ${PORT_REPO[@]}; do
if [[ -f $repo/$1/$BUILD_SCRIPT ]]; then
PPATH=$(dirname "$repo/$1/$BUILD_SCRIPT")
return 0
fi
done
return 1
}
catport() {
if getportpath "$1"; then
cat "$PPATH/$BUILD_SCRIPT"
exit 0
else
msgerr "Port '$1' not exist"
exit 1
fi
}
showportpath() {
if getportpath "$1"; then
echo "$PPATH"
exit 0
else
msgerr "Port '$1' not exist"
exit 1
fi
}
getpkgcache() {
for list in $(ls "$PACKAGE_DIR"); do
[ -f "$PACKAGE_DIR"/$list ] && ALL_PACKAGES+=($list)
done
for list in $(ls "$SOURCE_DIR"); do
[ -f "$SOURCE_DIR"/$list ] && ALL_SOURCES+=($list)
done
for repo in ${PORT_REPO[@]}; do
for port in $(ls $repo); do
if [ -f $repo/$port/$BUILD_SCRIPT ]; then
. $repo/$port/$BUILD_SCRIPT
PORT_PACKAGES+=($name-$version-$release.spkg.txz)
if [ ! -z $source ]; then
for src in ${source[@]}; do
if [ $(echo $src | grep -E "(ftp|http|https)://") ]; then
if [ $(echo $src | grep -E "::(ftp|http|https)://") ]; then
sourcename="$(echo $src | awk -F '::' '{print $1}')"
else
sourcename="$(echo $src | rev | cut -d / -f 1 | rev)"
fi
SOURCE_NAMES+=($sourcename)
fi
done
fi
fi
done
done
for i in ${PORT_PACKAGES[@]}; do
for pkg in ${!ALL_PACKAGES[@]}; do
if [ "${ALL_PACKAGES[pkg]}" = "$i" ]; then
unset 'ALL_PACKAGES[pkg]'
break
fi
done
done
for a in ${SOURCE_NAMES[@]}; do
for src in ${!ALL_SOURCES[@]}; do
if [ "${ALL_SOURCES[src]}" = "$a" ]; then
unset 'ALL_SOURCES[src]'
break
fi
done
done
}
pkgcache() {
getpkgcache
if [ ${#ALL_PACKAGES[@]} -gt 0 ]; then
ALL_PACKAGES_SIZE=$(pushd "$PACKAGE_DIR" && du -ch ${ALL_PACKAGES[@]} | grep total | awk '{print $1}' && popd)
else
ALL_PACKAGES_SIZE=0M
fi
if [ ${#ALL_SOURCES[@]} -gt 0 ]; then
ALL_SOURCES_SIZE=$(pushd "$SOURCE_DIR" && du -ch ${ALL_SOURCES[@]} | grep total | awk '{print $1}' && popd)
else
ALL_SOURCES_SIZE=0M
fi
msg "${color_cyan}Package cache ($ALL_PACKAGES_SIZE):${color_reset}"
[ ${#ALL_PACKAGES[@]} -gt 0 ] && (echo ${ALL_PACKAGES[@]} | tr ' ' '\n') || echo "(none)"
echo ""
msg "${color_cyan}Source cache ($ALL_SOURCES_SIZE):${color_reset}"
[ ${#ALL_SOURCES[@]} -gt 0 ] && (echo ${ALL_SOURCES[@]} | tr ' ' '\n') || echo "(none)"
}
clearpkgcache() {
needroot "Removing package & source cache"
getpkgcache
if [ ${#ALL_PACKAGES[@]} -gt 0 ]; then
for pkg in ${ALL_PACKAGES[@]}; do
rm -v $PACKAGE_DIR/$pkg
done
fi
if [ ${#ALL_SOURCES[@]} -gt 0 ]; then
for src in ${ALL_SOURCES[@]}; do
rm -v $SOURCE_DIR/$src
done
fi
}
updports() {
checktool httpup
needroot "Updating ports"
for repo in ${PORT_REPO[@]}; do
portname=$(basename $(echo $repo))
if [ -f /etc/ports/$portname.httpup ]; then
. /etc/ports/$portname.httpup
httpup sync $URL $repo
fi
done
}
duplicateports() {
dup=$(find ${PORT_REPO[@]} -type d -print | egrep -xv "($(echo ${PORT_REPO[@]} | tr ' ' '|'))" | \
rev | cut -d '/' -f1 | rev | sort | uniq -d)
if [ "$dup" ]; then
for dp in $dup; do
for repo in ${PORT_REPO[@]}; do
reponame=$(basename $repo)
[ -d $repo/$dp ] && echo "$repo/$dp"
done
done
else
msg "No duplicate ports found."
fi
}
listports() {
for repo in ${PORT_REPO[@]}; do
if [ "$1" = "$(basename $repo)" ]; then
REPO_EXIST=yes
for ports in $(ls $repo); do
if [ -f "$repo/$ports/$BUILD_SCRIPT" ]; then
. "$repo/$ports/$BUILD_SCRIPT"
if [ -f "$PACKAGE_DIR/$name-$version-$release.spkg.txz" ]; then
echo -e "$name ${color_green}$version${color_reset}-${color_blue}$release${color_reset} ${color_purple}[*]${color_reset}"
else
echo -e "$name ${color_green}$version${color_reset}-${color_blue}$release${color_reset}"
fi
fi
done
fi
done
[ "$REPO_EXIST" ] && exit 0 || msgerr "Repository ${color_red}$1${color_reset} not exist."
}
interrupted() {
exit 1
}
portinstall() {
for pkg in "${INSTALLPKG[@]}"; do
if getportpath $pkg; then
pushd "$PPATH"
buildpkg ${OPTS[@]} || exit 1
popd
else
msgerr "Port ${color_red}$pkg${color_reset} not found."
fi
done
}
listlocked() {
for pkg in "$INDEX_DIR"/*/.pkginfo; do
pkgpath=$(dirname $pkg)
pkgname=$(basename $pkgpath)
if [ -f "$pkgpath"/.lock ]; then
msg "$pkgname"
fi
done
}
lockpkg() {
needroot "Locking package"
for pkg in "${LOCKPKG[@]}"; do
if [ ! -d $INDEX_DIR/$pkg ]; then
msgerr "Package '$pkg' is not installed."
else
touch $INDEX_DIR/$pkg/.lock && msg "Successfully locked package '$pkg'."
fi
done
}
unlockpkg() {
needroot "Unlocking package"
for pkg in "${UNLOCKPKG[@]}"; do
if [ ! -d $INDEX_DIR/$pkg ]; then
msgerr "Package '$pkg' is not installed."
elif [ ! -f $INDEX_DIR/$pkg/.lock ]; then
msgerr "Package '$pkg' is not locked."
else
rm $INDEX_DIR/$pkg/.lock && msg "Successfully unlocked package '$pkg'."
fi
done
}
help() {
cat << EOF
Usage:
scratch [ <options> <arguments> ]
Options:
-i, --install install package
-u, --upgrade upgrade package
-r, --reinstall reinstall package
-id, --ignore-dependency skip dependency check
-ic, --ignore-conflict skip file/package conflict check
-fr, --force-rebuild rebuild package
-sd, --source-dir <path> set directory path for sources
-pd, --package-dir <path> set directory path for compiled package
-v, --verbose verbose process
-im, --ignore-mdsum skip md5sum check for sources
-um, --update-mdsum update md5sum file for port
-do, --download-only download sources only
-eo, --extract-only extract sources only
-kw, --keep-work keep working directory
-rd, --redownload re-download sources
--no-preinstall don't run pre-install script
--no-postinstall don't run post-install script
--no-backup skip backup when upgrading package
-dup,--duplicate-ports list duplicate ports
-l, --list-installed show list installed packages
-lp, --list-ports show list ports for repository
-lo, --list-orphan show list orphaned packages installed
-ci, --check-integrity check integrity between package's index and files in system
-cu, --check-update check for package update
--cache show old package and source caches
--clear-cache remove all old package and source caches
-up, --update-ports update port's repository
--info show scratchpkg info (setting)
-c, --cat-port cat port's buildscript (spkgbuild)
-dp, --dependent show package's dependent (check through package index)
-gdp,--global-dependent show package's dependent (check through port's repository)
-d, --depends show package's depends
-pi, --package-info <pkg> show package's info
-co, --check-owner show file's owner
-p, --package <pkgnames> set package name to build/install
-st, --show-tree show list files of installed package
-s, --search search for packages in port's repository
-t, --create-template create port's template for package
--listlocked list locked package
-pp, --port-path show ports directory path
--no-color disable colour for output
-h, --help show this help message
Example:
scratch -p firefox sudo -id -kw -i build, keep working dir, ignore missing dependency
and then install firefox and sudo
scratch -r -fr -im -p firefox sudo rebuild, skip md5sum check for sources and then
reinstall firefox and sudo
EOF
}
parse_options() {
if [ -z "$1" ]; then
help
else
while [ "$1" ]; do
case $1 in
-i | --install) OPTS+=($1); shift ;;
-u | --upgrade) OPTS+=($1); shift ;;
-r | --reinstall) OPTS+=($1); shift ;;
-id | --ignore-dependency) OPTS+=($1); shift ;;
-ic | --ignore-conflict) OPTS+=($1); shift ;;
--no-preinstall) OPTS+=($1); shift ;;
--no-postinstall) OPTS+=($1); shift ;;
-fr | --force-rebuild) OPTS+=($1); shift ;;
-v | --verbose) OPTS+=($1); shift ;;
-im | --ignore-mdsum) OPTS+=($1); shift ;;
-um | --update-mdsum) OPTS+=($1); shift ;;
-do | --download-only) OPTS+=($1); shift ;;
-eo | --extract-only) OPTS+=($1); shift ;;
-kw | --keep-work) OPTS+=($1); shift ;;
-rd | --redownload) OPTS+=($1); shift ;;
--no-backup) OPTS+=($1); shift ;;
--no-color) OPTS+=($1); nocolor; shift ;;
-dup| --duplicate-ports) duplicateports; exit 0; shift ;;
-l | --list-installed) listinstalled; exit 0; shift ;;
-lo | --list-orphan) listorphan; exit 0; shift ;;
-ci | --check-integrity) checkintegrity; exit 0; shift ;;
-cu | --check-update) showupdate; exit 0; shift ;;
--cache) pkgcache; exit 0; shift ;;
--clear-cache) clearpkgcache; exit 0; shift ;;
-up | --update-ports) updports; exit 0; shift ;;
--info) showinfo; exit 0; shift ;;
--listlocked) listlocked; exit 0; shift ;;
-h | --help) help; exit 0; shift ;;
-lp | --list-port) testinput "$2" && listports "$2" || exit 1; shift 2 ;;
-c | --cat-port) testinput "$2" && catport "$2" || exit 1; shift 2 ;;
-dp | --dependent) testinput "$2" && showdependent "$2" || exit 1; shift 2 ;;
-gdp| --global-dependent) testinput "$2" && showglobaldependent "$2" || exit 1; shift 2 ;;
-d | --depends) testinput "$2" && showdepends "$2" || exit 1; shift 2 ;;
-pi | --package-info) testinput "$2" && showpackageinfo "$2" || exit 1; shift 2 ;;
-co | --check-owner) testinput "$2" && checkowner "$2" || exit 1; shift 2 ;;
-st | --show-tree) testinput "$2" && showtree "$2" || exit 1; shift 2 ;;
-s | --search) testinput "$2" && searchpkg "$2" || exit 1; shift 2 ;;
-t | --create-template) testinput "$2" && createtemplate "$2" || exit 1; shift 2 ;;
-pp | --port-path) testinput "$2" && showportpath "$2" || exit 1; shift 2 ;;
-sd | --source-dir) testinput "$2" && OPTS+=($1 $2) || exit 1; shift 2 ;;
-pd | --package-dir) testinput "$2" && OPTS+=($1 $2) || exit 1; shift 2 ;;
-p | --package) testinput "$2" && while [ "$2" ]; do
if echo "" $2 | grep -vq "^ -"; then
INSTALLPKG+=($2); shift
else
break
fi
done; shift;;
--lock) testinput "$2" && while [ "$2" ]; do
if echo "" $2 | grep -vq "^ -"; then
LOCKPKG+=($2); shift
else
break
fi
done; shift;;
--unlock) testinput "$2" && while [ "$2" ]; do
if echo "" $2 | grep -vq "^ -"; then
UNLOCKPKG+=($2); shift
else
break
fi
done; shift;;
*) msgerr "Invalid option!"; exit 1 ;;
esac
done
fi
}
main() {
parse_options "$@"
checkdirexist "$INDEX_DIR"
if [ "${#LOCKPKG[@]}" -gt 0 ]; then
lockpkg
exit 0
fi
if [ "${#UNLOCKPKG[@]}" -gt 0 ]; then
unlockpkg
exit 0
fi
if [ "${#INSTALLPKG[@]}" -gt 0 ]; then
portinstall
exit 0
fi
exit 0
}
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
main "$@"