#!/bin/sh # # b2w_ctrl.sh written by F.Camel 2004/08/10 # # control tool for bbs2web # ######################################################################################## #prefix="/u/cp/91/9117029/public_html/tmp" prefix="." usage() { echo -e "usage:\tb2w_ctrl [-m | -d] -c class number [target number]" echo -e "\t-m: re-number to target number, swap number and target number" echo -e "\t-d: delete it" echo echo -e "\t-c class : class of file" echo "[NOTE] run this script under data ROOT of bbs2web" echo } if [ $# -le 0 ]; then usage exit 0 fi class= action= sn= tsn= while [ $# -gt 0 ] do if [ $1 = "-c" ]; then shift if [ $# -le 0 ]; then echo "missing class name for -c" usage exit 1 fi class=$1 elif [ $1 = "-m" ]; then if [ "$action" != "" ]; then echo "do not assign two action" usage exit 2 fi action="rename" elif [ $1 = "-d" ]; then if [ "$action" != "" ]; then echo "do not assign two action" usage exit 2 fi action="delete" else if [ "$sn" = "" ]; then sn=$1 elif [ "$tsn" = "" ]; then tsn=$1 else echo "unknown argument: $1" usage exit 3 fi fi shift done # input error checking if [ "$class" = "" ]; then echo "not assign class name" usage exit 4 elif [ "$action" = "" ]; then echo "not assign action (rename or delete)" usage exit 5 elif [ "$action" = "rename" -a "$tsn" = "" ]; then echo "not assign target number" usage exit 5 elif [ "$sn" = "" ]; then echo "not assign serial number" usage exit 6 fi # check if files exist if [ ! -d "$class" ]; then echo "class '$class' not exist" exit 11 elif [ ! -f "$class/$sn.htm" ]; then echo "'$class/$sn.htm' not exist" exit 12 elif [ "$tsn" != "" ]; then if [ ! -f "$class/$tsn.htm" ]; then echo "'$class/$tsn.htm' not exist" exit 12 fi fi # main path="${prefix}/${class}" tmp=".t$$" cp -f "${path}/.db" "${path}/.db.bak" export sn export path success=1 if [ $action = "delete" ]; then cat "${path}/.db" | perl -ne ' (unlink "$ENV{path}/$ENV{sn}.htm" or die "cannot remove file\n") if /^$ENV{sn}/; print if ! /^$ENV{sn}/; ' > $tmp #export -n sn # only bash has "export -n" success=$? elif [ $action = "rename" ]; then export tsn cat "${path}/.db" | perl -F"\t" -ane ' chomp $F[2]; print "$ENV{tsn}\t$F[1]\t$F[2]\n" if /^$ENV{sn}\s+/; print "$ENV{sn}\t$F[1]\t$F[2]\n" if /^$ENV{tsn}\s+/; print "$F[0]\t$F[1]\t$F[2]\n" if ! /^($ENV{sn}\s+|$ENV{tsn}\s+)/; ' | sort -n > $tmp #export -n sn # only bash has "export -n" success=$? if [ $success -eq 0 ]; then mv -f ${path}/$sn.htm ${path}/$sn.htm.bak mv -f ${path}/$tsn.htm ${path}/$sn.htm mv -f ${path}/$sn.htm.bak ${path}/$tsn.htm fi fi if [ $success -eq 0 ]; then mv -f $tmp "${path}/.db" else echo "fail to do $action" rm -f $tmp fi