#!/bin/sh
#
# Script to expand single files to the <directory>/default
# for use in the installation scripts or the other way around.
#
# Casper Dik (casper@fwi.uva.nl)
#

if [ x"$1" = x-d ]
then
    dirs=true
    shift
fi
if [ x"$1" = x-f ]
then
    files=true
    shift
fi
if [ $# = 0 ]
then
    echo Usage: "$0 [-d|-f] file ..." 1>&2
    exit 1
fi
status=0
for f
do
    if [ -f $f ]
    then
	if [ -z "$files" ]
	then
	    case "`file $f`" in
	    *ELF*SPARC*) def=sparc;;
	    *ELF*LSB*) def=i386;;
	    *) def=default;;
	    esac
	    echo "converting $f to $f/$def"
	    mkdir $f.d &&
	    mv $f $f.d/$def &&
	    mv $f.d $f
	else
	    echo "$f is already a file"
	fi
    elif [ -d $f ]
    then
	count=`ls -A $f|wc -l`
	if [ $count -ne 1 ]
	then
	    echo ${f}: not just one file. 1>&2
	    status=1
	else
	    def=`ls -A $f`
	    if [ -z "$dirs" ]
	    then
		echo "converting $f/$def to $f"
		mv $f $f.d &&
		mv $f.d/$def $f &&
		rmdir $f.d
	    else
		echo "$f is already a directory"
	    fi
	fi
    else
	echo ${f}: not a file or directory. 1>&2
	status=1
    fi
done
exit $status
