Files
scratchpkg/scratch
2019-04-25 17:37:52 +08:00

1403 lines
31 KiB
Bash
Executable File

#!/bin/bash
#
# scratchpkg
#
# Copyright (c) 2018 by Emmett1 (emmett1.2miligrams@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
RED='\e[0;31m'
GREEN='\e[0;32m'
YELLOW='\e[0;33m'
CYAN='\e[0;36m'
PURPLE='\e[0;35m'
CRESET='\e[0m'
nocolor() {
RED=
GREEN=
YELLOW=
CYAN=
PURPLE=
CRESET=
}
msg() {
echo -e "${GREEN}==>${CRESET} $1"
}
msgerr() {
echo -e "${RED}==> ERROR:${CRESET} $1"
}
msginst() {
echo -e "[${GREEN}i${CRESET}] $1"
}
msgmiss() {
echo -e "[${YELLOW}m${CRESET}] $1"
}
msgnoinst() {
echo -e "[ ] $1"
}
msgwarn() {
echo -e "${YELLOW}==> WARNING:${CRESET} $1"
}
needroot() {
if [ $UID != 0 ]; then
if [ "$#" -eq 0 ]; then
needroot "This operation"
else
msgerr "$@ need root access!"
fi
exit 1
fi
}
getportpath() {
for repo in ${PORT_REPO[@]}; do
if [[ -f $repo/$1/$BUILD_SCRIPT ]]; then
echo "$(dirname $repo/$1/$BUILD_SCRIPT)"
return 0
fi
done
return 1
}
pushd() {
command pushd $1 &>/dev/null
}
popd() {
command popd &>/dev/null
}
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
}
installed_pkg_info() {
if isinstalled $2; then
echo $(cat $INDEX_DIR/$2/.pkginfo | grep ^$1 | cut -d " " -f3-)
fi
}
allinstalled() {
for i in $INDEX_DIR/*; do
if isinstalled ${i##*/}; then
echo ${i##*/}
fi
done
}
confirm() {
read -r -p "$1 (Y/n) " response
case "$response" in
[Nn][Oo]|[Nn]) echo "$2"; exit 2 ;;
*) : ;;
esac
}
checktool() {
if ! type -p $1 &>/dev/null; then
msgerr "'$1' not exist in your system!"
exit 1
fi
}
needarg() {
if [[ -z "$@" ]]; then
msgerr "This operation required an arguments!"
exit 1
fi
}
catport() {
if PPATH=$(getportpath "$1"); then
cat "$PPATH/$BUILD_SCRIPT"
else
msgerr "Port '$1' not exist."
exit 1
fi
}
isinstalled() {
if [ -s $INDEX_DIR/$1/.pkginfo ] && [[ $(grep $1 $INDEX_DIR/$1/.pkginfo) ]]; then
return 0
else
return 1
fi
}
settermtitle() {
echo -en "\033]0;$@\a"
}
missingdep() {
local pkg d
for pkg in $(allinstalled); do
if [ $(getportpath "$pkg") ]; then
depends=$(grep "^# depends[[:blank:]]*:" $(getportpath "$pkg")/$BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++')
fi
if [ "$depends" ]; then
for d in ${depends[@]}; do
if ! isinstalled $d; then
msd+=($d)
fi
done
fi
if [ ${#msd[@]} -gt 0 ]; then
echo -e "${GREEN}$pkg${CRESET} missing ${RED}${msd[@]}${CRESET}"
fi
unset depends msd
done
}
checkintegrity() {
if [ "$1" ]; then
pushd /
if [ -f $INDEX_DIR/$1/.files ]; then
while IFS=' ' read -r line; do
if [ ! -e "$line" ]; then
MISSING_FILE=yes
if [ -L "$line" ]; then
echo -e "${YELLOW}broken symlink${CRESET} $1: /$line"
else
echo -e "${RED}file missing${CRESET} $1: /$line"
fi
fi
done < <(cat $INDEX_DIR/$1/.files)
else
echo "Package '$1' not installed."
exit 1
fi
popd
else
pushd /
for pkg in $(allinstalled); do
while IFS=' ' read -r line; do
if [ ! -e "$line" ]; then
MISSING_FILE=yes
if [ -L "$line" ]; then
echo -e "${YELLOW}broken symlink${CRESET} $pkg: /$line"
else
echo -e "${RED}file missing${CRESET} $pkg: /$line"
fi
fi
done < <(cat $INDEX_DIR/$pkg/.files)
done
popd
fi
[ "$UID" != "0" ] && msg "${YELLOW}(check integrity is recommended run as root or using sudo)${CRESET}"
if [ "$1" ]; then
p="Package '$1'"
else
p="Your system"
fi
[ ! "$MISSING_FILE" ] && msg "$p files is consistent with package tree."
}
listinstalled() {
local pkg
for pkg in $(allinstalled); do
iname=$(installed_pkg_info name $pkg)
iversion=$(installed_pkg_info version $pkg)
irelease=$(installed_pkg_info release $pkg)
echo -e "$iname ${GREEN}$iversion${CRESET}-${BLUE}$irelease${CRESET}"
unset iname iversion irelease
done
}
listorphan() {
local pkg dep
tmpallpkg=$(mktemp)
tmpalldep=$(mktemp)
for pkg in $INDEX_DIR/*/.pkginfo; do
pkg=$(basename $(dirname $pkg))
echo $pkg >> $tmpallpkg
dep="$dep $(grep "^# depends[[:blank:]]*:" $(getportpath $pkg)/$BUILD_SCRIPT 2>/dev/null | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++')"
done
echo $dep | tr ' ' '\n' | sort | uniq > $tmpalldep
grep -xvF -f $tmpalldep $tmpallpkg
rm $tmpalldep $tmpallpkg
}
lockpkg() {
local pkg
needroot "Locking package"
for pkg in "$@"; do
if ! isinstalled $pkg; then
msgerr "Package '$pkg' is not installed."
elif [ -f $INDEX_DIR/$pkg/.lock ]; then
msgerr "Package '$pkg' already locked."
else
touch $INDEX_DIR/$pkg/.lock && msg "Successfully locked package '$pkg'."
fi
done
}
unlockpkg() {
local pkg
needroot "Unlocking package"
for pkg in "$@"; do
if ! isinstalled $pkg; then
msgerr "Package '$pkg' is not installed."
elif [ ! -f $INDEX_DIR/$pkg/.lock ]; then
msgerr "Package '$pkg' is not locked."
else
rm -f $INDEX_DIR/$pkg/.lock && msg "Successfully unlocked package '$pkg'."
fi
done
}
listlocked() {
local pkg
for pkg in $(allinstalled); do
if [ -f "$INDEX_DIR"/$pkg/.lock ]; then
echo -e "$pkg"
fi
done
}
showdepends() {
local dep
if [ $(getportpath "$1") ]; then
depends=$(grep "^# depends[[:blank:]]*:" $(getportpath "$1")/$BUILD_SCRIPT | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++')
else
msgerr "Port '$1' not exist."
exit 1
fi
#isinstalled ${depends[0]}
for dep in ${depends[@]}; do
if isinstalled $dep; then
msginst "$dep"
elif getportpath $dep >/dev/null; then
msgnoinst "$dep"
else
msgmiss "$dep"
fi
done
}
showdependent() {
local port all dep pname
for port in ${PORT_REPO[@]}; do
if [ -d $port ]; then
for all in $port/*/$BUILD_SCRIPT; do
[ -f $all ] || continue
depend=$(grep "^# depends[[:blank:]]*:" $all | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++')
for dep in ${depend[@]}; do
if [ $dep = $1 ]; then
GDP=yes
pname=$(dirname $all)
pname=$(echo ${pname##*/})
if isinstalled $pname; then
msginst "$pname"
else
msgnoinst "$pname"
fi
fi
done
done
fi
done
[ "$GDP" ] && return 0 || return 1
}
checkowner() {
local arg
arg=$(echo $1 | sed 's:^/::')
grep -R $arg $INDEX_DIR/*/.files | sed "s:$INDEX_DIR/::" | sed "s:/.files::" | tr : " " | column -t
}
showtree() {
if ! isinstalled $1; then
msg "Package'$1' not installed."
else
while IFS=' ' read -r line; do
echo "$line"
done < <(cat $INDEX_DIR/$1/.files)
fi
}
updports() {
checktool httpup
needroot "Updating ports"
if [ ! -e "$REPO_FILE" ]; then
msgerr "Repo file not found! ($REPO_FILE)"
exit 1
fi
while read repodir repourl junk; do
case $repodir in
""|"#"*) continue ;;
esac
if [ -n "$repodir" ] && [ -n "$repourl" ]; then
httpup sync $repourl $repodir
if [ $? != 0 ]; then
msgerr "Failed sync from $repourl"
exit 1
fi
fi
done < "$REPO_FILE"
}
printreadme() {
needarg $@
if PPATH=$(getportpath "$1"); then
if [ -f "$PPATH/readme" ]; then
cat "$PPATH/readme"
else
msgerr "Port '$1' does not have readme."
fi
else
msgerr "Port '$1' not exist."
exit 1
fi
}
searchpkg() {
needarg $@
case $1 in
-*) msgerr "Invalid pattern '$1'"
return 1 ;;
esac
local port found OUTPUT
for port in ${PORT_REPO[@]}; do
if [ -d $port ]; then
pushd $port
OUTPUT=$(grep -R description | grep "$BUILD_SCRIPT:# description[[:blank:]]*:" | sed "s/$BUILD_SCRIPT:# description[[:blank:]]*://" | grep -i "$1" | cut -d '/' -f1)
popd
if [ -n "$OUTPUT" ]; then
found=yes
for out in ${OUTPUT[@]}; do
if [ -f $port/$out/$BUILD_SCRIPT ]; then
pushd $port/$out
description=$(grep "^# description[[:blank:]]*:" $BUILD_SCRIPT | sed 's/^# description[[:blank:]]*:[[:blank:]]*//')
. $BUILD_SCRIPT
popd
if [ ! -z "$name" ] && [ ! -z "$version" ] && [ ! -z "$release" ]; then
portname=$(basename $port)
search_result="${PURPLE}($portname)${CRESET} $name ${CYAN}$version-$release${CRESET} $description"
if isinstalled $name; then
echo -e "[${GREEN}*${CRESET}] $search_result"
else
echo -e "[ ] $search_result"
fi
unset description name version release
fi
fi
done
fi
fi
done
if [ ! "$found" ]; then
msg "No matching package found."
fi
}
foreignpkg() {
for pkg in $(allinstalled); do
if ! getportpath $pkg >/dev/null; then
iname=$(installed_pkg_info name $pkg)
iversion=$(installed_pkg_info version $pkg)
irelease=$(installed_pkg_info release $pkg)
echo -e "$iname ${GREEN}$iversion${CRESET}-${CYAN}$irelease${CRESET}"
fi
unset iname iversion irelease
done
}
srunhooks() {
local hook
if [ "$mode" = "upgrade" ] || [ "$mode" = "sysup" ]; then
opr=upgrade
else
opr=install
fi
for hook in $HOOK_DIR/*.hook; do
[ -f $hook ] || continue
description=$(grep "^# description[[:blank:]]*:" $hook | sed 's/^# description[[:blank:]]*:[[:blank:]]*//')
operation=$(grep "^# operation[[:blank:]]*:" $hook | sed 's/^# operation[[:blank:]]*:[[:blank:]]*//')
target=$(grep "^# target[[:blank:]]*:" $hook | sed 's/^# target[[:blank:]]*:[[:blank:]]*//')
if [ -n "$description" ] && [ -n "$operation" ] && [ -n "$target" ]; then
if [ "$(echo $operation | grep -w "$opr" )" ]; then
for pn in $@; do
if [ -s $INDEX_DIR/$pn/.files ] && [ "$(grep $target $INDEX_DIR/$pn/.files)" ]; then
[ "$ee" != 1 ] && { echo; ee=1; }
echo "$description"
. $hook
if [ "`type -t exechook`" = "function" ]; then
exechook
fi
break
fi
done
fi
fi
unset description operation target
unset -f exechook
done
}
srunremovehooks() {
local hook
if [ "${#runthishook[@]}" -gt 0 ]; then
echo
for hook in ${runthishook[@]}; do
description=$(cat "$hook" | grep ^"# description" | sed 's/\://' | cut -d ' ' -f 3-)
operation=$(cat "$hook" | grep ^"# operation" | sed 's/\://' | cut -d ' ' -f 3-)
target=$(cat "$hook" | grep ^"# target" | sed 's/\://' | cut -d ' ' -f 3-)
if [ -n "$description" ] && [ -n "$operation" ] && [ -n "$target" ]; then
echo "$description"
. $hook
if [ "`type -t exechook`" = "function" ]; then
exechook
fi
fi
unset description operation target
unset -f exechook
done
fi
}
srunpreremovehooks() {
local hook
for hook in $HOOK_DIR/*.hook; do
[ -f $hook ] || continue
operation=$(cat "$hook" | grep ^"# operation" | sed 's/\://' | cut -d ' ' -f 3-)
target=$(cat "$hook" | grep ^"# target" | sed 's/\://' | cut -d ' ' -f 3-)
if [ "$(echo $operation | grep -w "remove" )" ]; then
if [ "$(grep $target $INDEX_DIR/$1/.files)" ]; then
if [ ! $(echo ${runthishook[@]} | tr ' ' '\n' | grep -x $hook) ]; then
runthishook+=($hook)
fi
fi
fi
unset operation target
done
}
buildpkg() {
local OPTS
if [ -z "$1" ]; then
echo "Please specify package to build."
return 1
fi
while [ "$1" ]; do
if [[ "$1" =~ ^-(f|v|w|m|o|x|l)$ ]]; then
OPTS+=($1)
elif [[ "$1" =~ ^--(no-prebuild|force-rebuild|skip-mdsum|download|extract|keep-work|log)$ ]]; then
OPTS+=($1)
elif [[ "$1" =~ ^--(pkgdir|srcdir)= ]]; then
OPTS+=($1)
elif [[ "$1" =~ ^-. ]]; then
echo "Unrecognize option. ($1)"
return 1
else
PKGNAME=$1
fi
shift
done
pushd $(getportpath $PKGNAME)
if [ $? = 0 ]; then
settermtitle "Building $PKGNAME..."
pkgbuild ${OPTS[@]} || return 1
settermtitle "Building $PKGNAME done"
else
echo "Package '$PKGNAME' not found."
return 1
fi
return 0
}
installpkg() {
local pkg i int pkgcount count IPKG OPTS REINSTALL done_pkg
needroot "Installing package"
if [ -z "$1" ]; then
echo "Please specify package(s) to install."
return 1
fi
while [ "$1" ]; do
if [[ "$1" =~ ^-(c|v|l|-verbose|-ignore-conflict|-log)$ ]]; then
OPTS+=($1)
elif [[ "$1" =~ ^-(r|-reinstall)$ ]]; then
OPTS+=($1)
REINSTALL=1
elif [[ "$1" =~ ^-(d|-no-dep)$ ]]; then
NO_DEP=1
elif [[ "$1" = "--yes" ]] || [[ "$1" = "-y" ]]; then
NOCONFIRM=1
elif [[ "$1" =~ ^-. ]]; then
echo "Unrecognize option. ($1)"
return 1
else
PKGNAME+=($1)
fi
shift
done
# if reinstall, dont calculate dep, just reinstall it then exit
if [ "$REINSTALL" = 1 ]; then
for ii in ${PKGNAME[@]}; do
if [ ! $(getportpath $ii) ]; then
echo "Package '$ii' not found."
elif ! isinstalled $ii; then
echo "Package '$ii' not installed."
else
pushd $(getportpath $ii)
pkgbuild --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
break
fi
done_pkg+=($ii)
popd
fi
done
srunhooks ${done_pkg[@]}
[ "$error" = 1 ] && return 1 || return 0
fi
if [ "$NO_DEP" = 1 ]; then
for ii in ${PKGNAME[@]}; do
if [ ! $(getportpath $ii) ]; then
echo "Package '$ii' not found."
elif isinstalled $ii; then
echo "Package '$ii' already installed."
return 0
else
pushd $(getportpath $ii)
pkgbuild -i --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
break
fi
done_pkg+=($ii)
popd
fi
done
srunhooks ${done_pkg[@]}
[ "$error" = 1 ] && return 1 || return 0
fi
for i in ${PKGNAME[@]}; do
if [ ! $(getportpath $i) ]; then
echo "Package '$i' not found."
elif isinstalled $i; then
echo "Package '$i' already installed."
else
IPKG+=($i)
fi
done
if [ "${#IPKG[@]}" = 0 ]; then
echo "Nothing to do. Exiting..."
return 0
fi
echo "Resolving dependencies..."
INST="$(pkgdeplist -l -q ${IPKG[@]} | awk '{print $2}')"
if [ "$INST" ]; then
echo
pkgcount=0
for pkg in $INST; do
pkgcount=$(( $pkgcount + 1 ))
echo -en "[${GREEN}i${CRESET}] $pkg "
done
echo; echo
echo "( $pkgcount install )"
echo
if [ ! "$NOCONFIRM" ]; then
confirm "Continue install package(s)?" "Package installation cancelled."
echo
fi
error=0
count=0
total=$(echo $INST | wc -w)
for int in ${INST[@]}; do
count=$(( $count + 1 ))
portpathh=$(getportpath $int)
if [ "$portpathh" ]; then
pushd $portpathh
settermtitle "[ $count/$total ] installing $int..."
pkgbuild -i --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
count=$(( $count - 1 ))
break
fi
done_pkg+=($int)
popd
else
msgwarn "Skipping missing package: $int"
fi
unset portpathh
done
settermtitle "Triggering install hook"
srunhooks ${done_pkg[@]}
settermtitle "$count/$total package(s) installed"
return "$error"
fi
}
outdatepkg() {
local pkg
for pkg in $(allinstalled); do
if [ ! -e "$INDEX_DIR/$pkg/.lock" ] && getportpath $pkg >/dev/null; then
. $(getportpath $pkg)/$BUILD_SCRIPT
iversion=$(installed_pkg_info version $pkg)
irelease=$(installed_pkg_info release $pkg)
if [ "$release" != "$irelease" ] || [ "$version" != "$iversion" ]; then
echo $name
fi
unset iversion irelease
fi
done
}
removepkg() {
local pkg i IPKG OPTS
needroot "Removing package"
if [ -z "$1" ]; then
echo "Please specify package(s) to remove."
return 1
fi
while [ "$1" ]; do
if [[ "$1" =~ ^--(no-postremove|no-preremove)$ ]]; then
OPTS+=($1)
elif [[ "$1" = "--yes" ]] || [[ "$1" = "-y" ]]; then
NOCONFIRM=1
elif [[ "$1" =~ ^-. ]]; then
echo "Unrecognize option. ($1)"
return 1
else
PKGNAME+=($1)
fi
shift
done
for i in ${PKGNAME[@]}; do
if ! isinstalled $i; then
echo "Package '$i' not installed."
else
IPKG+=($i)
fi
done
if [ "${#IPKG[@]}" = 0 ]; then
echo "Nothing to do. Exiting..."
return 0
fi
if [ "$IPKG" ]; then
echo "Removing packages..."
echo
pkgcount=0
count=0
for pkg in ${IPKG[@]}; do
pkgcount=$(( $pkgcount + 1 ))
echo -en "[${RED}x${CRESET}] $pkg "
done
echo; echo
echo "( $pkgcount remove )"
echo
if [ ! "$NOCONFIRM" ]; then
confirm "Continue remove package(s)?" "Package removing cancelled."
echo
fi
for pkg in ${IPKG[@]}; do
count=$(( $count + 1 ))
srunpreremovehooks $pkg
settermtitle "[ $count/$pkgcount ] Removing $pkg..."
pkgdel --no-hook $pkg ${OPTS[@]} || return 1
done
settermtitle "Triggering remove hook"
srunremovehooks
settermtitle "$pkgcount package(s) removed"
fi
}
sysup() {
local d UPGPKG NEWPKG PKGOUTDATE OPTS done_pkg
needroot "Upgrading package"
while [ "$1" ]; do
if [ "$1" = "--yes" ] || [ "$1" = "-y" ]; then
NOCONFIRM=1
elif [ "$1" = "--no-backup" ]; then
OPTS+=($1)
fi
shift
done
echo "Checking for outdated packages..."
PKGOUTDATE=$(outdatepkg)
if [ ! "$PKGOUTDATE" ]; then
echo "All package is up to date."
return 0
fi
echo "Resolving dependencies..."
DEP=$(pkgdeplist ${PKGOUTDATE[@]} -l | awk '{print $2}')
echo
UPGPKG=0
NEWPKG=0
for d in $DEP; do
if [ "$(echo $PKGOUTDATE | tr ' ' '\n' | grep -x $d)" = "$d" ]; then
echo -ne "[${GREEN}u${CRESET}] $d "
WILLINSTALL+=($d)
UPGPKG=$(( $UPGPKG + 1 ))
elif ! isinstalled $d && [ $(getportpath "$d") ]; then
echo -ne "[${CYAN}n${CRESET}] $d "
WILLINSTALL+=($d)
NEWPKG=$(( $NEWPKG + 1 ))
fi
done
echo
echo
echo "( $UPGPKG upgrade, $NEWPKG new install )"
echo
if [ ! "$NOCONFIRM" ]; then
confirm "Continue upgrade/install these package(s)?" "Package upgrade cancelled."
echo
fi
error=0
count=0
total=$(echo ${WILLINSTALL[@]} | wc -w)
for inst in ${WILLINSTALL[@]}; do # install all required dependencies and target packages itself
count=$(( $count + 1 ))
pushd $(getportpath $inst)
if ! isinstalled $inst; then
settermtitle "[ $count/$total ] Installing $inst..."
pkgbuild -i --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
count=$(( $count - 1 ))
break
fi
else
settermtitle "[ $count/$total ] Upgrading $inst..."
pkgbuild -u --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
count=$(( $count - 1 ))
break
fi
fi
done_pkg+=($inst)
done
settermtitle "Triggering install hook"
srunhooks ${done_pkg[@]}
settermtitle "$count/$total package(s) upgraded"
return $error
}
upgradepkg() {
local pkg done_pkg
needroot "Upgrading package"
needarg "$@"
while [ "$1" ]; do
if [[ "$1" =~ ^--(no-postupgrade|no-preupgrade)$ ]]; then
OPTS+=($1)
elif [[ "$1" =~ ^-(-no-dep|d)$ ]]; then
NO_DEP=1
elif [[ "$1" = "--yes" ]] || [[ "$1" = "-y" ]]; then
NOCONFIRM=1
elif [[ "$1" =~ ^-. ]]; then
echo "Unrecognize option. ($1)"
exit 1
else
PKGNAME+=($1)
fi
shift
done
for pkg in ${PKGNAME[@]}; do
if ! isinstalled $pkg; then
msgerr "Package '$pkg' not installed."
return 1
fi
if [ ! $(getportpath $pkg) ]; then
msgerr "Package '$pkg' not exist."
return 1
fi
. $(getportpath $pkg)/$BUILD_SCRIPT
if [ "$(installed_pkg_info version $pkg)-$(installed_pkg_info release $pkg)" = "$version-$release" ]; then
msg "Package '$pkg' is up-to-date."
return 0
fi
done
if [ -z "$NO_DEP" ]; then
echo "Resolving dependencies..."
DEP=$(pkgdeplist -l -n ${PKGNAME[@]} | awk '{print $2}')
for dep in $DEP; do
if ! isinstalled $dep; then
if [ $(getportpath $dep) ]; then
NEWPKG+=($dep)
fi
fi
done
fi
echo
count=0
for i in ${NEWPKG[@]}; do
count=$(( $count + 1 ))
echo -en "[${CYAN}n${CRESET}] $i "
done
for i in ${PKGNAME[@]}; do
count=$(( $count + 1 ))
echo -en "[${GREEN}u${CRESET}] $i "
done
echo
echo
echo "( ${#PKGNAME[@]} upgrade, ${#NEWPKG[@]} new install )"
echo
total=$count
count=0
if [ ! "$NOCONFIRM" ]; then
confirm "Continue upgrade/install these package(s)?" "Package upgrade cancelled."
echo
fi
if [ ${#NEWPKG[@]} -gt 0 ]; then
for newpkg in ${NEWPKG[@]}; do
count=$(( $count + 1 ))
pushd $(getportpath $newpkg)
settermtitle "[ $count/$total ] Installing $newpkg..."
pkgbuild -i --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
count=$(( $count - 1 ))
break
fi
done_pkg+=($newpkg)
popd
done
fi
for pkg in ${PKGNAME[@]}; do # upgrade all target packages
count=$(( $count + 1 ))
pushd $(getportpath $pkg)
settermtitle "[ $count/$total ] Upgrading $pkg..."
pkgbuild -u --no-hook ${OPTS[@]}
if [ $? != 0 ]; then
error=1
count=$(( $count - 1 ))
break
fi
done_pkg+=($pkg)
popd
done
settermtitle "triggering upgrade hook"
srunhooks ${done_pkg[@]}
settermtitle "$count/$total package(s) upgraded"
return $error
}
outdate() {
local pkg
for pkg in $(allinstalled); do
if [ $(getportpath $pkg) ]; then
. $(getportpath $pkg)/$BUILD_SCRIPT
iversion=$(installed_pkg_info version $pkg)
irelease=$(installed_pkg_info release $pkg)
[ -f "$INDEX_DIR/$pkg/.lock" ] && ITSLOCK="[locked]"
outdatemsg="$name ${RED}$iversion-$irelease${CRESET} => ${GREEN}$version-$release${CRESET} ${CYAN}$ITSLOCK${CRESET}"
newerinstmsg="$name ${RED}$iversion-$irelease${CRESET} => ${GREEN}$version-$release${CRESET} ${YELLOW}[newer installed]${CRESET} ${CYAN}$ITSLOCK${CRESET}"
if [ "$version" != "$iversion" ]; then
vercomp $version $iversion
if [ $? = 2 ]; then
echo -e "$outdatemsg"
OUTDATE=yes
elif [ $? = 1 ]; then
echo -e "$newerinstmsg"
OUTDATE=yes
fi
elif [ "$release" != "$irelease" ]; then
vercomp $release $irelease
if [ $? = 2 ]; then
echo -e "$outdatemsg"
OUTDATE=yes
elif [ $? = 1 ]; then
echo -e "$newerinstmsg"
OUTDATE=yes
fi
fi
unset ITSLOCK
fi
done
[ ! "$OUTDATE" ] && msg "All package is up to date."
}
clearpkgcache() {
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
}
getpkgcache() {
COMPRESSION_MODE="xz"
[ -f /etc/scratchpkg.conf ] && . /etc/scratchpkg.conf
for list in "$PACKAGE_DIR"/*; do
[ -f "$list" ] && ALL_PACKAGES+=($(basename $list))
done
for list in "$SOURCE_DIR"/*; do
[ -f "$list" ] && ALL_SOURCES+=($(basename $list))
done
for repo in ${PORT_REPO[@]}; do
if [ "$(find $repo/*/ -mindepth 1 -print -quit 2>/dev/null)" ]; then # check directory if its not empty
for port in $repo/*/$BUILD_SCRIPT; do
. $port
PORT_PACKAGES+=($name-$version-$release.spkg.tar.$COMPRESSION_MODE)
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
done
fi
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
[ ${#ALL_PACKAGES[@]} -gt 0 ] && (echo ${ALL_PACKAGES[@]} | tr ' ' '\n')
[ ${#ALL_SOURCES[@]} -gt 0 ] && (echo ${ALL_SOURCES[@]} | tr ' ' '\n')
echo
echo -e "Package caches ($ALL_PACKAGES_SIZE)"
echo -e "Source caches ($ALL_SOURCES_SIZE)"
echo
if [ ${#ALL_PACKAGES[@]} -gt 0 ] || [ ${#ALL_SOURCES[@]} -gt 0 ]; then
confirm "Clear old caches?" "Old caches is keep."
needroot "Clear old caches"
clearpkgcache
fi
}
showportpath() {
needarg $@
if PPATH=$(getportpath "$1"); then
echo "$PPATH"
else
msgerr "Port '$1' not exist."
exit 1
fi
}
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
}
usage_extra() {
cat << EOF
Usage:
$(basename $0) <operation> [ <pkgname/pattern/file> ]
Operation:
depends <package> show depends of a package
search <pattern> search packages in port's repos
lock <packages> lock packages from upgrade
unlock <packages> unlock packages from upgrade
cat <package> view a package build scripts
dependent <package> show package's dependent
own <file> show package's owner of file
pkgtree <package> show list files of installed package
path <package> show package's buildscripts path
sync update port's repo
sysup full system update
dup print duplicate ports in repo
readme print readme file if exist
listinst list installed package in system
listorphan list orphan package
integrity check integrity of package's files
outdate check for outdate packages
cache print leftover cache
missingdep check for mising dependency of installed package
foreignpkg print package installed without port in repo
listlocked print locked packages
EOF
}
usage_build() {
cat << EOF
Usage:
$(basename $0) build [ <pkgname> <options> ]
Options:
-f, --force-rebuild force rebuild
-m, --skip-mdsum skip md5sum check for sources
-d, --no-dep skip dependency check
-e, --extract extract only
-w, --keep-work keep woring directory
-o, --download download source files only
-v, --verbose be verbose
--redownload re-download source files
--srcdir=<path> override default SOURCE_DIR
--pkgdir=<path> override default PACKAGE_DIR
--no-preinstall skip pre-install script
EOF
}
usage_upgrade() {
cat << EOF
Usage:
$(basename $0) upgrade [ <pkgname> <options> ]
Options:
-d, --no-dep skip installing dependencies (new dependencies)
-c, --ignore-conflict skip file conflict check
-v, --verbose print install process
--no-backup skip backup configuration file
--no-preupgrade skip pre-upgrade script
--no-postupgrade skip post-upgrade script
-y, --yes dont ask confirmation
EOF
}
usage_remove() {
cat << EOF
Usage:
$(basename $0) remove [ <pkgname> <options> ]
Options:
-d, --no-dep skip dependency check
-v, --verbose print removed files
--no-preremove skip pre-remove script
--no-postremove skip post-remove script
-y, --yes dont ask confirmation
EOF
}
usage_install() {
cat << EOF
Usage:
$(basename $0) install [ <pkgname> <options> ]
Options:
-d, --no-dep skip installing dependencies
-c, --ignore-conflict skip file conflict check
-r, --reinstall reinstall installed package
--no-preinstall skip pre-install script
--no-postinstall skip post-install script
-y, --yes dont ask confirmation
EOF
}
usage_help() {
cat << EOF
Usage:
$(basename $0) help <operation>
Operation:
build build only packages
install install packages
remove remove packages in system
upgrade upgrade packages and install new dependencies (if any)
extra various extra options
EOF
}
usage() {
if [ -z "$1" ];then
usage_help
else
case "$1" in
install) usage_install ;;
remove) usage_remove ;;
upgrade) usage_upgrade ;;
build) usage_build ;;
extra) usage_extra ;;
esac
fi
}
main() {
if [ "$mode" = "build" ]; then
buildpkg $@
exit $?
fi
if [ "$mode" = "install" ]; then
installpkg $@
exit $?
fi
if [ "$mode" = "upgrade" ]; then
upgradepkg $@
exit $?
fi
if [ "$mode" = "remove" ]; then
removepkg $@
exit $?
fi
if [ "$mode" = "sysup" ]; then
sysup $@
exit $?
fi
if [ "$mode" = "outdate" ]; then
outdate
exit $?
fi
if [ "$mode" = "listorphan" ]; then
listorphan
exit $?
fi
if [ "$mode" = "search" ]; then
searchpkg $1
exit $?
fi
# search for foreign port (installed package with no port in repos)
if [ "$mode" = "foreignpkg" ]; then
foreignpkg
exit $?
fi
if [ "$mode" = "sync" ]; then
updports
exit $?
fi
if [ "$mode" = "listinstalled" ]; then
listinstalled
exit $?
fi
if [ "$mode" = "readme" ]; then
printreadme $1
exit $?
fi
if [ "$mode" = "pkgtree" ]; then
showtree $1
exit $?
fi
if [ "$mode" = "own" ]; then
checkowner $1
exit $?
fi
if [ "$mode" = "dependent" ]; then
showdependent $1
exit $?
fi
if [ "$mode" = "cat" ]; then
catport $1
exit $?
fi
if [ "$mode" = "depends" ]; then
showdepends $1
exit $?
fi
if [ "$mode" = "lock" ]; then
lockpkg $@
exit $?
fi
if [ "$mode" = "unlock" ]; then
unlockpkg $@
exit $?
fi
if [ "$mode" = "listlocked" ]; then
listlocked
exit $?
fi
if [ "$mode" = "listinst" ]; then
listinstalled
exit $?
fi
if [ "$mode" = "cache" ]; then
pkgcache
exit $?
fi
if [ "$mode" = "help" ]; then
usage $1
exit $?
fi
if [ "$mode" = "integrity" ]; then
checkintegrity $1
exit $?
fi
if [ "$mode" = "path" ]; then
showportpath $1
exit $?
fi
if [ "$mode" = "dup" ]; then
duplicateports
exit $?
fi
if [ "$mode" = "missingdep" ]; then
missingdep
exit $?
fi
echo "Run 'scratch help' to see available mode and options"
exit 5
}
BUILD_SCRIPT="spkgbuild"
INDEX_DIR="/var/lib/scratchpkg/index"
REPO_FILE="/etc/scratchpkg.repo"
HOOK_DIR="/etc/hooks"
SOURCE_DIR="/var/cache/scratchpkg/sources"
PACKAGE_DIR="/var/cache/scratchpkg/packages"
if [ ! -f "$REPO_FILE" ]; then
msgerr "repo file not exist. ($REPO_FILE)"
exit 1
else
while read repodir repourl junk; do
case $repodir in
""|"#"*) continue ;;
esac
PORT_REPO+=($repodir)
done < "$REPO_FILE"
fi
mode=$1
shift
for opt in $@; do
case $opt in
--nocolor) nocolor ;;
--*) MAINOPTS+=($opt) ;;
-*) for (( i=1; i<${#opt}; i++ )); do MAINOPTS+=(-${opt:$i:1}); done ;;
*) MAINOPTS+=($opt) ;;
esac
done
main ${MAINOPTS[@]}