##################### # Draft Version 0.22 # Date: 22.12.20 # cfg.common # - helper functions to control addons # - to be sourced by cfg_asterisk and other addon scripts # - all variables used(*) by this script have a trailing underline, e.g. i_ # *) they may get changed, overwritten, deleted #################################################################### # Changes: # - cp... made parm1_ (cp options) optional # - cprs use -R (-r not supported with old firmware) # - ln... always (re)create links (existing links might point to wrong target) # - new internal function shift_parms_ # - added function lnds2_: like lnds_ but added parameter chroot_dir # (usage: lookup files in /chroot_dir/source_dir during installation # but access files at /source_dir when running in chroot environment) # Suggestions of things to be added: # - further functions potentially required for other installations # - verbose flag with additional output for _fct functions # - other common functions e.g. ramdisk , errorexit etc. #################################################################### #errorexit() #{ # echo -e "$@" # exit 1 #} ######################################################################## # internal function: shift_parms_ # - remove parm1_ and copy # parm1_=parm2_, parm2_=parm3_ ... parm<$i_-1>_=parm<$i_>_ # unset parm<$i_>_ # let i_-=1 # purpose: allow optional parameters ######################################################################## shift_parms_() { if [ $i_ -ge 1 ]; then j_=1 k_=2 while [ $k_ -le $i_ ]; do eval parm${j_}_=`eval echo \\$parm${k_}_` let j_+=1 let k_+=1 done eval unset parm${i_}_ let i_-=1 fi } ######################################################################## # internal function: mkdir_rcrsv_ [ ] # - create all levels of required directory structure provided in # - consider to be relative to if parm is set # - if is omitted, is assumed to be an absolute directory path ######################################################################## mkdir_rcrsv_() { if [ $# -gt 1 ];then create_dir_=$1 parms_=$2 [ -d $create_dir_ ] || errorexit "$funct_: Base directory $create_dir_ does not exist." else create_dir_="" parms_=$1 fi for d_ in `echo $parms_ | sed "s/\// /g"`;do create_dir_=$create_dir_/$d_ if ! [ -d $create_dir_ ];then mkdir $create_dir_ || errorexit "$funct_: No RW File System ($create_dir_)" fi done } ######################################################### # usage: _fct funct_name@parm1[:parm2][:parm3]...[:parm{n}}] ######################################################### _fct() { # Determine function funct_=${1%@*} [ $verbose ] && echo "Executing: _fct $1" i_=0 # Determine parameters for function and store in variables parm1_, parm2_, parm3_, ... parm{n}_ args_=`echo \${1#*@} | sed "s/:/ /g"` for parm_ in $args_;do let i_+=1 eval parm${i_}_="$parm_" done case $funct_ in ######################################################### instdir) # instdir@/var/media/ftp|::varname_install_root_dir:varname_fs_type # Determine if "/var/media/ftp" is provided or assume otherwise. Return install_root_dir and fs-type information stored in variable names provided. # Also confirm that FS is RW and create on determined . Errorexit if it is nor a RW Filesystem. # PARMS: # /var/media/ftp: absolute path to default FBF USB moint-point , where attached and mounted USB-drives are expected ( if usb-device is to be determined under /var/media/ftp/* ) # : absolute directory name, where $addon is to be installed, in case end-user has provided fully qialified directory name for installation. # : $addon package name, e.g. asterisk, dropbear etc. # varname_install_root_dir: name of variable, where fully specified installation directory will be returned in # varname_fs_type: name of variable, where fs-type of fully specified installation directory will be returned in [ $i_ -eq 4 ] || errorexit "$funct_: Insufficient number of parameters" if [ $parm1_ = "/var/media/ftp" -o $parm1_ = "/var/media/ftp/" ];then for path_ in /var/media/ftp/*; do [ -d $path_/$parm2_ ] && break done [ -d $path_ ] || errorexit "$funct_: No attached and mounted usb-device found under \"/var/media/ftp/\"" else [ -d $parm1_ ] || errorexit "$funct_: Installation path provided \"$parm1_\" does not exist!" path_=$parm1_ fi det_mnt_=$path_ while [ $det_mnt_ ];do # If installation path is references by a Symbolinc Link, then determine the realpath if [ -L $det_mnt_ ];then # temporarily use det_mnt_ as buffer variable det_mnt_="`ls -l $det_mnt_`" # set det_mnt_ to realpath of symbolic link ( basically the path the symbolinc link is pointing to) det_mnt_=${det_mnt_##* -> }; fi # Determine FS-Type if mount-point located if mnt_pnt_=`mount | grep "on $det_mnt_ "`;then # Errorexit if Installation path provided is a "read only (RO)" File Sysren echo $mnt_pnt_ | sed "s,^.* \(.*\) \(.*\) \(.*\) \(.*\),\4," | grep -q "ro" && errorexit "$funct_: Installation path \"$path_\" is on a Read Only FS!" # Store FS-Type of mount point located in variable name provided by $parm4_ eval $parm4_=`echo $mnt_pnt_ | sed "s,^.* \(.*\) \(.*\) \(.*\) \(.*\),\3,"` # Store install root dir information in variable name provided by $parm3_ eval $parm3_=$path_ mkdir_rcrsv_ $path_ $parm2_ break; fi # go up one level in directory hierarchy det_mnt_=${det_mnt_%/*} done ;; ######################################################### mkdr) # mkdr@:[:][:]...[:] # Create directory path ... relative to # PARMS: # : absolute path to RW filesystem # : drectory paths to be created relative to [ $i_ -ge 2 ] || errorexit "$funct_: Insufficient number of parameters" let x_=2 while [ $x_ -le $i_ ] do mkdir_rcrsv_ $parm1_ `eval echo \\$parm${x_}_` let x_+=1 done ;; ######################################################### cpnr) # cpnr@[copy_options:]:[:file1][:file2]...[:file{n}] # Copy file1 ... file{n} ( or * if no files specified ) from to . File attributes will be preserved. # PARMS: # : options for cp command with leading "-" # : absolute path to copy from # : absolute target path to copy files to. will be created if not existing. # file{n}: optional file names to be copied. Copy "*" ( all ) assumed if no file name(s) specified unset options_ if [ $i_ -ge 3 -a ! -d "$parm1_" ]; then # assume $parm1_ contains options options_=$parm1_ shift_parms_ fi [ $i_ -ge 2 ] || errorexit "$funct_: Incorrect number ($i_) of parameters into function: $funct_" ! [ -d $parm2_ ] && mkdir_rcrsv_ $parm2_ if [ $i_ -gt 2 ];then let x_=3 while [ $x_ -le $i_ ] do cp $options_ $parm1_/`eval echo \\$parm${x_}_` $parm2_ let x_+=1 done else cp $options_ $parm1_/* $parm2_ fi ;; ######################################################### cprs) # cprs@[copy_options:]: # Copy recursively to . File attributes will be preserved. # PARMS: # : options for cp command with leading "-" # : absolute path to copy from # : absolute target path to copy files to. will be created if not existing. unset options_ if [ $i_ -eq 3 -a ! -d "$parm1_" ]; then # assume $parm1_ contains options options_=$parm1_ shift_parms_ fi [ $i_ -eq 2 ] || errorexit "$funct_: Incorrect number ($i_) of parameters into function: $funct_" [ -d $parm2_ ] || mkdir_rcrsv_ $parm2_ cp -R $options_ $parm1_/* $parm2_ ;; ######################################################### cpdf) # cpdf@[copy_options:]: # Copy *.default from to . ".default" extension is omitted in . Existing files in WILL BE KEPT / NOT BE OVERWRITTEN. # PARMS: # : options for cp command with leading "-" # : absolute path to copy from # : absolute target path to copy files to. will be created if not existing. unset options_ if [ $i_ -eq 3 -a ! -d "$parm1_" ]; then # assume $parm1_ contains options options_=$parm1_ shift_parms_ fi [ $i_ -eq 2 ] || errorexit "$funct_: Incorrect number ($i_) of parameters into function: $funct_" ! [ -d $parm2_ ] && mkdir_rcrsv_ $parm2_ for f_ in $parm1_/*.default; do f_=${f_##*/} if [ -f $parm2_/${f_%.default} ];then echo "not overwriting $parm1_/${f_%.default}" else cp $options_ $parm1_/$f_ $parm2_/${f_%.default} fi done ;; ######################################################### cpnL) # cpnL@[copy_options:]: # Copy * from to . Ignore if file is SYMBOLIC LINK! # PARMS: # : options for cp command with leading "-" # : absolute path to copy from # : absolute target path to copy files to. will be created if not existing. unset options_ if [ $i_ -eq 3 -a ! -d "$parm1_" ]; then # assume $parm1_ contains options options_=$parm1_ shift_parms_ fi [ $i_ -eq 2 ] || errorexit "$funct_: Incorrect number ($i_) of parameters into function: $funct_" ! [ -d $parm2_ ] && mkdir_rcrsv_ $parm2_ for f_ in $parm1_/*;do [ -L $f_ ] || cp $options_ $f_ $parm2_/${f_##*/} done ;; ######################################################### lnsf) # lnsf@<[target_dir/]link_name|:[:]...[:] # Create a link named link_name in current (or target) directory to # OR create one ore more symbolic links to in # (warning: existing directories will be removed and replaced by symbolic links) # PARMS: # : name of symbolic link to be created in current (or target) dir ( $PWD ) # : target path where symbolic links to will be created # : relative (or absolute) directory or file path to be linked in [ $i_ -ge 2 ] || errorexit "$funct_: Incorrect number of parameters into function: $funct_" let x_=2 while [ $x_ -le $i_ ] do softlink_=`eval echo \\$parm${x_}_` # if ! [ -L $softlink_ ]; then [ -d $parm1_/`basename $softlink_` ] && rm -r $parm1_/`basename $softlink_` ln -sf $softlink_ $parm1_ # fi let x_+=1 done ;; ######################################################### lndf) # Create forced softlink to Files/Dirs to $parm2_/$parmX if no similar items exist in $parm1_ already [ $i_ -ge 2 ] || errorexit "$funct_: Incorrect number of parameters into function: $funct_" if [ $i_ -gt 2 ];then let x_=3 while [ $x_ -le $i_ ] do softlink_=`eval echo \\$parm${x_}_` for ro_item_ in $parm2_/$softlink_;do if [ -f $parm1_/$ro_item_ -o -d $parm1_/$ro_item_ ];then echo "File exists in RW space. Not linking $parm2_/$ro_item_." else [ -L $parm1_/$ro_item_ ] || ln -sf $parm2_/$ro_item_ $parm1_ fi done let x_+=1 done else for ro_item_ in $parm2_/*;do [ "$ro_item_" = "$parm2_/*" ] && break if [ -f $parm1_/${ro_item_##*/} -o -d $parm1_/${ro_item_##*/} ]; then echo "File {ro_item_##*/} exists in RW space. Not linking $ro_item_." else ln -sf $ro_item_ $parm1_ fi done fi ;; ######################################################### lndf2) # lndf2@:: # For files in / create forced symbolic links # pointing to /f (if f is a file in /) # All links are created in target dir; do not overwrite existing files # [ $i_ -eq 3 ] || errorexit "$funct_: Incorrect number of parameters into function: $funct_" for ro_item_ in $parm2_/$parm3_/*;do [ "$ro_item_" = "$parm2_/$parm3_/*" ] && break if [ -f $parm1_/${ro_item_##*/} -o -d $parm1_/${ro_item_##*/} ]; then echo "File {ro_item_##*/} exists in RW space. Not linking to $ro_item_." else ln -sf $parm3_/${ro_item_##*/} $parm1_ fi done ;; ######################################################### wgetx) # Download $parm${n}_ files from source $parm2_ to directory $parm3_, if files not already in $parm3_ # wgetx@http|ftp:[//]www.serverfrom.com:/target/dir:file1:[file2]:[file{n}] [ $i_ -ge 4 ] || errorexit "$funct_: Incorrect number of parameters into function: $funct_" [ $parm1_ = "http" -o $parm1_ = "ftp" ] || errorexit "$funct_: Unknown protocol \"$parm1_\" used for wget function." let x_=4 while [ $x_ -le $i_ ] do file_=`eval echo \\$parm${x_}_` if [ -b $parm3_ ]; then # target is block device (e.g. ramdisk) # trim leading "//" if $parm2_ is in format "//www.spblinux.de" wget $parm1_://${parm2_##*//}/$file_ -O $parm3_ else if ! [ -f $parm3_/$file_ ];then # trim leading "//" if $parm2_ is in format "//www.spblinux.de" wget $parm1_://${parm2_##*//}/$file_ -O $parm3_/$file_ [ $? -eq 0 ] || errorexit "$funct_: Couldn't download \"$parm2_/$file_\"" fi fi let x_+=1 done ;; ######################################################### mntif) # mount $parm${n-1} to parm${n} using optional mount flags defined in $parm${n-2}, $parm${n-3}, ... [ $i_ -ge 2 ] || errorexit "$funct_: Incorrect number of parameters into function: $funct_" [ -d `eval echo \\$parm${i_}_` ] || mkdir_rcrsv_ `eval echo \\$parm${i_}_` mount | grep -q "on `eval echo \\$parm${i_}_`" || mount $args_ [ $? -eq 0 ] || errorexit "$funct_: Failed to mount `eval echo \\$parm${i_}_`" ;; *) errorexit "_fct: Function <$funct_> is not defined" ;; esac }