scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

pkgsetup (6529B)


      1 #!/bin/sh
      2 #
      3 # See LICENSE file for copyright and license details.
      4 #
      5 # This scripts aim to simplify the installing and bootstrapping of the pkgsrc
      6 # framework on non-NetBSD systems. It also get the pkgsrc-wip directory and
      7 # allow to keep all update. Feel free to contribute.
      8 #
      9 # TODO
     10 #
     11 #	o Add saving output to log file.
     12 #	o Add nice(1) support.
     13 #	o Replace "cvs" whit VCS_CMD, VCS_OPTS, etc.
     14 #	o Enable verbose flags for all *_CMD if -v.
     15 #	o Catch bootstrap error (see http://wiki.netbsd.org/pkgsrc/how_to_use_pkgsrc_on_linux/#index12h1)
     16 #
     17 
     18 ##
     19 # Configuration values.
     20 ##
     21 
     22 # Local paths
     23 BASE="$HOME/sources/pkgsrc"
     24 PKGS="$BASE/pkgs"
     25 BINS="$BASE/bins"
     26 TARBALL="$BASE/pkgsrc.tar.bz2"
     27 
     28 # Fetch command and tarball
     29 FETCH_CMD="wget" 
     30 FETCH_OPT="-cqO $TARBALL"
     31 FETCH_FILE="ftp://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc.tar.bz2"
     32 
     33 # Extract command
     34 EXT_CMD="tar"
     35 EXT_OPT="jxf"
     36 EXT_BOPT="-C $BASE"
     37 
     38 # SourceForge username
     39 USERNAME=anonymous
     40 
     41 ##
     42 # Globals
     43 ##
     44 VERBOSE=0
     45 UP_BASE=0
     46 UP_WIP=0
     47 
     48 # Show an error and exit.
     49 err() {
     50 	[ $VERBOSE -gt 1 ] && echo "Operation failed."
     51 	exit 1
     52 }
     53 
     54 # Show the configuration and if it applies to the working framework.
     55 do_show_conf() {
     56 	echo "
     57 Script configuration:
     58 
     59 Base dir: $BASE
     60 Packages: $PKGS
     61 Binaries: $BINS
     62 	"
     63 
     64 	if [ ! -d "$BASE" -o ! -d "$BINS" -o ! -d "$PKGS" ]; then
     65 		echo "This don't applies to a known framework."
     66 	else
     67 		echo "This applies to the framework in '$BASE'."
     68 	fi
     69 }
     70 
     71 # Show few suggestions.
     72 do_suggest() {
     73 	echo "
     74 It's likely that you want to add the following to your
     75 $BINS/usr/pkg/etc/mk.conf file:
     76 
     77    FETCH_CMD=wget
     78    FETCH_BEFORE_ARGS=-4 -nd
     79    FETCH_OUTPUT_ARGS=-O
     80    FETCH_RESUME_ARGS=-c
     81    PKG_RESUME_TRANSFERS=YES
     82 
     83 To include man pages of packages you may need set the MANPATH environment
     84 variable or change the man(1) configuration file, by adding the following
     85 path:
     86 
     87    $BINS/usr/pkg/man
     88    
     89 You may also want to add the following to your ~/.profile:
     90 
     91    export PATH="\$PATH:$BINS/usr/pkg/bin:$BINS/usr/pkg/sbin"
     92 
     93 For more informations read the documentation about pkgsrc{-wip}, man interface,
     94 shell, etc.
     95 	"
     96 }
     97 
     98 # Remove the whole framework.
     99 do_deinstall() {
    100 	[ $VERBOSE -gt 0 ] && echo "Deinstalling the framework..."
    101 
    102 	if [ -d "$BASE/pkgsrc" ]; then
    103 		[ $VERBOSE -gt 1 ] && echo "Removing $BASE/pkgsrc..."
    104 		rm -rf "$BASE/pkgsrc"
    105 	fi
    106 	if [ -d "$PKGS" ]; then
    107 		[ $VERBOSE -gt 1 ] && echo "Removing $PKGS..."
    108 		rm -rf "$PKGS"
    109 	fi
    110 	if [ -d "$BINS" ]; then
    111 		[ $VERBOSE -gt 1 ] && echo "Removing $BINS..."
    112 		rm -rf "$BINS"
    113 	fi
    114 }
    115 
    116 # Prepare the builds of the framework. Fetch and extract the tarball. Finally
    117 # creates the $BASE directory.
    118 do_prepare() {
    119 	mkdir -p "$BASE"
    120 
    121 	# Fetch the tarball
    122 	[ $VERBOSE -gt 1 ] && echo "Fetching the tarball ($(basename $TARBALL))..."
    123 	"$FETCH_CMD" $FETCH_OPT "$FETCH_FILE"
    124 	[ $? -ne 0 ] && err
    125 
    126 	# Extract the tarball
    127 	[ $VERBOSE -gt 1 ] && echo "Extracting the tarball to $PKGS..."
    128 	"$EXT_CMD" $EXT_OPT "$TARBALL" $EXT_BOPT
    129 	mv "$BASE/pkgsrc" "$PKGS"
    130 	[ $? -ne 0 ] && err
    131 }
    132 
    133 # Bootstrap pkgsrc.
    134 do_bootstrap() {
    135 	[ $VERBOSE -gt 1 ] && echo "Bootsrapping pkgsrc..."
    136 
    137 	cd "$PKGS/bootstrap"
    138 	[ -d "$PKGS/bootstrap/work" ] && . "$PKGS/bootstrap/cleanup" && rm -rf "$BINS"
    139 
    140 	./bootstrap	--unprivileged				\
    141 			--prefix="$BINS/usr/pkg"		\
    142 			--pkgdbdir="$BINS/var/db/pkg"		\
    143 			--sysconfdir="$BINS/usr/pkg/etc"	\
    144 			--varbase="$BINS/var"			\
    145 			2>&1 > /dev/null
    146 	[ $? -ne 0 ] && err
    147 
    148 	do_suggest
    149 }
    150 
    151 # Update the selected tree(s).
    152 do_update() {
    153 	[ $UP_BASE -eq 0 -a $UP_WIP -eq 0 ] && echo "You have to specify at least one of -w or -b." && err
    154 
    155 	[ $VERBOSE -gt 0 ] && echo "Updating the pkgsrc tree..."
    156 
    157 	cd "$PKGS"
    158 
    159 	# Base
    160 	if [ $UP_BASE -ne 0 ]; then
    161 		[ $VERBOSE -gt 1 ] && echo "Updating base..."
    162 		cvs up
    163 	fi
    164 
    165 	# Pkgsrc-wip
    166 	if [ $UP_WIP -ne 0 ]; then
    167 
    168 		[ $VERBOSE -gt 1 ] && echo "Updating wip..."
    169 
    170 		if [ -d "$PKGS/wip" ]; then
    171 			[ $VERBOSE -gt 2 ] && echo "The pkgsrc-wip tree already exists. Updating..."
    172 			cvs up
    173 		else
    174 
    175 			[ -z "$USERNAME" ] && echo "You have to specify the username. See the -U flag." && err
    176 
    177 			[ $VERBOSE -gt 2 ] && echo "No pkgsrc-wip tree. First checkout..."
    178 			cvs -d :ext:$USERNAME@pkgsrc-wip.cvs.sourceforge.net:/cvsroot/pkgsrc-wip co -P wip
    179 		fi
    180 	fi
    181 
    182 }
    183 
    184 # Show the usage.
    185 usage() {
    186 	echo "Usage: $0 [-ubwidvVh]"
    187 
    188 	[ -z "$1" ] && exit 1
    189 
    190 	echo "
    191 Options:
    192    -u\t\t: update the selected tree(s)
    193    -b\t\t: select the base tree for update
    194    -w\t\t: select the -wip tree for update
    195    -i\t\t: install the framework (fetch and extract the tarball first)
    196    -d\t\t: deinstall the framework (use this only if you know what you are doing)
    197    -c\t\t: cleanup the base directory from unused files
    198    -s\t\t: print the suggestions (usually shown after bootstrap)
    199    -S\t\t: show the actual configuration
    200    -B\t\t: just bootstrap pkgsrc (use with caution)
    201    -U <user>\t: specify the pkgsrc-wip CVS username
    202    -v\t\t: Enable verbose. Specify it more times to be more verbose.
    203    -V\t\t: show version informations
    204    -h\t\t: print this help
    205 	"
    206 
    207 	exit 1
    208 }
    209 
    210 # Remove unused files.
    211 do_cleanup() {
    212 	[ $VERBOSE -gt 0 ] && echo "Cleaning up..."
    213 	rm -rf "$TARBALL"
    214 }
    215 
    216 # The main function.
    217 main() {
    218 	deinstall=0
    219 	prepare=0
    220 	bootstrap=0
    221 	update=0
    222 	cleanup=0
    223 
    224 	while getopts "ubwidcsSBU:vVh" arg
    225 	do
    226 		case $arg in
    227 			u) update=1 ;;
    228 			b) UP_BASE=1;;
    229 			w) UP_WIP=1;;
    230 			i) # Install
    231 				if [ $deinstall -eq 0 -a -d "$PKGS" -o -d "$BINS" ]; then
    232 				echo "
    233 Dirty base directory. You have to deinstall the framework first. If you already
    234 installed it but didn't bootstrap pkgsrc, then you may want to just bootstrap
    235 it, saving lots of time. In this case, just run: $0 -B
    236 If unsure, reinstall the whole framework with the following command: $0 -di
    237 "
    238 				exit 1
    239 	fi
    240 				prepare=1
    241 				bootstrap=1
    242 				update=1
    243 				;;
    244 
    245 			d) deinstall=1;;
    246 			c) cleanup=1;;
    247 			s) do_suggest;;
    248 			S) do_show_conf;;
    249 			B) bootstrap=1;;
    250 			U) USERNAME="$OPTARG";;
    251 			v) VERBOSE=$(echo $VERBOSE+1|bc);;
    252 			V)
    253 				echo "Pkgsetup-0.0.2 (C) $(date +'%Y') Claudio M. Alessi"
    254 				exit 0
    255 				;;
    256 			h) usage 1;;
    257 			*) usage;;
    258 		esac
    259 	done
    260 
    261 	[ $prepare -eq 0 -a ! -d "$PKGS" ] && echo "No framework found. Install it first (see the -i flag)." && err
    262 
    263 	opdo=0
    264 	[ $deinstall -gt 0 ] &&	do_deinstall && opdo=$(echo $opdo+1|bc)
    265 	[ $prepare -gt 0 ] &&	do_prepare && opdo=$(echo $opdo+1|bc)
    266 	[ $bootstrap -gt 0 ] &&	do_bootstrap && opdo=$(echo $opdo+1|bc)
    267 	[ $update -gt 0 ] &&	do_update && opdo=$(echo $opdo+1|bc)
    268 	[ $cleanup -gt 0 ] &&	do_cleanup && opdo=$(echo $opdo+1|bc)
    269 
    270 	[ $opdo -gt 0 ] && echo "All done ($opdo of 5 phases)." || usage
    271 }
    272 
    273 main "$@"