diff --git a/src/ini_val.sh b/src/ini_val.sh new file mode 100755 index 0000000..0f7b2ed --- /dev/null +++ b/src/ini_val.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +function ini_val() { + local file="${1}" + local sectionkey="${2}" + local val="${3}" + local delim=" = " + local section="" + local key="" + + # Split on . for section. However, section is optional + read section key <<<$(IFS="."; echo ${sectionkey}) + if [ -z "${key}" ]; then + key="${section}" + section="" + fi + + local current=$(awk -F"${delim}" "/^${key}${delim}/ {for (i=2; i> "${file}" + else + # add to section + sed "/\[${section}\]/a ${key}${delim}${val}" -i "${file}" + fi + else + # replace existing + sed "/^${key}${delim}/s/${delim}.*/${delim}${val}/" -i "${file}" + fi + fi +} diff --git a/src/megamount.sh b/src/megamount.sh new file mode 100644 index 0000000..7797035 --- /dev/null +++ b/src/megamount.sh @@ -0,0 +1,32 @@ +__dir=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd) + +source "${__dir}/parse_url.sh" +function megamount () { + local url="${1}" + local target="${2}" + + proto=$(parse_url "${url}" "proto") + user=$(parse_url "${url}" "user") + pass=$(parse_url "${url}" "pass") + host=$(parse_url "${url}" "host") + port=$(parse_url "${url}" "port") + path=$(parse_url "${url}" "path") + + umount -lf "${target}" || true + mkdir -p "${target}" + if [ "${proto}" = "smb://" ]; then + mount -t cifs --verbose -o "username=${user},password=${pass},hard" "//${host}/${path}" "${target}" + elif [ "${proto}" = "afp://" ]; then + # start syslog-ng + # afpfsd || echo "Unable to run afpfsd. Does /dev/log exist?" && exit 1 + mount_afp "${url}" "${target}" + elif [ "${proto}" = "nfs://" ]; then + mount -t nfs --verbose -o "vers=3,nolock,soft,intr,rsize=32768,wsize=32768" ${host}:/${path} ${target} + else + echo "ERR: Unknown protocol ${proto}" + exit 1 + fi + + # chmod 777 "${target}" + ls -al "${target}/" +} diff --git a/src/parse_url.sh b/src/parse_url.sh new file mode 100644 index 0000000..4cd2b9e --- /dev/null +++ b/src/parse_url.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +function parse_url() { + # Based on http://stackoverflow.com/a/6174447/151666 + local parse="${1}" + local need="${2}" + + local proto="$(echo $parse | grep :// | sed -e's,^\(.*://\).*,\1,g')" + local url="$(echo ${parse/$proto/})" + local userpass="$(echo $url | grep @ | cut -d@ -f1)" + local user="$(echo $userpass | grep : | cut -d: -f1)" + local pass="$(echo $userpass | grep : | cut -d: -f2)" + local hostport="$(echo ${url/$userpass@/} | cut -d/ -f1)" + local host="$(echo $hostport | grep : | cut -d: -f1)" + local port="$(echo $hostport | grep : | cut -d: -f2)" + local path="$(echo $url | grep / | cut -d/ -f2-)" + + [ -z "${user}" ] && user="${userpass}" + [ -z "${host}" ] && host="${hostport}" + if [ -z "${port}" ]; then + [ "${proto}" = "http://" ] && port="80" + [ "${proto}" = "https://" ] && port="443" + [ "${proto}" = "mysql://" ] && port="3306" + [ "${proto}" = "redis://" ] && port="6379" + fi + + if [ -n "${need}" ]; then + echo ${!need} + else + echo "" + echo " Use second argument to return just 1 variable." + echo " parse_url() demo: " + echo "" + echo " proto: ${proto}" + echo " user: ${user}" + echo " pass: ${pass}" + echo " host: ${host}" + echo " port: ${port}" + echo " path: ${path}" + echo "" + fi +}