1#!/bin/bash
2
3# This file is part of the aMule project.
4#
5# Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6# Copyright (c) 2004-2011 Jacobo Vilella (Jacobo221)
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License
10# as published by the Free Software Foundation; either
11# version 2 of the License, or (at your option) any later version.
12# 
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17# 
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
21#
22
23###################################################################
24#                                                                 #
25#     linkcvs will link the whole cvs tree to the given path      #
26#  allowing keeping several compilations from a single cvs tree   #
27#                                                                 #
28#    Warning: This script overwrites old files in target_path     #
29#                                                                 #
30###################################################################
31#                                                                 #
32#           Script valid as of 4rd June 2004 aMule CVS            #
33#         Suggestions go to Jacobo221 at @amule dot .org          #
34#                                                                 #
35###################################################################
36#                                                                 #
37#     This code is distributed under terms of the GPL License     #
38#               http://www.gnu.org/copyleft/gpl.html              #
39#                                                                 #
40###################################################################
41#                                                                 #
42#   EXEC is a list of the files which will be called as scripts   #
43#       EXEC_NUMBER is the amount of EXEC files in the list       #
44#    EXEC_PREFIX is the command to prefix the call of the EXEC    #
45#   EXEC_POSTFIX is the command to postfix the call of the EXEC   #
46#    COPY is a list of files which will be copies, not linked     #
47#         Priviledge order (max to min): EXEC, COPY, CVS          #
48#                                                                 #
49###################################################################
50
51EXEC_NUMBER=0
52# The follwing four lines were needed for aMule versions previous to 2.0.0
53# EXEC_NUMBER=1
54# EXEC=("autogen.sh")
55# EXEC_PREFIX=("original=\`readlink src/SharedFileList.h\` && cp -d --remove-destination \"\$original\" src/ && ")
56# EXEC_POSTFIX=("ln -sf \"\$original\" src/SharedFileList.h")
57COPY=""
58
59if [ $# -ne 2 ]; then
60        echo "`basename $0`: invalid number of arguments"
61        echo "Try \``basename $0` <cvs_tree_path> <target_path>'"
62	exit 1
63elif [ ! -e "$1" ]; then
64        echo "`basename $0`: $1 doesn't exist!"
65	exit 2
66elif [ ! -d "${1}/CVS" ]; then
67        echo "`basename $0`: $1 isn't a CVS tree!"
68	exit 3
69elif [ ! -e "${1}/CVS/Entries" ]; then
70        echo "`basename $0`: $1 has no Entries file!"
71	exit 4
72fi
73
74SOURCE="$1"
75TARGET="$2"
76
77if [ "`echo \"$SOURCE\" | cut -c 1`" != "/" ] && [ "`echo \"$SOURCE\" | cut -c 1`" != "~" ]; then
78                                        SOURCE="`pwd`/$SOURCE"
79fi
80if [ "`echo \"$TARGET\" | cut -c 1`" != "/" ] && [ "`echo \"$TARGET\" | cut -c 1`" != "~" ]; then
81                                        TARGET="`pwd`/$TARGET"
82fi
83
84cvs_rec()
85{
86	local FILES=`cat "${SOURCE}/$1"/CVS/Entries | cut -f 2 -d "/" -s`
87	# Alternative option would be to place `grep "^D/"` and `grep "^"`
88	for file in $FILES; do
89		if [ -d "${SOURCE}/${1}/$file" ]; then
90			mkdir -p "${TARGET}/${1}/$file"
91			cvs_rec "${1}/$file"
92		else
93			target="${TARGET}/${subfile/$SOURCE}/${1}"
94			if [ ! -e "${SOURCE}/${1}/$file" ]; then
95                                echo "${SOURCE}/${1}/$file not found!"
96                        else
97                                ln -sf "${SOURCE}/${1}/$file" "$target"
98                        fi
99		fi
100	done
101}
102
103if [ ! -e "$TARGET" ]; then
104        echo -n "$TARGET doesn't exist. Should I create it? [y/n]"
105        read -s -n 1 opt
106        if [ $opt = Y -o $opt = y ]; then
107                echo " Copying..."
108                mkdir -p "$TARGET"
109        else
110                echo "`basename $0`: target_path invalid"
111                exit 5
112        fi
113fi
114
115cvs_rec ""
116
117for file in $COPY; do
118        mkdir -p "${TARGET}/`dirname $file`"
119        for subfile in ${SOURCE}/$file; do
120                if [ ! -e "$subfile" ]; then
121                        echo "$subfile not found!"
122                else
123                        # -d --remove-destination is esential for links handling
124                        cp -d --remove-destination "$subfile" "${TARGET}/${subfile/$SOURCE}"
125                fi
126        done
127done
128
129i=0
130while [ "$i" -lt "$EXEC_NUMBER" ]; do
131        mkdir -p "${TARGET}/`dirname $file`"
132        for subfile in "${SOURCE}"/${EXEC[$i]}; do
133                if [ ! -e "$subfile" ]; then
134                        echo "$subfile not found!"
135                else
136                        target="${TARGET}/${subfile/$SOURCE}"
137                        rm -f "$target" # Necessary in case $target is a link
138			echo ${EXEC_PREFIX[$i]} > "$target"
139                        echo -n "$subfile && " >> "$target"
140			echo ${EXEC_POSTFIX[$i]} >> "$target"
141                        chmod a+x "$target"
142                fi
143        done
144	i=`expr $i + 1`
145done
146
147