#!/bin/sh
#
# @file usbsetup.sh
# @brief USB disk setup script for Armadillo 240
# @date $Date$
# @author Noriaki Ando <n-ando@aist.go.jp>
#         Harumi Miyamoto
#
# Copyright (C) 2008
#     Task-intelligence Research Group,
#     Intelligent Systems Research Institute,
#     National Institute of
#         Advanced Industrial Science and Technology (AIST), Japan
#     All rights reserved.
#
# $Id$
#

#
# @if jp
#
# このシェルスクリプトは Almadillo240 で RTコンポーネントを動かすのに必
# 要なファイルやライブラリ等を適当なディレクトリ構造でUSBメモリにコピー
# します。OpenRTM-aist Webページで紹介されているRTコンポーネント自動起
# 動用 romfs (romイメージ) を Armadillo に書き込むことにより、このUSBメ
# モリ上にコピーされたRTコンポーネントを自動で起動することができるよう
# になります。
#
# 使用方法
#
# 引数なしで起動すると、ATDEのデフォルトのUSBディスクマウントディレクト
# リである /media/usbdisk に適当なディレクトリを作成しファイルをコピー
# します。
#
# それ以外の場所にディレクトリ作成およびファイルのコピーを行いたい場合
# は、引数にディレクトリを指定してください。ディレクトリはあらかじめ作
# 成する必要があります。
#
# @else
#
# This shell script will create appropriate directory tree and will
# copy files to run RT-Components on Armadillo240. RTCs that are
# copied to a usb disk by this shell script are automatically launched
# on the romfs (rom image) that is introduced on OpenRTM web page.
#
# Usage
#
# Without any arguments, this script will create directories and will
# copy files into the usb disk mounted on the ATDE's default mount
# point /media/usbdisk.
#
# In order to set up directories and files on any other location,
# please give the target directory as an argument.  The directory has
# to be created in advance.
#
# @endif
#

usbdisk="/media/usbdisk"
usbdisk="/tmp/usbdisk"
srclibdir="/usr/arm-linux-gnu/lib/" 
#srclibdir="/usr/lib/" 
libs="libomniDynamic4.so.0 libomniORB4.so.0 libomnithread.so.3 libRTC-0.4.2.so.0 librt.so.1 libACE.so.5.4.7"

#------------------------------------------------------------
# usage
#------------------------------------------------------------
usage()
{
    cat <<EOF

usage: $0 [dest_dir=$usbdisk]

EOF
}

#------------------------------------------------------------
# boot.sh file
#------------------------------------------------------------
boot_sh()
{
    cat <<EOF
#!/bin/sh
if test -e /etc/default/openrtm.conf ; then
	. /etc/default/openrtm.conf
else
	. /etc/default/slideshow.conf
fi
#echo \$USBDIR
set -- component/*
for com do
	export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$USBDIR/lib
	\$USBDIR/\$com -f \$USBDIR/rtc.conf
done
EOF
}

#------------------------------------------------------------
# rtc.conf file
#------------------------------------------------------------
rtc_conf() {
    cat <<EOF
#
# @file rtc.conf
# @brief rtc.conf for Armadillo
#
# Set your CORBA NameServer's IP address in XXX.XXX.XXX.XXX
corba.nameservers: XXX.XXX.XXX.XXX
naming.formats: %h.host_cxt/%n.rtc
EOF
}

#------------------------------------------------------------
# message for newly created rtc.conf
#------------------------------------------------------------
rtcconf_msg()
{
    echo "--------------------------------------------------------------------"
    echo " New rtc.conf was generated."
    echo ""
    echo " NOTICE: Please modify the entry of corba.nameservers of rtc.conf"
    echo "         to the address of CORBA NameServer currently used."
    echo "--------------------------------------------------------------------"
}

#------------------------------------------------------------
# message for newly created component dir
#------------------------------------------------------------
compdir_msg()
{
    echo "--------------------------------------------------------------------"
    echo " A component directory was created."
    echo ""
    echo " NOTICE: Please copy your RTCs for Armadillo to the directory."
    echo "--------------------------------------------------------------------"
}

#------------------------------------------------------------
# fexist _file_name_
#
# return false if file does not exist
#------------------------------------------------------------
fexist()
{
    fname=$1
    if test -f $fname; then
	echo -n "overwrite? :" $fname "(y/N?) "
	ans="N"
	read ans
	if test "x$ans" = "xY" -o "x$ans" = "xy" ; then
	    return 1
	else
	    return 0
	fi
    fi
    return 1
}

#------------------------------------------------------------
# copy _src_file_ _dest_file_
#
# file copy function with overwrite checking 
#------------------------------------------------------------
copy()
{
    src=$1
    dst=$2
    if test ! -f $src ; then
	echo "Warning: file not found, " $src
	return
    fi

    if fexist $dst; then
	echo "skipped."
    else
	cp $src $dst
	echo "copied:" $l
    fi
}

#------------------------------------------------------------
# lib_copy
#
# copying libraries in $libs from $srclibdir to $destdir/lib
#------------------------------------------------------------
lib_copy()
{
    dstlibdir=$dstdir/lib
    echo "Copying libraries..."
    for l in $libs ;
    do
	copy $srclibdir$l $dstdir/lib/$l
    done
}

#------------------------------------------------------------
# checking args
#------------------------------------------------------------
arg_check()
{
    if test $# -gt 2 ; then
	usage 
	exit 1 
    fi
    
    if test $# = 1 ; then 
	dstdir=$1
	if test ! -d $dstdir ; then
	    echo "error: No such directory:" $dstdir
	    exit 1
	fi
    fi

    if test $# = 0 ; then
	if test -d $usbdisk ; then
	    dstdir=$usbdisk
	else
	    echo "no usbdisk or target directory."
	    usage
	    exit 1
	fi
    fi
}

#---------------------------------------------------------------------
# main
#---------------------------------------------------------------------
arg_check $*

echo "RTCs bootable usb disk for Armadillo will be setup on" $dstdir
echo ""
echo "ls -l $dstdir"
ls -l $dstdir
echo ""
echo -n "continue? (Y/n)"
read ans
if test "x$ans" = "xn" -o "x$ans" = "xN" ; then
    echo "abort"
    exit 0
fi

new_conf="no"
new_cdir="no"

# create boot.sh
if ! fexist $dstdir/boot.sh ; then
    rm $dstdir/boot.sh
    boot_sh > ${dstdir}/boot.sh
fi

# create rtc.conf
if ! fexist $dstdir/rtc.conf ; then
    rtc_conf > $dstdir/rtc.conf
    echo "new rtc.conf created"
    new_conf="yes"
fi

# create component dir
if test ! -d $dstdir/component ; then
    mkdir $dstdir/component
    new_cdir="yes"
fi

# create lib dir and copy libs
if test ! -d $dstdir/lib ; then
    mkdir $dstdir/lib
fi
lib_copy

# message
test "x$new_cdir" = "xyes" && compdir_msg
test "x$new_conf" = "xyes" && rtcconf_msg
echo "done"
