#!/bin/sh -e
. ./Funicularfile
DRIVER_DIR=`dirname $0`
. ${DRIVER_DIR}/fu-platform-${PLATFORM}.sh
runnable() {
command -V $1 >/dev/null
}
funicular_init() {
imgtype=$1
shift
case $imgtype in
system)
if [ -e $SYSTEM_IMAGE ]; then
echo "$SYSTEM_IMAGE already exists! Delete it first."
exit 1
fi
platform_initsys "$SYSTEM_IMAGE"
;;
setup)
funicular_initsetup "$SETUP_IMAGE"
;;
dist)
if [ -e $DIST_IMAGE ]; then
echo "$DIST_IMAGE already exists! Delete it first."
exit
fi
platform_initdist "$DIST_IMAGE"
;;
*)
echo "Usage: funicular init (system|setup|dist)"
exit 1
esac
}
funicular_install() {
if [ ! -e $INSTALL_IMAGE ]; then
echo "$INSTALL_IMAGE does not exist. Obtain it first."
echo $INSTALL_IMAGE_URL
exit 1
fi
if runnable install_instructions; then
cat <<EOF
=========================
INSTALLATION INSTRUCTIONS
=========================
EOF
install_instructions
fi
platform_install $*
}
funicular_initsetup() {
echo "Creating setup image '$1'..."
rm -rf "$1"
mkdir -p distfiles
for url in $DISTFILES; do
base=`echo $url | awk '{split($0,a,"@"); print a[1]}'`
dest=`echo $url | awk '{split($0,a,"@"); print a[2]}'`
if [ "X$dest" = X ]; then
dest=`basename $url`
fi
url="$base"
if [ -e distfiles/$dest ]; then
echo "distfiles/$dest already fetched"
else
wget $url -O distfiles/$dest
fi
done
mkdir -p distrepos
for spec in $DISTREPOS; do
path=`echo $spec | awk '{split($0,a,"@"); print a[1]}'`
tag=`echo $spec | awk '{split($0,a,"@"); print a[2]}'`
url="$DISTREPO_ROOT$path"
dest=`basename $url`
if runnable name_map; then
dest=`name_map $dest`
fi
if [ ! -d distrepos/$dest ]; then
git clone $url distrepos/$dest || exit 1
fi
branch=`cd distrepos/$dest && git rev-parse --abbrev-ref HEAD`
if [ "X$branch" != "XHEAD" ]; then
(cd distrepos/$dest && git pull) || exit 1
fi
if [ "X$tag" != X ]; then
(cd distrepos/$dest && git checkout $tag) || exit 1
fi
done
rm -rf staging_area
mkdir -p staging_area
for file in distfiles/*; do
destname=`basename $file`
if runnable name_map; then
destname=`name_map $destname`
fi
cp -p $file staging_area/$destname
done
cd distrepos
for repo in *; do
tar zcf ../staging_area/$repo.tar.gz $repo
done
cd ..
if [ -d skel ]; then
cp -Rp skel/* staging_area/
fi
if runnable setup_script; then
setup_script
fi
platform_initsetup "$1"
#rm -rf staging_area
}
funicular_setup() {
if runnable setup_instructions; then
cat <<EOF
==================
SETUP INSTRUCTIONS
==================
EOF
setup_instructions
fi
platform_setup $*
}
funicular_start() {
platform_start $*
}
funicular_builddist() {
if [ ! -e $DIST_IMAGE ]; then
echo "$DIST_IMAGE does not exist. (Initialize it first.)"
exit 1
fi
if runnable dist_script; then
dist_script $*
fi
if runnable dist_instructions; then
cat <<EOF
=========================
DISTRIBUTION INSTRUCTIONS
=========================
EOF
dist_instructions
fi
platform_dist $*
}
funicular_distboot() {
if [ ! -e $DIST_IMAGE ]; then
echo "$DIST_IMAGE does not exist. (Initialize and build it first.)"
exit 1
fi
platform_distboot $*
}
funicular_backup() {
bu=`basename $SYSTEM_IMAGE`
bu="$bu-backup.tar.gz"
if [ "X$1" != X ]; then
bu=$1
fi
if [ -e $bu ]; then
echo "$bu already exists! Delete it or rename it first."
exit 1
fi
tar zcvf $bu $SYSTEM_IMAGE
}
funicular_restore() {
if [ -e $SYSTEM_IMAGE ]; then
echo "$SYSTEM_IMAGE already exists! Delete it first."
exit 1
fi
bu=`basename $SYSTEM_IMAGE`
bu="$bu-backup.tar.gz"
if [ "X$1" != X ]; then
bu=$1
fi
tar zxvf $bu
}
########
# MAIN #
########
CMD=$1
shift
case $CMD in
init|install|setup|start|builddist|distboot|backup|restore)
funicular_$CMD $*
;;
*)
cat <<EOF
Usage: funicular <command>
where <command> is one of:
init
install
setup
start
builddist
distboot
backup
restore
EOF
exit 1
esac