#!/bin/sh
# squeak - main script for invoking Squeak.  See squeak(1) for arguments.
#
# BUG: This script does not handle vm arguments that have spaces in them.
#      Is this solvable in portable shell script?


set -e




msg () {
    if [ "$verbose" = "yes" ]
    then
        echo "$@"
    fi
}



# defaults
mode="install"
vm_options=""
verbose="no"



# defaults from the environment
if [ "$SQUEAK_IMAGE_DIR" = "" ]
then
    image_dir="/usr/share/squeak"
else
    image_dir="$SQUEAK_IMAGE_DIR"
fi
if [ "$SQUEAKVM" = "" ]
then
    squeakvm="squeakvm"
else
    squeakvm="$SQUEAKVM"
fi
if [ "$SQUEAK_IMAGE" = "" ]
then
    image="squeak.image"
else
    image="$SQUEAK_IMAGE"
fi



# check if the first argument looks like an image
if (echo $1 | grep -q '^[^-]')
then
    image=$1
    mode=run
    shift
fi

# parse arguments
while true
do
    if [ "$1" = "" ]
    then
        break
    fi

    case $1 in
        -run)
            mode=run
	    shift
            ;;

        -l|-list)
            mode=list
	    shift
            ;;

        -install)
            mode=install
	    shift
            ;;

        -image)
            shift
            image=$1
            shift
            if [ "$image" = "" ]
            then
                echo "-image requires an argument"
                exit 1
            fi
            ;;

	-v|-verbose)
	    shift
	    verbose=yes
            ;;

	--version)
	    echo "script version 1.0"
	    $squeakvm -version
	    exit
	    ;;

	--help)
	    echo "squeak [ filename ]"
	    echo ""
	    echo "squeak [-install  | -list  | -l  | -run ]  [-image filename]"
	    echo "       [-verbose  | -v ]  [vm options]  [-- image options]"
	    exit 1
	    ;;
         
        --)
            shift
            break
            ;;

         *)
            vm_options="$vm_options $1"
            shift
            ;;
    esac
done
  
# add .image to the end, if necessary 
if (echo $image | grep -qv '\.image$')
then
    image="$image.image"
fi

# compute the name of the changes file
changes=`echo $image | sed -e 's/\.image$/.changes/'`



# help routine to install a file
uncompress_and_install () {
    source=$1
    dest=$2

    if [ -f $source.bz2 ]
    then
        source=$source.bz2 
        decompress=bunzip2
    else if [ -f $source.gz ]
    then
        source=$source.gz
        decompress=gunzip
    else if [ -f $source ]
    then
        decompress=cat
    else
        source=""
    fi
    fi
    fi
    
    if [ "$source" = "" ]
    then
        # quietly do nothing if the source is not there;
        # this is used to support bare images that don't
        # have a changes file
        :
    else
        msg "installing $source as $dest..."
        $decompress < $source > $dest
    fi
}


# install the image if necessary
if [ ! -f $image ]
then
    if [ "$mode" = "run" ]
    then
        echo "The image $image is not present and -run was specified"
        exit 2
    fi

    if [ -f $changes ]
    then
        echo "$image is missing but $changes is present."
        echo "Refusing to continue.  Rename or remove $changes,"
        echo "or specify a different -image."
        exit 2
    fi

    available_images=`(cd $image_dir; ls) | grep '\.image.*$' | sed -e 's/\.gz$//' -e 's/\.bz2$//' -e 's/\.image$//' | sort | uniq`

    if [ "$available_images" = "" ]
    then
        echo "No image is available to install."
        exit 2
    fi

    if [ "$mode" = "install" ]
    then
        # choose an image automatically
        choice=""
        for  img in $available_images
        do
            if [ "$choice" = "" ]
            then
                choice=$img
            fi
            if [ "$img" = "squeak" ]
            then
                choice=$img
            fi
        done
    else
        # let the user choose
        echo "Select an image from the following choices:"
        i=1
        for img in $available_images
        do
            echo "  $i. $img"
            i=`expr $i + 1`
        done
        num_avail=`expr $i - 1`
        echo "Choice: "
        read choice_i

        choice=""
        i=1
        for img in $available_images
        do
            if [ $i = $choice_i ]
            then
                choice=$img
            fi
            i=`expr $i + 1`
        done 
        if [ "$choice" = "" ]
        then
            echo "invalid selection"
            exit 1
        fi
    fi

    # install the chosen image
    uncompress_and_install $image_dir/$choice.image $image
    chmod u+x $image
    uncompress_and_install $image_dir/$choice.changes $changes

    # install all available sources files
    for sourcef in $image_dir/*.sources
    do
        locname=`basename $sourcef`
        if [ ! -f $locname ]
        then
            msg "Linking $locname to $sourcef..."
            ln -s $sourcef $locname
        fi
    done
fi


# in the below command, there should theoretically be a -- before the
# $*, but currently the VM is passing the -- to the image!

msg "Running the VM: $squeakvm $vm_options $image $*"
exec $squeakvm $vm_options $image $*

