|
#!/usr/bin/env bash |
|
# @(#) sakuten management utility tools/libraries |
|
# |
|
# @(#) copyright (c) 2018 Cj-bc |
|
# @(#) This software is released under MIT License |
|
# @(#) version 0.1 |
|
|
|
|
|
function sakutenutl.help { |
|
cat <<-EOF |
|
sakuten management utility tools |
|
available subcommands: |
|
application -- making... |
|
config -- making... |
|
classroom -- making... |
|
lottery -- making... |
|
user -- making... |
|
help -- making... |
|
available independent commands(for direct use): |
|
get_user_info -- get user infomation from Public_id |
|
search_user_application -- saerch application for given Public_id from given dump file |
|
decode_public_id -- decode 4-letter ID to numeric ID |
|
EOF |
|
|
|
} |
|
|
|
function sakutenutl.error { |
|
case $1 in |
|
'EX_OK') echo 0;; |
|
'EX_USAGE') echo 64;; |
|
'EX_DATAERR') echo 65;; |
|
'EX_NOINPUT') echo 66;; |
|
'EX_NOUSER') echo 67;; |
|
'EX_NOHOST') echo 68;; |
|
'EX_UNAVAILABLE') echo 69;; |
|
'EX_SOFTWARE') echo 70;; |
|
'EX_OSERR') echo 71;; |
|
'EX_OSFILE') echo 72;; |
|
'EX_CANTCREAT') echo 73;; |
|
'EX_IOERR') echo 74;; |
|
'EX_TEMPFAIL') echo 75;; |
|
'EX_PROTOCOL') echo 76;; |
|
'EX_NOPERM') echo 77;; |
|
'EX_CONFIG') echo 78;; |
|
'SAKUTENUTL.EX_NOTFOUND') echo 44;; |
|
* ) echo 255;; |
|
esac |
|
} |
|
|
|
# decode 4-letter public_id to numeric id |
|
# those codes are written in python right now, |
|
# but will be replaced with shellscript |
|
# @param <str public_id> |
|
# @return EX_OK(0) <int decoded_public_id> |
|
# @return EX_USAGE(64) usage |
|
function sakutenutl.decode_public_id { |
|
if [ -p /dev/stdin ]; then |
|
target_public_id=$(cat -) |
|
[ "$target_public_id" = "" ] && echo "not enough args.\nusage:\n$ decode_public_id <target_public_id>" && return $(sakutenutl.error EX_USAGE) |
|
else |
|
[ $# -lt 1 ] && echo "not enough args.\nusage:\n$ decode_public_id <target_public_id>" && return $(sakutenutl.error EX_USAGE) |
|
target_public_id=$1 |
|
fi |
|
[ $(echo -n $target_public_id | wc -m) -ne 4 ] && echo "The encoded public_id should be 4-letter word" && return $(sakutenutl.error EX_USAGE) |
|
target_public_id=${target_public_id^^} |
|
|
|
python <<-EOF |
|
import sys |
|
|
|
target = "${target_public_id}" |
|
encoder = "ACDEFGHJKLMNPRTWXY" |
|
|
|
|
|
def decode_public_id(str_id): |
|
""" |
|
make numeric ID from 4-letter ID |
|
Args: |
|
str_id (str): ID consisting of a number and 3 alphabets |
|
Return: |
|
num_id (int): numeric ID |
|
""" |
|
def alpha2num(c): |
|
return encoder.find(c) |
|
|
|
def num2num(c): |
|
return 5 if c == '9' else int(c) - 3 |
|
|
|
alphas = [alpha2num(c) for c in str_id[1:]] |
|
alphas.insert(0, num2num(str_id[0])) |
|
return sum(alphas[i] * 18**(3-i) for i in range(4)) |
|
|
|
|
|
print(decode_public_id(target)) |
|
EOF |
|
|
|
} |
|
|
|
|
|
# get user infomation from DB dump |
|
# @param <str public_id> <str dump_dir> |
|
# @stdin <int decoded_public_id> |
|
# @return EX_OK(0) <int user_id> <int public_id> <str secret_id> <str authority> <int win_count> <int lose_count> <str kind> <data first_access> |
|
# @return EX_NOUSER(67) error_message |
|
# @return EX_USAGE(64) usage |
|
function sakutenutl.get_user_info { |
|
[ $# -lt 2 ] && echo "not enough args.\nusage:\n$ get_user_info <target_public_id> <target_dump_dir>" && return $(sakutenutl.error EX_USAGE) |
|
local target=$1 |
|
local dir=$2 |
|
local id public_id secret_id authority win_count lose_count kind first_access |
|
local found=false |
|
decoded_id=$(decode_public_id ${target}) |
|
while read id public_id secret_id authority win_count lose_count kind first_access; do |
|
[ "${public_id}" = "$decoded_id" ] && echo "${id} ${public_id} ${secret_id} ${authority} ${win_count} ${lose_count} ${kind} ${first_access}" && found=true |
|
done < <(cat ${dir}/user.txt | grep ${decoded_id}) |
|
|
|
if $found; then |
|
echo 'done.' |
|
return $(sakutenutl.error EX_OK) |
|
else |
|
echo "user ${target}: not found" |
|
return $(sakutenutl.error SAKUTENUTL.EX_NOTFOUND) |
|
fi |
|
} |
|
|
|
|
|
# search user application |
|
# @param <str public_id> <str dump_dir> |
|
# @return EX_OK(0) <int appplication_id> <int lottery_id> <int user_id> <str status> <bool is_rep> |
|
# @return EX_USAGE(64) usage |
|
# @return EX_NOTFOUND(44) given user has no application |
|
function sakutenutl.search_user_application { |
|
[ $# -lt 2 ] && echo "not enough args.\nusage:\n$ search_user_application <target_dump_dir> <target_public_id> ..." && return $(sakutenutl.error EX_USAGE) |
|
local dir=$1 |
|
shift |
|
local -a target_public_ids=($@) |
|
local found=false |
|
for target_public_id in ${target_public_ids[@]}; do |
|
local target_user_id public_id |
|
read target_user_id public_id _ < <(get_user_info $target_public_id $dir) |
|
|
|
echo "searching applications for ${target_public_id}..." |
|
|
|
local application_id lottery_id user_id status is_rep |
|
while read application_id lottery_id user_id status is_rep |
|
do |
|
[ "${user_id}" = "$target_user_id" ] && echo "$application_id $lottery_id $user_id $status $is_rep" && found=true |
|
done < <(cat ${dir}/application.txt | grep $target_user_id) |
|
if $found; then |
|
echo "done." |
|
else |
|
echo "application for public_id: ${public_id} not found" |
|
fi |
|
done |
|
[ $found ] && return $(sakutenutl.error EX_OK) || return $(sakutenutl.error SAKUTENUTL.EX_NOTFOUND) |
|
} |
|
|
|
|
|
# get lottery info from $SAKUTEN_DUMP_DIR/ |
|
# @param <int lottery._id> |
|
# @return <string |
|
function sakutenutl.lottery.get { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl.config.set { |
|
: |
|
} |
|
|
|
|
|
# interface to contents related to lotteries |
|
function sakutenutl.lottery { |
|
: |
|
} |
|
|
|
|
|
# interface to contents related to applications |
|
function sakutenutl.application { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl.user { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl.classroom { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl.utl { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl.config { |
|
: |
|
} |
|
|
|
|
|
function sakutenutl { |
|
[ $# -eq 0 ] || [ "$1" = "help" ] && sakutenutl.help && return $(sakutenutl.error EX_USAGE) |
|
fi |
|
case $1 in |
|
'config' ) shift; sakutenutl.config $@;; |
|
'user') shift; sakutenutl.user $@;; |
|
'application' ) shift; sakutenutl.application $@;; |
|
'lottery' ) shift; sakutenutl.lottery $@;; |
|
'classroom' ) shift; sakutenutl.classroom $@;; |
|
'util' ) shift; sakutenutl.utl $@;; |
|
* ) shift; sakutenutl.help && return $(sakutenutl.error EX_USAGE);; |
|
esac |
|
|
|
return $(sakutenutl.error EX_OK) |
|
} |