OSDN Git Service

Update version number 0.1.2 --> 0.1.3
[gast/master.git] / gast_symlink
1 #!/bin/bash
2 #   gast_symlink : create symbolic link to each wrapper script as alias name.
3 #
4 #   Copyright (C) 2010 Tadashi Koike
5 #
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.
10 #
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.
15 #
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  
19 #   02110-1301, USA.
20 #
21 #----------------------------------------------------------------------
22 # (I am weak in English. Please modify English comment more suitable. :T.K)
23 #
24 ALIAS_FILE="gast.aliases"
25 CWD=$(pwd)
26 # get a directory path (full-path) of this script
27 SCRIPT_NAME=${0##*/}
28 if [[ "$0" =~ ".+/[^/]+" ]]; then
29     SCRIPT_PATH=${0%/*}
30 else
31     SCRIPT_PATH=${0%${SCRIPT_NAME}}
32 fi
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#*/}" ;;
38     \/*)                :       ;;
39     *)          SCRIPT_PATH="${CWD}/${SCRIPT_PATH}" ;;
40 esac
41
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
48         return
49     fi
50     local symlink_name=$1
51     local exist_file=$2
52     local symlink_to=
53
54     if [ ! -e "${exist_file}" ]; then
55         echo "${SCRIPT_NAME}: '${exist_file}' does not exists. Could not create symbolic link." >&2
56         return
57     fi
58
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
61         return
62     fi
63
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
69             return
70         fi
71     fi
72
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
76 }
77
78 function delete_symlink () {
79     # $1 : symbolic link name
80     # $2 : original file name
81     if [ -z "$1" ]; then
82         echo "${SCRIPT_NAME} : illegal file name. (symlink:'$1')" >&2
83         return
84     fi
85     local symlink_name=$1
86     local original_name=$2
87     local symlink_to=
88
89     [ ! -e "${symlink_name}" ] && return
90
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
94         return
95     fi
96
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
102         return
103     fi
104
105     # Delete symbolic link
106     unlink "${symlink_name}"
107     [ $? -ne 0 ] && echo "${SCRIPT_NAME}: Couldn't delete symbolic link '${symlink_name}'" >&2
108 }
109
110 ### MAIN ###
111 unlink_flag=
112 while getopts 'hu' OPTION
113 do
114     case "${OPTION}" in
115     u)  unlink_flag=1
116         ;;
117     ?)  echo "Usage: ${SCRIPT_NAME} : [-u] [-h] [<script_name> [...]]" >&2
118         echo " [OPTION]" >&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
124         exit 1
125         ;;
126     esac
127 done
128 shift $(($OPTIND - 1))
129
130 script_name_list=($@)
131 finished_list=()
132 argument_check=
133 [ $# -gt 0 ] && argument_check=yes
134
135 cd "${SCRIPT_PATH}"> /dev/null 2>&1
136
137 if [ ! -f "${ALIAS_FILE}" ]; then
138     echo "${SCRIPT_NAME}: Couldn't find ${ALIAS_FILE}. exit." >&2
139     exit 1
140 fi
141
142 line_no=0
143 while read -r line
144 do
145     let line_no+=1
146     [[ "${line}" =~ "^[:blank:]*$" ]] && continue
147     [[ "${line}" =~ "^[:blank:]*#" ]] && continue
148     if [[ "${line}" =~ "^[:blank:]*([^[:blank:]]+)[:blank:]*\:[:blank:]*([^#]+)[:blank:]*(#.*)*$" ]]; then
149         :
150     else
151         echo "${SCRIPT_NAME}: format error in LINE ${line_no} of ${ALIAS_FILE}."
152         continue
153     fi
154     gast_family_script=${BASH_REMATCH[1]}       # script name
155     alias_list=${BASH_REMATCH[2]//,/ }          # list of alias name
156
157     # if user specified script name(s) by command argument, treat only it.
158     if [ -n "${argument_check}" ]; then
159         hit_flag=
160         cnt=0
161         while [ -n "${script_name_list[$cnt]}" ]
162         do
163             if [ "${gast_family_script}" = "${script_name_list[$cnt]}" ]; then
164                 hit_flag=1
165                 break
166             fi
167             let cnt+=1
168         done
169         [ -z "${hit_flag}" ] && continue
170     fi
171
172     # Loop for create/delete symbolic link for ${gast_family_script}
173     for alias_name in ${alias_list}
174     do
175         if [ -z "${unlink_flag}" ]; then
176             create_symlink "${alias_name}" "${gast_family_script}"
177         else
178             delete_symlink "${alias_name}" "${gast_family_script}"
179         fi
180     done
181     finished_list=(${finished_list[@]} ${gast_family_script})
182 done < "${ALIAS_FILE}"
183
184 # check
185 if [ -n "${argument_check}" ]; then
186     cnt=0
187     while [ -n "${script_name_list[$cnt]}" ]
188     do
189         for finished in ${finished_list[@]}
190         do
191             if [ "${script_name_list[$cnt]}" = "${finished}" ]; then
192                 unset script_name_list[$cnt]
193                 break
194             fi
195         done
196         let cnt+=1
197     done
198     script_name_list=(${script_name_list[@]})
199     if [ -n "${script_name_list[0]}" ]; then
200         for script_name in ${script_name_list[@]}
201         do
202             warning_list="${warning_list}'${script_name}',"
203         done
204         warning_list="${warning_list%,}"
205         echo "${SCRIPT_NAME}: there is no entry for ${warning_list}." >&2
206     fi
207 fi