#!/bin/sh #--------------------------------------------------------------------------- # /var/install/bin/check-folder check and create a folder # # Creation: 2006-10-17 hb # Last Update: $Id$ # # Copyright (c) 2006-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #--------------------------------------------------------------------------- #--------------------------------------------------------------------------- # usage # check-folder # -f, --setfolder /x/y/z # msg_do_now # # Example: check-folder -f /tmp/meinfolder/achtung "EISFAX_USER_${idx}_COPY_TO_PATH" # #--------------------------------------------------------------------------- # include eislib . /var/install/include/eislib # set ask ask=/var/install/bin/ask #--------------------------------------------------------------------------- # check folder #--------------------------------------------------------------------------- check_folder() { create_folder="${1}" msg_do_now="${2}" # check for folder exists if [ -d "${create_folder}" ] then # when exists exit 0 else # if not exists, ask for create echo mecho --info "${msg_do_now}" mecho --warn "Folder '${create_folder}' is missing." echo echo "OK, lets go to create it now," if ${ask} "I ask you for the rights, OK?" 'y' then # when yes ask_for_folder else # when no echo mecho --info "${msg_do_now}" mecho --warn "OK, your answer is no, solve that first." echo anykey exit 1 fi fi } # --------------------------------------------------------------------------- # ask for rights to create a folder # --------------------------------------------------------------------------- ask_for_folder() { user='' fchown='' mod='' fchmod='' cat < the name for the owner users -> the name for the user group ------------------------------------ EOF # if user empty while [ -z "${user}" ] do # ask for user and group to create echo user_group=$(${ask} " Enter user:group" 'nobody:nogroup' "+") if ! echo "${user_group}" | grep -q ":" then # separator ':' is missing mecho --warn " Separator ':' is missing." else # go on # check for user is given before :xxx f_user="$(echo "${user_group}" | cut -d":" -f1)" if [ -z "${f_user}" ] then # is no user given; return mecho --warn " No user are given." else # go on # check user exists in etc/passwd if ! getent passwd ${f_user} >/dev/null then # user is not a system user; return mecho --warn " User '${f_user}' is not a working user on your system." else # go on # check for group is given after xxx: f_group="$(echo "${user_group}" | cut -d":" -f2)" if [ -z "${f_group}" ] then # is no group given; return mecho --warn " No group are given." else # go on # check group exists in etc/group if ! getent group ${f_group} >/dev/null then # group is not given; return mecho --warn " Group '${f_group}' is not a working group on your system." else # go on # show the value if ${ask} " Is that OK? '${user_group}'" 'y' then # if OK then done, else return user=ok fi # end if ${group} in /etc/group fi # end if -z ${fgroup} fi # end if ${fuser} in etc/passwd fi # end if -z ${fuser} fi # end check for separator ":" fi # end while [ -z "${user}" ] done cat < no rights 1 -> x-execute 2 -> w-write 4 -> r-read first number '[7]' rights for 'owner' second number '[5]' rights for 'group' third number '[5]' rights for 'other' ------------------------------------------- EOF while [ -z "${mod}" ] do # ask for dir right echo fchmod=$(${ask} "Enter rights" '755' "700-777" ) if ${ask} "Is that OK? '${fchmod}'" 'y' then # if OK then done, else return mod=ok fi done # show the value before create nad ask for OK echo mecho --info " ${msg_do_now}" mecho "I create this folder '${create_folder}'" mecho "for user:group '${user_group}'" mecho "with rights '${fchmod}'" echo if ${ask} "Alright now?" 'y' then # create it now mkdir -p ${create_folder} chown ${user_group} ${create_folder} chmod ${fchmod} ${create_folder} case ${?} in 0) mecho " Folder '${create_folder}' now exists." exit 0 ;; *) echo mecho --info " ${msg_do_now}" mecho --warn " An unexpected error occured." mecho --warn " Please solve that first." echo anykey exit 1 ;; esac else echo mecho --warn " It was not OK." if ${ask} " Shall we to the same?" 'y' then # if no, return mecho " OK, we do the same" ask_for_folder else exit 1 fi fi } # --------------------------------------------------------------------------- # main # --------------------------------------------------------------------------- folder='' msg='' if [ ${#} -lt 1 ] then cat <