diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/bin/.juju.swp b/bin/.juju.swp index f0bb305..37af76d 100644 Binary files a/bin/.juju.swp and b/bin/.juju.swp differ diff --git a/bin/juju b/bin/juju index 66c8077..13c6489 100755 --- a/bin/juju +++ b/bin/juju @@ -1,63 +1,93 @@ #!/usr/bin/env bash +# +# This file is part of JuJu: The universal GNU/Linux package manager +# +# Copyright (c) 2012-2014 Filippo Squillace +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, 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 . +# -JUJU_HOME=$1 -[ -n $JUJU_HOME ] && JUJU_HOME=~/.juju -JUJU_REPO=https://bitbucket.org/fsquillace/juju-repo/raw/master -JUJU_BIN=$(dirname "$0") -ORIGIN_WD=$(pwd) +NAME='juju' +VERSION='1.0' +source "$(dirname $0)/../lib/core.sh" -function setup_juju(){ - trap - QUIT EXIT ABRT KILL TERM INT - trap "rm -rf ${maindir}; die \"Error occurred when installing $pkggrp\"" EXIT QUIT ABRT KILL TERM INT +################################### +### General functions ### +################################### - maindir=$(TMPDIR=/tmp mktemp -d -t juju.XXXXXXXXXX) - builtin cd ${maindir} +usage() { + echo -e "JuJu: The portable GNU/Linux distribution" + echo -e "Usage: $NAME [options]" + echo -e "Operations:" + echo -e "-i, --setup-from-file Setup the JuJu image ${JUJU_HOME}" + echo -e "-h, --help Show this help message" + echo -e "-v, --version Show the $NAME version" +} - info "Downloading JuJu..." - imagefile=juju-$(uname -m).tar.gz - wget ${JUJU_REPO}/$imagefile - tar -zxpf $imagefile - - info "Installing JuJu..." - mkdir -p $JUJU_HOME - cp -f -a ${maindir}/juju/* $JUJU_HOME - info "JuJu installed successfully" - - builtin cd $ORIGIN_WD - trap - QUIT EXIT ABRT KILL TERM INT +version() { + echo -e "JuJu ($VERSION): The portable GNU/Linux distribution" + echo -e "Copyright (c) 2012-2014 Filippo Squillace " + echo -e "Homepage: http://github.com/fsquillace/juju" } -function run_juju(){ - ${JUJU_BIN}/arch-chroot $JUJU_HOME -} +################################### +### MAIN PROGRAM ### +################################### +TEMP=`getopt -o ihv --long setup-from-file,help,version -n 'juju' -- "$@"` -echoerr() { echo "$@" 1>&2; } -function die(){ -# $@: msg (mandatory) - str: Message to print - error $@ - exit 1 -} -function error(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;31m$@\033[0m" -} -function warn(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;33m$@\033[0m" -} -function info(){ -# $@: msg (mandatory) - str: Message to print - echo -e "\033[1;37m$@\033[0m" -} +if [ $? != 0 ] ; then error "Error on parsing the command line. Try juju -h." ; exit ; fi -if [ ! "$(ls -A $JUJU_HOME)" ] +# Note the quotes around `$TEMP': they are essential! +eval set -- "$TEMP" + +OPT_SETUP_FROM_FILE=false +OPT_HELP=false +OPT_VERSION=false +while true ; do + case "$1" in + -i|--setup-from-file) OPT_SETUP_FROM_FILE=true ; shift ;; + -h|--help) OPT_HELP=true ; shift ;; + -v|--version) OPT_VERSION=true ; shift ;; + --) shift ; break ;; + *) error "Internal error!" ; exit 1 ;; + esac +done + +$OPT_HELP && usage && exit +$OPT_VERSION && version && exit + +ARGS=() +for arg do + ARGS+=($arg) +done + +############## CHECKING ESSENTIAL COMMANDS #################### +# Check if the essentials commands are installed (wget and tar) +if ! which wget &> /dev/null || ! which tar &> /dev/null then - setup_juju + error "Error: wget and tar commands are essentials for JuJu. Get them first." + exit 1 fi -run_juju - +################ DEFINE ACTION ######################## +if $OPT_SETUP_FROM_FILE; then + setup_from_file_juju ${ARGS[@]} +else + setup_and_run_juju +fi +# vim: set ts=4 sw=4 noet: diff --git a/lib/core.sh b/lib/core.sh new file mode 100644 index 0000000..d52ad03 --- /dev/null +++ b/lib/core.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +# +# This file is part of JuJu: The portable GNU/Linux distribution +# +# Copyright (c) 2012-2014 Filippo Squillace +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, 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 . +# + +# References: +# https://wiki.archlinux.org/index.php/PKGBUILD +# https://wiki.archlinux.org/index.php/Creating_Packages + +################################ IMPORTS ################################# +# Define the variables for the dependency commands bash, wget, tar, which, awk, grep, xz, file +WGET=wget +TAR=tar +source "$(dirname ${BASH_ARGV[0]})/util.sh" + +################################# VARIABLES ############################## +[ -z ${JUJU_HOME} ] && JUJU_HOME=~/.juju +JUJU_REPO=https://bitbucket.org/fsquillace/juju-repo/raw/master +JUJU_BIN=$(dirname "$0") +ORIGIN_WD=$(pwd) + +################################# MAIN FUNCTIONS ############################## + + +function cleanup_build_directory(){ +# $1: maindir (optional) - str: build directory to get rid + local maindir=$1 + builtin cd $ORIGIN_WD + trap - QUIT EXIT ABRT KILL TERM INT + rm -fr "$maindir" +} + + +function prepare_build_directory(){ + trap - QUIT EXIT ABRT KILL TERM INT + trap "rm -rf ${maindir}; die \"Error occurred when installing JuJu\"" EXIT QUIT ABRT KILL TERM INT +} + + +function setup_juju(){ +# Setup the JuJu environment + local maindir=$(TMPDIR=/tmp mktemp -d -t juju.XXXXXXXXXX) + prepare_build_directory + + info "Downloading JuJu..." + builtin cd ${maindir} + local imagefile=juju-$(uname -m).tar.gz + wget ${JUJU_REPO}/${imagefile} + + info "Installing JuJu..." + mkdir -p ${JUJU_HOME} + builtin cd ${JUJU_HOME} + tar -zxpf ${maindir}/${imagefile} + info "JuJu installed successfully" + + cleanup_build_directory ${maindir} +} + + +function setup_from_file_juju(){ +# Setup from file the JuJu environment + if [ "$(ls -A $JUJU_HOME)" ] + then + error "Error: JuJu has been already installed in $JUJU_HOME" + return 1 + fi + + local imagefile=$1 + if [ ! -e ${imagefile} ] + then + die "Error: The JuJu image file ${imagefile} does not exist" + fi + info "Installing JuJu from ${imagefile}..." + mkdir -p ${JUJU_HOME} + builtin cd ${JUJU_HOME} + tar -zxpf ${ORIGIN_WD}/${imagefile} + info "JuJu installed successfully" + + builtin cd $ORIGIN_WD +} + + +function run_juju(){ + ${JUJU_BIN}/arch-chroot $JUJU_HOME +} + + +function setup_and_run_juju(){ +# Setup and run the JuJu environment +# The setup function will be executed only if the +# JuJu envinronment in $JUJU_HOME is not present. + + [ ! "$(ls -A $JUJU_HOME)" ] && setup_juju + run_juju +} + + diff --git a/lib/util.sh b/lib/util.sh new file mode 100644 index 0000000..8388fe1 --- /dev/null +++ b/lib/util.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# This file is part of JuJu: The portable GNU/Linux distribution +# +# Copyright (c) 2012-2014 Filippo Squillace +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU Library General Public License as published +# by the Free Software Foundation; either version 2, 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 . + +echoerr() { echo "$@" 1>&2; } +function die(){ +# $@: msg (mandatory) - str: Message to print + error $@ + exit 1 +} +function error(){ +# $@: msg (mandatory) - str: Message to print + echoerr -e "\033[1;31m$@\033[0m" +} +function warn(){ +# $@: msg (mandatory) - str: Message to print + echoerr -e "\033[1;33m$@\033[0m" +} +function info(){ +# $@: msg (mandatory) - str: Message to print + echo -e "\033[1;37m$@\033[0m" +}