Fixed overlay_apply() to remove the leading / produced when translating D_foodirs
[opensde-nopast.git] / lib / overlay / overlay-functions.in
blob38f98b416b915cd17bd120ad2334ad15ef0d76f3
1 # --- SDE-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 # Filename: lib/overlay/overlay-functions.in
5 # Copyright (C) 2007 - 2008 The OpenSDE Project
7 # More information can be found in the files COPYING and README.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License. A copy of the
12 # GNU General Public License can be found in the file COPYING.
13 # --- SDE-COPYRIGHT-NOTE-END ---
15 # returns the type of overlay based on the extension of the overlay file
17 overlay_get_type()
19         local ext="${1##*.}"
21         case "$ext" in
22                 sh|exec)        echo "exec"     ;;
23                 txt|patch|ln)   echo "$ext"     ;;
24                 *)      # and skip anything else
25                         ;;
26         esac
29 # returns the name of a target file based on the name overlay file
31 overlay_get_filename()
33         local filename="${1##*/}"; filename="${filename%.*}"
35         # rock_substitute will add a leading / for most translations
36         echo "${filename}" | tr '_%' '/_' | rock_substitute | sed -e 's,^/,,'
39 # apply an overlay dir
40 overlay_apply() {
41         local overlaydir="$1" rootfs="${2:-$root}"
42         local file= source= target= ext=
43         local type= mode= ref= targetdir=
45         for file in "$overlaydir"/*; do
46                 # only consider known overlay types
47                 type=$( overlay_get_type "$file" )
48                 [ -n "$type" ] || continue
50                 # full filename of the target file, and it's directory
51                 target=$( overlay_get_filename "$file" )
52                 targetdir="$rootfs/$target"; targetdir="${targetdir%/*}"
54                 # final adjustments based on the type
55                 case "$type" in
56                         exec)   mode=0755 ;;
57                         *)      mode=0644 ;;
58                 esac
60                 # and finally inject the file
61                 case "$type" in
62                         exec|txt)
63                                 echo_status "injecting $target ($type)"
64                                 mkdir -p "$targetdir"
65                                 [ ! -e "$rootfs/$target" ] || rm -f "$rootfs/$target"
66                                 cp -f "$file" "$rootfs/$target"
67                                 chmod $mode "$rootfs/$target"
68                                 ;;
69                         ln)     ref=$( cat "$file" )
70                                 echo_status "injecting $target -> $ref"
71                                 mkdir -p "$targetdir"
72                                 ln -snf "$ref" "$rootfs/$target"
73                                 ;;
74                         patch)
75                                 echo_status "$(patch -p1 -d "${rootfs}" 2>&1 < $file )"
76                                 ;;
77                 esac
78         done