2 # gast_symlink : create symbolic link to each wrapper script as alias name.
4 # Copyright (C) 2010 Tadashi Koike
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #----------------------------------------------------------------------
22 # (I am weak in English. Please modify English comment more suitable. :T.K)
24 ALIAS_FILE="gast.aliases"
26 # get a directory path (full-path) of this script
28 if [[ "$0" =~ ".+/[^/]+" ]]; then
31 SCRIPT_PATH=${0%${SCRIPT_NAME}}
33 case "${SCRIPT_PATH}" in
34 '.' | '') SCRIPT_PATH="${CWD}" ;;
35 \./*) SCRIPT_PATH="${CWD}/${SCRIPT_PATH#*/}" ;;
36 '~') SCRIPT_PATH="${HOME}" ;;
37 \~/*) SCRIPT_PATH="${HOME}/${SCRIPT_PATH#*/}" ;;
39 *) SCRIPT_PATH="${CWD}/${SCRIPT_PATH}" ;;
42 ### function declaration start ###
43 function create_symlink () {
44 # $1 : symbolic link name
45 # $2 : original file name
46 if [ -z "$1" -a -z "$2" ]; then
47 echo "${SCRIPT_NAME} : illegal file name. (symlink: '$1', original file: '$2')" >&2
54 if [ ! -e "${exist_file}" ]; then
55 echo "${SCRIPT_NAME}: '${exist_file}' does not exists. Could not create symbolic link." >&2
59 if [ -e "${symlink_name}" -a ! -h "${symlink_name}" ]; then
60 echo "${SCRIPT_NAME}: '${symlink_name}' exists as normal file or directory. Could not create symbolic link." >&2
64 if [ -h "${symlink_name}" ]; then
65 symlink_to=$(ls -l ${symlink_name})
66 symlink_to="${symlink_to##* }"
67 if [ "${symlink_to}" != "${exist_file}" ]; then
68 echo "${SCRIPT_NAME}: ${symlink_name} exists for other file '${symlink_name}'" >&2
73 # Create symbolic link
74 ln -sf "${exist_file}" "${symlink_name}"
75 [ $? -ne 0 ] && echo "${SCRIPT_NAME}: error occured. Couldn't create symbolic link '${symlink_name}'" >&2
78 function delete_symlink () {
79 # $1 : symbolic link name
80 # $2 : original file name
82 echo "${SCRIPT_NAME} : illegal file name. (symlink:'$1')" >&2
86 local original_name=$2
89 [ ! -e "${symlink_name}" ] && return
91 # now, at least file exists.
92 if [ ! -h "${symlink_name}" ]; then
93 echo "${SCRIPT_NAME} : '${symlink_name}' exists as normal file or directory. Could not delete it." >&2
97 # now, ${symlink_name} is recognized as a symbolic link
98 symlink_to=$(ls -l ${symlink_name})
99 symlink_to="${symlink_to##* }"
100 if [ -n "${original_name}" -a "${symlink_to}" != "${original_name}" ]; then
101 echo "${SCRIPT_NAME} : stop deleting symbolic link '${symlink_name}'. It linked to '${symlink_to}'." >&2
105 # Delete symbolic link
106 unlink "${symlink_name}"
107 [ $? -ne 0 ] && echo "${SCRIPT_NAME}: Couldn't delete symbolic link '${symlink_name}'" >&2
112 while getopts 'hu' OPTION
117 ?) echo "Usage: ${SCRIPT_NAME} : [-u] [-h] [<script_name> [...]]" >&2
119 echo " -u : delete symbolic link for <script_name>" >&2
120 echo " [ARGUMENT]" >&2
121 echo " <script_name> : gast-family script file. if there is no argument," >&2
122 echo " all gast-family scripts in ${ALIAS_FILE} are treated." >&2
123 [ "${OPTION}" = "h" ] && exit 0
128 shift $(($OPTIND - 1))
130 script_name_list=($@)
133 [ $# -gt 0 ] && argument_check=yes
135 cd "${SCRIPT_PATH}"> /dev/null 2>&1
137 if [ ! -f "${ALIAS_FILE}" ]; then
138 echo "${SCRIPT_NAME}: Couldn't find ${ALIAS_FILE}. exit." >&2
146 [[ "${line}" =~ "^[:blank:]*$" ]] && continue
147 [[ "${line}" =~ "^[:blank:]*#" ]] && continue
148 if [[ "${line}" =~ "^[:blank:]*([^[:blank:]]+)[:blank:]*\:[:blank:]*([^#]+)[:blank:]*(#.*)*$" ]]; then
151 echo "${SCRIPT_NAME}: format error in LINE ${line_no} of ${ALIAS_FILE}."
154 gast_family_script=${BASH_REMATCH[1]} # script name
155 alias_list=${BASH_REMATCH[2]//,/ } # list of alias name
157 # if user specified script name(s) by command argument, treat only it.
158 if [ -n "${argument_check}" ]; then
161 while [ -n "${script_name_list[$cnt]}" ]
163 if [ "${gast_family_script}" = "${script_name_list[$cnt]}" ]; then
169 [ -z "${hit_flag}" ] && continue
172 # Loop for create/delete symbolic link for ${gast_family_script}
173 for alias_name in ${alias_list}
175 if [ -z "${unlink_flag}" ]; then
176 create_symlink "${alias_name}" "${gast_family_script}"
178 delete_symlink "${alias_name}" "${gast_family_script}"
181 finished_list=(${finished_list[@]} ${gast_family_script})
182 done < "${ALIAS_FILE}"
185 if [ -n "${argument_check}" ]; then
187 while [ -n "${script_name_list[$cnt]}" ]
189 for finished in ${finished_list[@]}
191 if [ "${script_name_list[$cnt]}" = "${finished}" ]; then
192 unset script_name_list[$cnt]
198 script_name_list=(${script_name_list[@]})
199 if [ -n "${script_name_list[0]}" ]; then
200 for script_name in ${script_name_list[@]}
202 warning_list="${warning_list}'${script_name}',"
204 warning_list="${warning_list%,}"
205 echo "${SCRIPT_NAME}: there is no entry for ${warning_list}." >&2