#!/bin/sh
#
#                   IAUNPACK  -  Script to unpack a .ia file
#
# This script runs in either temporary mode (only make the files for the
# duration of the script and then remove them) or keep mode.
#
# The command line arguments get parsed with the mode, by default, set to
# be temporary mode.
#
# In either Mode
# --------------
# Check to ensure that the .ia file exists
# The .ia file is check to ensure that it exists and that it is not a directory
# 
# In Temporary Mode
# -----------------
# Build the temporary directory name: /tmp/iatmp[.#] or <directory>/iatmp[.#].
# If .../iatmp exists then a simple while loop will find the first number, n,
# such that .../iatmp.n does not exist.
# This directory is created
# The specified .ia file is untar'ed to the temporary directory we created
# iaplay is called with "=DEFAULT_GEOMETRY" which is the overrideable geometry
# followed by the user's arguments (no script file name -- that means the
# user can specify a script file name or if not then the default "script.
# iascript" will be used)
# The directory that we created and all of its contents are removed. (rm -rf)
#
# In Keep Mode
# ------------
# Verify that the destination directory (by default ".") is actually a 
# directory.
# Untar the file to the destination directory
# (done).
#
# AUTHOR:  Chris Palmer
# DATE:    93-06-03

# Set up all our default values

default_tmp_dir=/tmp
temp_mode=1
dir=.
got_dir=0
verbose=0

# Set default options for iaplay here   -  CRP 94-06-24
iaplay_opts=
DEFAULT_GEOMETRY="+0+0"

# Remember where the user started from  -  CRP 94-06-24
user_cwd=`pwd`

USAGE="iaunpack: [-k] [-v] [-p <directory] <packed script> [options to iaplay]"
VERSION="iaunpack:  Version 0.2     (c) 1994 Government of Canada        94-08-15"

# Parse the options setting :
# temp_mode = 1 ==> normal mode
# temp_mode = 0 ==> keep mode (just unpack files)
# dir       = . ==> No directory specified
# $1 is the source file name, $2-$10 are the options to iaplay

while getopts kvp: c
do
    case $c in
        k)     temp_mode=0;;
        p)     dir=$OPTARG
               got_dir=1;;
        v)     verbose=1;;
        \?)    echo $USAGE
               exit 1;;
    esac
done

# remove all the options which
shift `expr $OPTIND - 1`

# Verify that the user specified a file and that we can read that file

if test -z "$1"
then
    echo $USAGE
    echo $VERSION
    exit 2
fi

echo $VERSION

if test ! -r "$1" -o -d "$1"
then
    echo "iaunpack: Couldn't access - $1"
    exit 3
fi

# Now let's try and figure out where we are going to be putting these files

if test $temp_mode -eq 1
then
    if test $got_dir -eq 0
    then
        # They didn't give us a directory, use the default temporary dir.
        dir=$default_tmp_dir
    fi
    
    # From whatever directory start with iatmp and try that.
    dir=$dir/iatmp
    i=0;
    prefix=$dir
    
    while test -r "$dir"
    do
        # dir exists, go back to the prefix and add the next number on to it
        i=`expr $i + 1`
        dir=$prefix.$i
    done
    
    if test $verbose -eq 1
    then
        echo "iaunpack: Making directory : $dir"
    fi
    
    mkdir $dir
    
    trap "cd $user_cwd;rm -rf $dir; exit 5" 1 2 3 4 6 7 8 9 10 11 12 13 14 15 2> /dev/null

else
    if test ! -d $dir
    then
        echo "iaunpack: Error keeping files in $dir (not a directory!)"
        exit 4
    fi
fi

src=$1

if test $verbose -eq 1
then
    echo "iaunpack: Unpacking $src --> $dir"
fi

# We have the interesting problem of not knowing where src is located
# (it could be an absolute path or a relative path) and what we really want
# to do is (cd $dir; tar -xf $src) -- do it the more inconvenient way

cat $src | (cd $dir; if tar -xf - 2> .tmp.tar ; then rm .tmp.tar ; fi)

if test -s $dir/.tmp.tar
then
    echo "iaunpack: Error(s) running tar $src => $dir/*"
    cat $dir/.tmp.tar
    wc -l $dir/.tmp.tar
    
    if test $temp_mode -eq 1
    then
        if test verbose -eq 1
        then
            echo "iaunpack: Removing directory $dir"
        fi

	cd $user_cwd
        rm -rf $dir
    else
        rm  $dir/.tmp.tar
    fi
    
    exit 3
fi

if test $temp_mode -eq 1
then
    # Rotate off the command name which we no longer need to get one extra
    # possible option on to the command line
    shift
    
    if test $verbose -eq 1
    then
        echo "iaunpack: Running Illustrated Audio Player"
        echo "iaunpack: iaplay =$DEFAULT_GEOMETRY $iaplay_opts $1 $2 $3 $4 $5 $6 $7 $8 $9"
    fi
    
    cd $dir
    iaplay =$DEFAULT_GEOMETRY $iaplay_opts $1 $2 $3 $4 $5 $6 $7 $8 $9

    if test $verbose -eq 1
    then
        echo "iaunpack: Cleaning up $dir"
    fi
    cd $user_cwd
    rm -rf $dir
fi
