Code clean up part 4
- use "[" instead of "test" - remove double quotes where word splitting does not occur - revise some "sed" statements - use "((" construct where possible - use "return" in place of long winded "if" statements - use "+=" where possible - use "type" instead of "which" - use "[[" construct where appropriate
This commit is contained in:
parent
b42a6cb813
commit
663a6d55ae
109
apt-cyg
109
apt-cyg
|
@ -68,18 +68,20 @@ function findworkspace()
|
||||||
cache=/setup
|
cache=/setup
|
||||||
|
|
||||||
# work wherever setup worked last, if possible
|
# work wherever setup worked last, if possible
|
||||||
|
if [ -e /etc/setup/last-cache ]
|
||||||
if test -e /etc/setup/last-cache
|
|
||||||
then
|
then
|
||||||
tmp="`head -1 /etc/setup/last-cache`"
|
tmp=$(</etc/setup/last-cache)
|
||||||
cache="`cygpath -au "$tmp"`"
|
cache=$(cygpath -au "$tmp")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if test -e /etc/setup/last-mirror
|
if [ -e /etc/setup/last-mirror ]
|
||||||
then
|
then
|
||||||
mirror="`head -1 /etc/setup/last-mirror`"
|
mirror=$(</etc/setup/last-mirror)
|
||||||
fi
|
fi
|
||||||
mirrordir="`echo "$mirror" | sed -e "s/:/%3a/g" -e "s:/:%2f:g"`"
|
mirrordir=$(sed '
|
||||||
|
s / %2f g
|
||||||
|
s : %3a g
|
||||||
|
' <<< "$mirror")
|
||||||
|
|
||||||
echo Working directory is $cache
|
echo Working directory is $cache
|
||||||
echo Mirror is $mirror
|
echo Mirror is $mirror
|
||||||
|
@ -89,36 +91,32 @@ function findworkspace()
|
||||||
|
|
||||||
function getsetup()
|
function getsetup()
|
||||||
{
|
{
|
||||||
if test "$noscripts" == "0" -a "$noupdate" == "0"
|
(( noscripts || noupdate )) && return
|
||||||
|
touch setup.ini
|
||||||
|
mv setup.ini setup.ini-save
|
||||||
|
wget -N $mirror/$ARCH/setup.bz2
|
||||||
|
if [ -e setup.bz2 ]
|
||||||
then
|
then
|
||||||
touch setup.ini
|
bunzip2 setup.bz2
|
||||||
mv setup.ini setup.ini-save
|
mv setup setup.ini
|
||||||
wget -N $mirror/$arch/setup.bz2
|
echo Updated setup.ini
|
||||||
if test -e setup.bz2 && test $? -eq 0
|
else
|
||||||
|
wget -N $mirror/$ARCH/setup.ini
|
||||||
|
if [ -e setup.ini ]
|
||||||
then
|
then
|
||||||
bunzip2 setup.bz2
|
|
||||||
mv setup setup.ini
|
|
||||||
echo Updated setup.ini
|
echo Updated setup.ini
|
||||||
else
|
else
|
||||||
wget -N $mirror/$arch/setup.ini
|
mv setup.ini-save setup.ini
|
||||||
if test -e setup.ini && test $? -eq 0
|
echo Error updating setup.ini, reverting
|
||||||
then
|
|
||||||
echo Updated setup.ini
|
|
||||||
else
|
|
||||||
mv setup.ini-save setup.ini
|
|
||||||
echo Error updating setup.ini, reverting
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkpackages()
|
function checkpackages()
|
||||||
{
|
{
|
||||||
if test "-$packages-" = "--"
|
(( ${#packages} )) && return
|
||||||
then
|
echo Nothing to do, exiting
|
||||||
echo Nothing to do, exiting
|
exit
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# process options
|
# process options
|
||||||
|
@ -131,7 +129,7 @@ command=""
|
||||||
filepackages=""
|
filepackages=""
|
||||||
packages=""
|
packages=""
|
||||||
|
|
||||||
while test $# -gt 0
|
while (( $# ))
|
||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
|
||||||
|
@ -166,7 +164,7 @@ do
|
||||||
;;
|
;;
|
||||||
|
|
||||||
--file | -f)
|
--file | -f)
|
||||||
if ! test "-$2-" = "--"
|
if (( ${#2} ))
|
||||||
then
|
then
|
||||||
file="$2"
|
file="$2"
|
||||||
dofile=1
|
dofile=1
|
||||||
|
@ -178,26 +176,26 @@ do
|
||||||
;;
|
;;
|
||||||
|
|
||||||
update | show | find | describe | packageof | install | remove)
|
update | show | find | describe | packageof | install | remove)
|
||||||
if test "-$command-" = "--"
|
if (( ${#command} ))
|
||||||
then
|
then
|
||||||
command=$1
|
packages+=" $1"
|
||||||
else
|
else
|
||||||
packages="$packages $1"
|
command=$1
|
||||||
fi
|
fi
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
packages="$packages $1"
|
packages+=" $1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if test $dofile = 1
|
if (( dofile ))
|
||||||
then
|
then
|
||||||
if test -f "$file"
|
if [ -f "$file" ]
|
||||||
then
|
then
|
||||||
filepackages+=$(awk '{printf " %s", $0}' "$file")
|
filepackages+=$(awk '{printf " %s", $0}' "$file")
|
||||||
else
|
else
|
||||||
|
@ -248,17 +246,20 @@ case "$command" in
|
||||||
checkpackages
|
checkpackages
|
||||||
for pkg in $packages
|
for pkg in $packages
|
||||||
do
|
do
|
||||||
key=`which "$pkg" 2>/dev/null | sed "s:^/::"`
|
key=$(type -P "$pkg" | sed s./..)
|
||||||
if test "-$key-" = "--"
|
if (( ! ${#key} ))
|
||||||
then
|
then
|
||||||
key="$pkg"
|
key=$pkg
|
||||||
fi
|
fi
|
||||||
for manifest in /etc/setup/*.lst.gz
|
for manifest in /etc/setup/*.lst.gz
|
||||||
do
|
do
|
||||||
found=$(gzip -cd $manifest | grep -c "$key")
|
found=$(gzip -cd $manifest | grep -c "$key")
|
||||||
if test $found -gt 0
|
if (( found ))
|
||||||
then
|
then
|
||||||
package=`echo $manifest | sed -e "s:/etc/setup/::" -e "s/.lst.gz//"`
|
package=$(sed '
|
||||||
|
s,/etc/setup/,,
|
||||||
|
s,.lst.gz,,
|
||||||
|
' <<< $manifest)
|
||||||
echo Found $key in the package $package
|
echo Found $key in the package $package
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
@ -273,7 +274,7 @@ case "$command" in
|
||||||
do
|
do
|
||||||
|
|
||||||
already=`grep -c "^$pkg " /etc/setup/installed.db`
|
already=`grep -c "^$pkg " /etc/setup/installed.db`
|
||||||
if test $already -ge 1
|
if (( already ))
|
||||||
then
|
then
|
||||||
echo Package $pkg is already installed, skipping
|
echo Package $pkg is already installed, skipping
|
||||||
continue
|
continue
|
||||||
|
@ -295,7 +296,7 @@ case "$command" in
|
||||||
}
|
}
|
||||||
' RS='\n\n@ ' FS='\n' package="$pkg" setup.ini > "release/$pkg/desc"
|
' RS='\n\n@ ' FS='\n' package="$pkg" setup.ini > "release/$pkg/desc"
|
||||||
desc=$(<"release/$pkg/desc")
|
desc=$(<"release/$pkg/desc")
|
||||||
if test "-$desc-" = "-Package not found-"
|
if [[ $desc = 'Package not found' ]]
|
||||||
then
|
then
|
||||||
echo Package $pkg not found or ambiguous name, exiting
|
echo Package $pkg not found or ambiguous name, exiting
|
||||||
rm -r "release/$pkg"
|
rm -r "release/$pkg"
|
||||||
|
@ -308,7 +309,7 @@ case "$command" in
|
||||||
# pick the latest version, which comes first
|
# pick the latest version, which comes first
|
||||||
install=$(awk '/^install: / {print $2; exit}' "release/$pkg/desc")
|
install=$(awk '/^install: / {print $2; exit}' "release/$pkg/desc")
|
||||||
|
|
||||||
if test "-$install-" = "--"
|
if (( ! ${#install} ))
|
||||||
then
|
then
|
||||||
echo 'Could not find "install" in package description: obsolete package?'
|
echo 'Could not find "install" in package description: obsolete package?'
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -321,7 +322,7 @@ case "$command" in
|
||||||
# check the md5
|
# check the md5
|
||||||
digest=$(awk '/^install: / {print $4; exit}' desc)
|
digest=$(awk '/^install: / {print $4; exit}' desc)
|
||||||
digactual=$(md5sum $file | awk NF=1)
|
digactual=$(md5sum $file | awk NF=1)
|
||||||
if ! test $digest = $digactual
|
if [ $digest != $digactual ]
|
||||||
then
|
then
|
||||||
echo MD5 sum did not match, exiting
|
echo MD5 sum did not match, exiting
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -356,31 +357,31 @@ case "$command" in
|
||||||
}
|
}
|
||||||
' rq='^requires: ' "release/$pkg/desc")
|
' rq='^requires: ' "release/$pkg/desc")
|
||||||
warn=0
|
warn=0
|
||||||
if ! test "-$requires-" = "--"
|
if (( ${#requires} ))
|
||||||
then
|
then
|
||||||
echo Package $pkg requires the following packages, installing:
|
echo Package $pkg requires the following packages, installing:
|
||||||
echo $requires
|
echo $requires
|
||||||
for package in $requires
|
for package in $requires
|
||||||
do
|
do
|
||||||
already=`grep -c "^$package " /etc/setup/installed.db`
|
already=`grep -c "^$package " /etc/setup/installed.db`
|
||||||
if test $already -ge 1
|
if (( already ))
|
||||||
then
|
then
|
||||||
echo Package $package is already installed, skipping
|
echo Package $package is already installed, skipping
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
apt-cyg --noscripts install $package
|
apt-cyg --noscripts install $package
|
||||||
(( $? )) && warn=1
|
(( $? && warn++ ))
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
if ! test $warn = 0
|
if (( warn ))
|
||||||
then
|
then
|
||||||
echo "Warning: some required packages did not install, continuing"
|
echo 'Warning: some required packages did not install, continuing'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# run all postinstall scripts
|
# run all postinstall scripts
|
||||||
|
|
||||||
pis=`ls /etc/postinstall/*.sh 2>/dev/null | wc -l`
|
pis=`ls /etc/postinstall/*.sh 2>/dev/null | wc -l`
|
||||||
if test $pis -gt 0 && ! test $noscripts -eq 1
|
if (( pis && ! noscripts ))
|
||||||
then
|
then
|
||||||
echo Running postinstall scripts
|
echo Running postinstall scripts
|
||||||
for script in /etc/postinstall/*.sh
|
for script in /etc/postinstall/*.sh
|
||||||
|
@ -400,7 +401,7 @@ case "$command" in
|
||||||
do
|
do
|
||||||
|
|
||||||
already=`grep -c "^$pkg " /etc/setup/installed.db`
|
already=`grep -c "^$pkg " /etc/setup/installed.db`
|
||||||
if test $already = 0
|
if (( ! already ))
|
||||||
then
|
then
|
||||||
echo Package $pkg is not installed, skipping
|
echo Package $pkg is not installed, skipping
|
||||||
continue
|
continue
|
||||||
|
@ -408,13 +409,13 @@ case "$command" in
|
||||||
dontremove="cygwin coreutils gawk bzip2 tar wget bash"
|
dontremove="cygwin coreutils gawk bzip2 tar wget bash"
|
||||||
for req in $dontremove
|
for req in $dontremove
|
||||||
do
|
do
|
||||||
if test "-$pkg-" = "-$req-"
|
if [[ $pkg = $req ]]
|
||||||
then
|
then
|
||||||
echo apt-cyg cannot remove package $pkg, exiting
|
echo apt-cyg cannot remove package $pkg, exiting
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
if ! test -e "/etc/setup/$pkg.lst.gz"
|
if [ ! -e "/etc/setup/$pkg.lst.gz" ]
|
||||||
then
|
then
|
||||||
echo Package manifest missing, cannot remove $pkg. Exiting
|
echo Package manifest missing, cannot remove $pkg. Exiting
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -423,7 +424,7 @@ case "$command" in
|
||||||
|
|
||||||
# run preremove scripts
|
# run preremove scripts
|
||||||
|
|
||||||
if test -e "/etc/preremove/$pkg.sh"
|
if [ -e "/etc/preremove/$pkg.sh" ]
|
||||||
then
|
then
|
||||||
"/etc/preremove/$pkg.sh"
|
"/etc/preremove/$pkg.sh"
|
||||||
rm "/etc/preremove/$pkg.sh"
|
rm "/etc/preremove/$pkg.sh"
|
||||||
|
|
Loading…
Reference in New Issue