#!/bin/bash
# 使いかた
# 1. 次のファイルを同じディレクトリに置く
# sound_list.sh -- このファイル
# sound_list.html.tpl
#
# 2. 次の設定を適宜変更してください
# サウンドファイルが置いてあるディレクトリ
dir=~/dev/www/pri/tech/js/001.sound_list/sound
# サウンドファイルの拡張子 - 複数指定には対応していません
sound_file_type="*.ogg"
# 生成する html ファイルを設置するディレクトリと
# dir に設定したディレクトリが違う場合はその相対パスを設定
# 同じなら空でよい(よく分からない場合もとりあえず空でよい)
path=sound
# 3. sound_list.sh を実行する
# 標準出力に出ます
# ex)
# $ sh sound_list.sh > sound_list.html
#
# 処理全体の流れ
#
# * 指定ディレクトリ下のサウンドファイルを取得する
# * テンプレートファイルを元として
# リスト部分を取得したサウンドファイルリストに差し替える
# * それを出力する
# テンプレートファイル
# この sh ファイルと同じ場所にある前提
tpl=`dirname $0`/sound_list.html.tpl
# サウンドファイルリストを取得する
sound_files=`find $dir -name $sound_file_type | sort`
# find は絶対パスで取れてしまうため
# 指定ディレクトリからの相対パスに直す
# (上の sort のあとにパイプで繋いで1行でやっても可)
sound_files=`for sound_file in $sound_files; do echo ${sound_file/$dir/.\/$path} ; done`
# 取得したサウンドファイルリストを
の形式にする
# これを一時ファイルとして作成
# 一時ファイルの初期化
:>tmp.txt
# テンプレートファイルから 部分の書式を取得
li=`grep "##sound_file##" $tpl`
# の書式に変換したものを一時ファイルに出力していく
# このとき連番もつける
#
# * ファイルパスなど置換後文字に / がある場合は
# sed -e "s|xxxx|yyyy|g" で sed に指定する文字の区切りを | にできます
i=0
for sound_file in $sound_files; do
echo $li | sed -e "s|##i##|$i|g" -e "s|##sound_file##|$sound_file|g" >> tmp.txt
i=$((i+1))
done
# テンプレートファイルを元として
# リスト部分を上で作成した一時ファイルに差し替える
#
# sed -e "/##sound_files##/r tmp.txt" で リスト部を tmp.txt の内容に差し替え
# 差し替え箇所の目印 ##sound_files##, ##sound_file## を削除
cat $tpl | \
sed -e "/##sound_files##/r tmp.txt" \
-e "/##sound_files##/d" \
-e "/##sound_file##/d" \
> tmp_out.txt
# 1つ目のサウンドファイルを