1 Unstar Star 1 Fork 0

肖成创 / raspberrypi-toolsother

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
fork of https://github.com/raspberrypi/tools.git spread retract

Clone or download
sysidk 10.28 KB
Copy Edit Web IDE Raw Blame History
Gray Girling authored 2012-07-25 16:44 . regular refresh of scripts
#!/bin/bash
# sysidk: System Image Development Kit
# Potentially stand-alone access to the sysidk scripts and installation
# management
cmd="$0"
cmd_dir="`dirname -- "$cmd"`"
cmd_dir=`(cd "$cmd_dir">/dev/null;pwd)`
cmd_name="`basename -- $cmd`"
dir_sysidk="$HOME/.sysidk"
rc_sysidk="sysidk.rc"
env_sysidk="sysidk.env"
default_dir_bin="/opt/sysidk/bin"
default_dir_pkg="/var/sysidk"
default_dir_localbin="$dir_sysidk/bin"
default_dir_localpkg="$dir_sysidk/pkg"
sysidk_users=users
[ -z "$DIR_BCM_RELEASES" ] || default_dir_pkg="$DIR_BCM_RELEASES"
help()
{
if [ "_$1" = "_commands" ]; then
sysidk_help_commands
else
echo "syntax: <command> <options>"
echo " Run a system image development kit command"
echo "syntax: --help commands"
echo " List the <commands>"
echo "syntax: <command> --help"
echo " Help about command"
echo
echo "syntax: --setup [-l] [-b <install_dir>] [-p <pkg_dir>] <scripts package> "
echo " Place sysidk scripts in the installation directory"
echo " -l Use local directories - no need to setup with root access"
echo " -b Set binary directory for scripts"
echo " ($default_dir_bin by default or "
echo " $default_dir_localbin if using -l)"
echo " -p Set directory for storing packages"
echo " ($default_dir_pkg by default or "
echo " $default_dir_localpkg if using -l)"
echo "syntax: --add <package>..."
echo " Install sysidk packages"
echo " (Use 'sysidk sysipk -h' for other package management options)"
echo
echo "syntax: --install [-l] [-b <install_dir>] [-p <pkg_dir>] <scripts package> "
echo " Setup (as --setup below) and install sysidk commands into your"
echo " shell sessions (on next logon)"
echo "syntax: --uninstall"
echo " Uninstall sysidk commands from your shell sessions (effective"
echo " after you next logon)"
echo
echo "syntax: --bindir"
echo " Show installed binary directory"
echo "syntax: --pkgdir"
echo " Show installed packages directory"
fi
}
content_env()
{ local scripts="$1"; shift
local pkgs="$1"; shift
cat <<EOF
# do no edit - written by $cmd_name --setup
# this file is intended to be 'source'd by a bourne shell
# last updated `date`
dir_bcm_scripts="$scripts"
DIR_BCM_RELEASES="$pkgs"
EOF
return $?
}
content_rc()
{ cat <<EOF
# do no edit - written by $cmd_name --setup
# this file is intended to be 'source'd by a bourne shell during shell startup
# last updated `date`
source "$dir_sysidk/$env_sysidk"
export DIR_BCM_RELEASES
if [ -f "\$dir_bcm_scripts/bcm_setup" ]; then
source "\$dir_bcm_scripts/bcm_setup"
fi
EOF
return $?
}
setup_dir_sysidk()
{ local do_update=false
local rc=0
[ "_$1" = "_-u" ] && { do_update=true; shift; }
if [ ! -d "$dir_sysidk" ]; then
mkdir "$dir_sysidk"
do_update=true
fi
if [ ! -d "$dir_sysidk" ]; then
echo "$cmd_name: can't set up $dir_sysidk - sorry"
rc=1
fi
return $rc
}
check_have_scripts()
{ local rc=0
if [ -z "$dir_bcm_scripts" ]; then
echo "$cmd_name: Sorry - you need to install the scripts">&2
echo "$cmd_name: use --setup or --install">&2
rc=2
elif [ ! -d "$dir_bcm_scripts" ]; then
echo "$cmd_name: Your scripts directory doesn't exist - $dir_bcm_scripts">&2
echo "$cmd_name: try reinstalling using --setup or --install?">&2
rc=1
fi
return $rc
}
check_have_packages()
{ local rc=0
if [ -z "$DIR_BCM_RELEASES" ]; then
echo "$cmd_name: Sorry - you need to install the scripts">&2
echo "$cmd_name: use --setup or --install">&2
rc=2
elif [ ! -d "$DIR_BCM_RELEASES" ]; then
echo "$cmd_name: Your packages directory doesn't exist - $DIR_BCM_RELEASES">&2
echo "$cmd_name: try reinstalling using --setup or --install?">&2
rc=1
fi
return $rc
}
sysidk_help_commands()
{
if setup_dir_sysidk; then
[ -r "$dir_sysidk/$env_sysidk" ] && . "$dir_sysidk/$env_sysidk"
if check_have_scripts; then
echo "$cmd_name: Commands"
ls "$dir_bcm_scripts" | grep -v "^lib$" | xargs -I ^ echo " ^"
fi
fi
}
sysidk_add()
{ local rc=0
if ! PATH="$dir_bcm_scripts:$PATH" type sysipk>/dev/null 2>/dev/null; then
echo "$cmd_name: your installed scripts don't include the 'sysipk' command">&2
echo "$cmd_name: add the scripts package first?">&2
rc=1
else
local opts=
local pk
local type
for pk in "$@"; do
case "$pk" in
-* | --*) opts="$opts $pk";;
*-root-nodata.tgz | *-data-root.tgz | *-root.tgz) type=root;;
*-boot.tgz) type=boot;;
*-trk.tgz) type=track;;
*-bin.tgz) type=scripts;;
*) type=unknown;;
esac
if [ "$type" = "unknown" ]; then
echo "$cmd_name: sorry - don't recognize the package type of '$pk'"
rc=1
elif [ ! -z "$type" ]; then
# echo "sysipk $type $opts --install $pk">&2
PATH="$dir_bcm_scripts:$PATH" sysipk $type $opts --install "$pk"
rc=$?
fi
done
fi
return $rc
}
sysidk_setup()
{ local scripts="$default_dir_bin"
local pkgs="$default_dir_pkg"
local rc=0
local cmd_sudo=sudo
if [ "_$1" = "_-l" -o "_$1" = "_--local" ]; then
cmd_sudo=
scripts="$default_dir_localbin"
scripts="$default_dir_localpkg"
fi
[ "_$1" = "_-p" -o "_$1" = "_--package" ] && { pkgs="$1"; shift; }
[ "_$1" = "_-b" -o "_$1" = "_--bin" ] && { scripts="$1"; shift; }
if [ ! -z "$cmd_sudo" ] && \
! type $cmd_sudo >/dev/null 2>/dev/null; then
echo "$cmd_name: sorry - this scripts needs a '$cmd_sudo' command to execute as root" >&2
elif ! $cmd_sudo mkdir -p "$scripts"; then
echo "$cmd_name: can't create a place for your scripts - do you need root access?">&2
rc=1
elif ! $cmd_sudo mkdir -p "$pkgs"; then
echo "$cmd_name: can't create a place for your packages - $pkgs">&2
rc=2
else
$cmd_sudo chgrp $sysidk_users "$pkgs" "$scripts"
$cmd_sudo chmod g+rw "$pkgs" "$scripts"
echo "$cmd_name: creating $dir_sysidk/$env_sysidk">&2
content_env "$scripts" "$pkgs" > "$dir_sysidk/$env_sysidk"
echo "$cmd_name: creating $dir_sysidk/$rc_sysidk">&2
content_rc > "$dir_sysidk/$rc_sysidk"
rc=$?
fi
if [ $rc -eq 0 -a $# -gt 0 ]; then
local script_pkg="$1"; shift
local unpack=
case "$script_pkg" in
*-bin.tgz) unpack="-xzf";;
*) ;;
esac
if [ -z "$unpack" ]; then
echo "$cmd_name: scripts package name has unrecognized format">&2
rc=3
else
echo "$cmd_name: unpacking scripts into $scripts">&2
tar -C "$scripts" $unpack "$script_pkg"
rc=$?
fi
fi
if [ $rc -eq 0 -a $# -gt 0 ]; then
. "$dir_sysidk/$env_sysidk"
sysidk_add "$@"
rc=$?
fi
return $rc
}
sysidk_install()
{ # update the user's shell RC script to include the scripts on the cmd
local remove=false
[ "_$1" = "_-d" ] && { shift; remove=true; }
local rc_file=".bashrc"
local firstline="#=== sysidk set-up"
local lastline="#=== end of sysidk set-up"
local rc=0
local tmp="/tmp/${cmd_name}_instrc"
case "$SHELL" in
*/bash) rc_file=.bashrc;;
*/dash) rc_file=.profile;;
*/sh) rc_file=.profile;;
# *csh) rc_file=.cshrc;; # not a bourne shell
*) rc_file=.bashrc
echo "$cmd_name: sorry don't recongize your SHELL '$SHELL' - updating $rc_file">&2
;;
esac
rc_file="$HOME/$rc_file"
# rc_file=test.rc
echo "$cmd_name: updating $rc_file";
if [ ! -r "$rc_file" ]; then
echo "$cmd_name: you haven't defined a $rc_file shell initialization file!">&2
rc=1
elif $remove; then
if grep "^$firstline" "$rc_file" >/dev/null; then
sed -itmp -e "/$firstline/,/$lastline/ d" "$rc_file"
rm -f "$rc_file.tmp"
else
echo "$cmd_name: no installation found in $rc_file">&2
fi
else
{ echo "$firstline"
echo "# (please leave '#===' lines) last updated `date`"
echo "source \"$dir_sysidk/$rc_sysidk\""
echo "$lastline"
} > $tmp
if grep "^$firstline" "$rc_file" >/dev/null; then
sed -itmp -e "/$lastline/ r $tmp" \
-e "/$firstline/,/$lastline/ d" "$rc_file"
rm -f "$rc_file.tmp"
else
# echo "$cmd_name: appended"
{ echo; cat $tmp; } >> "$rc_file"
fi
rm -f "$tmp"
fi
return $rc
}
rc=0
if [ "_$1" = "_--help" -o "_$1" = "_-h" ]; then
shift
help "$@" >&2
exit 1
elif ! setup_dir_sysidk; then
exit 1
fi
if [ "_$1" = "_--install" -o \
"_$1" = "_--uninstall" -o \
"_$1" = "_--setup" ]; then
op="$1"
shift
if [ "_$op" = "_--install" ]; then
sysidk_setup "$@"
rc=$?
[ $rc -eq 0 ] && {
sysidk_install "$@"
rc=$?
}
elif [ "_$op" = "_--uninstall" ]; then
sysidk_install -d "$@"
rc=$?
else
sysidk_setup "$@"
rc=$?
fi
exit $rc
fi
. "$dir_sysidk/$env_sysidk"
if [ "_$1" = "_--add" -o "_$1" = "_-a" ]; then
shift
if check_have_scripts && check_have_packages; then
sysidk_add "$@"
rc=$?
else
rc=2
fi
elif [ "_$1" = "_--bindir" -o "_$1" = "_-b" ]; then
echo "$dir_bcm_scripts"
test ! -z "$dir_bcm_scripts"
rc=$?
elif [ "_$1" = "_--pkgdir" -o "_$1" = "_-p" ]; then
echo "$DIR_BCM_RELEASES"
test ! -z "$DIR_BCM_RELEASES"
rc=$?
else
# run the command without requiring previous installation
if check_have_scripts && check_have_packages; then
PATH="$dir_bcm_scripts:$PATH" "$@"
rc=$?
else
rc=2
fi
fi
exit $rc

Comment ( 0 )

Sign in for post a comment

other
1
https://gitee.com/xiaochengchuang/raspberrypi-tools.git
git@gitee.com:xiaochengchuang/raspberrypi-tools.git
xiaochengchuang
raspberrypi-tools
raspberrypi-tools
master

Search