#!/system/bin/sh
#
# add launch of specified script in DIRECTORY
#
# malez @2010
#
# 20100323 fix for args
#

FS=/data
DIRECTORY=${FS}/run
SHELL="#!/system/bin/sh"

usage(){
        echo "Usage : $0 ALIAS command"
	echo "FILE is an absolute path to the script to lauch"
	echo "ALIAS is the name script will be called as in $DIRECTORY"
	echo "permit to control order of execution"
        exit 1
}

error(){

        echo "Error : $0"
        exit 1
}

if [ $# -lt 2 ]; then
        usage
fi

#if [ $(echo $1 | cut -b 1) != "/" ]; then
#   echo "Script name must be given in absolute path"
#fi

# mouting FS read-write
#mount -o remount,rw $RUN_FS || error "Unable to remount $RUN_FS rw"

#creating target directory
if [ ! -d $DIRECTORY ]; then
   busybox mkdir $DIRECTORY
fi

#if [ a$2 != a ]; then
ALIAS=$1
shift
#fi
FILE=${DIRECTORY}/$ALIAS

#create script file
echo "$SHELL" > $FILE

#while (( "$#" )); do
for arg in "$@" ; do 

	echo -n "$arg " >> $FILE
	#echo -n "$arg" 
	shift

done
echo "" >> $FILE
chmod u+x $FILE


#linking
#ln -s $1 $RUN_DIRECTORY/$ALIAS
#echo "$1
#RET=$?

#mount -o remount,ro $RUN_FS || error "Unable to remount $RU_FS ro"

exit 0
